譯者序:Google在Android 4.3發(fā)布時(shí)提供了一套新的UiAutomation框架來(lái)支持用戶界面自動(dòng)化測(cè)試,該框架通過(guò)運(yùn)用已有的Accessibility APIs來(lái)模擬用戶跟設(shè)備用戶界面的交互:比如獲取窗口界面控件和注入事件等。如在4.3之前UiAutomator工具是通過(guò)InputManager或者更早的WindowManager來(lái)注入KeyEvent等,4.3之后用的就是新框架UiAutomation使用的Accessibility APIs來(lái)注入事件了。
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的孫吳網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!Class for interacting with the device's UI by simulation user actions and introspection of the screen content. It relies on the platform accessibility APIs to introspect the screen and to perform some actions on the remote view tree. It also allows injecting of arbitrary raw input events simulating user interaction with keyboards and touch devices. One can think of a UiAutomation as a special type of AccessibilityService
which does not provide hooks for the service life cycle and exposes other APIs that are useful for UI test automation.
這是一個(gè)通過(guò)模擬用戶操作來(lái)與設(shè)備用戶界面交互以及獲取屏幕內(nèi)容的類。它依賴于平臺(tái)的輔助功能APIs來(lái)在遠(yuǎn)程的控件樹上獲取屏幕內(nèi)容以及執(zhí)行一些操作。同時(shí)它也允許通過(guò)注入原生事件(譯者注:指的就是InputEvent. KeyEvent也是繼承于InputEvent的,所以說(shuō)它是原生事件)來(lái)模擬用戶的按鍵和觸屏操作。我們可以認(rèn)為UiAutomation就是一個(gè)特殊類型的AccessibilityService,其既不會(huì)為控制服務(wù)的生命周期而提供鉤子函數(shù),也不會(huì)暴露任何其他可以直接用于用戶界面測(cè)試自動(dòng)化的APIs.
The APIs exposed by this class are low-level to maximize flexibility when developing UI test automation tools and libraries. Generally, a UiAutomation client should be using a higher-level library or implement high-level functions. For example, performing a tap on the screen requires construction and injecting of a touch down and up events which have to be delivered to the system by a call to injectInputEvent(InputEvent, boolean)
.
這個(gè)類暴露出來(lái)的APIs是很低層的,目的就是為了在開發(fā)用戶界面測(cè)試自動(dòng)化框架和庫(kù)時(shí)提供大的彈性。總的來(lái)說(shuō),一個(gè)UiAutomation客戶端應(yīng)該使用一些(基于UiAutomation的)更高層次的庫(kù)或者實(shí)現(xiàn)更高層次的方法。比如,模擬一個(gè)用戶在屏幕上的點(diǎn)擊事件需要構(gòu)造并注入一個(gè)按下和一個(gè)彈起事件,然后必須調(diào)用UiAutomation的一個(gè)injectInputEvent(InputEvent, boolean)的調(diào)用來(lái)發(fā)送給操作系統(tǒng)。
The APIs exposed by this class operate across applications enabling a client to write tests that cover use cases spanning over multiple applications. For example, going to the settings application to change a setting and then interacting with another application whose behavior depends on that setting.
這個(gè)類暴露出來(lái)的APIs可以跨應(yīng)用,這樣用戶就可以編寫可以跨越多個(gè)應(yīng)用的測(cè)試用例腳本了。比如,打開系統(tǒng)的設(shè)置應(yīng)用去修改一些設(shè)置然后再與另外一個(gè)依賴于該設(shè)置的應(yīng)用進(jìn)行交互(譯者注:這個(gè)在instrumentation這個(gè)框架可以做不到的)。
Testing and Debugging(來(lái)自android 4.3的APIs官方改動(dòng)文檔)
The new UiAutomation
class provides APIs that allow you to simulate user actions for test automation. By using the platform's AccessibilityService
APIs, the UiAutomation
APIs allow you to inspect the screen content and inject arbitrary keyboard and touch events.
新的UiAutomation這個(gè)類提供了一系列的APIs來(lái)允許你在測(cè)試自動(dòng)化時(shí)模擬用戶的操作。通過(guò)封裝使用了平臺(tái)上的AccessibilityService APIs, UiAutomation
APIs允許你獲取窗口(控件)內(nèi)容并且注入按鍵和觸屏事件。
To get an instance of UiAutomation
, call Instrumentation.getUiAutomation()
. In order for this to work, you must supply the -w
option with the instrument
command when running your InstrumentationTestCase
from adb shell
.
你可以通過(guò)調(diào)用Instrumentation.getUiAutomation()來(lái)獲得UiAutomation的一個(gè)實(shí)例。為了讓它工作起來(lái),當(dāng)你在adb shell上運(yùn)行你的InstrumentationTestCase的時(shí)候你還需要為instrument命令提供-w這個(gè)選項(xiàng)。
With the UiAutomation
instance, you can execute arbitrary events to test your app by callingexecuteAndWaitForEvent()
, passing it a Runnable
to perform, a timeout period for the operation, and an implementation of the UiAutomation.AccessibilityEventFilter
interface. It's within yourUiAutomation.AccessibilityEventFilter
implementation that you'll receive a call that allows you to filter the events that you're interested in and determine the success or failure of a given test case.
通過(guò)UiAutomation的實(shí)例,你可以調(diào)用其executeAndWaitForEvent()對(duì)你的應(yīng)用注入不同的事件來(lái)進(jìn)行測(cè)試:該函數(shù)會(huì)接受一個(gè)可執(zhí)行 Runnable線程對(duì)象用來(lái)執(zhí)行事件注入操作,一個(gè)操作超時(shí),以及一個(gè)實(shí)現(xiàn)了UiAutomation.AccessibilityEventFilter的類的實(shí)例。正是在這個(gè)UiAutomation.AccessibilityEventFilter實(shí)現(xiàn)類里面你會(huì)收到一個(gè)回調(diào)來(lái)讓你過(guò)濾那些你喜歡的事件并決定你的測(cè)試用例是否通過(guò)。
To observe all the events during a test, create an implementation of UiAutomation.OnAccessibilityEventListener
and pass it to setOnAccessibilityEventListener()
. Your listener interface then receives a call toonAccessibilityEvent()
each time an event occurs, receiving an AccessibilityEvent
object that describes the event.
如果要在測(cè)試時(shí)監(jiān)控所有的事件,你需要?jiǎng)?chuàng)建一個(gè)UiAutomation.OnAccessibilityEventListener的實(shí)現(xiàn)類然后把它的實(shí)例傳遞給setOnAccessibilityEventListener()。你的監(jiān)聽接口將會(huì)在每次有事件觸發(fā)的時(shí)候接收到一個(gè)發(fā)送給onAccessibilityEvent()的回調(diào),里面的參數(shù)就是一個(gè)描述該事件的AccessibilityEvent
的對(duì)象
There is a variety of other operations that the UiAutomation
APIs expose at a very low level to encourage the development of UI test tools such as uiautomator. For instance, UiAutomation
can also:
UiAutomation
APIs還暴露了很多其他的低層次的操作來(lái)鼓勵(lì)大家去開發(fā)如uiautomator這樣的用戶界面測(cè)試工具。比如UiAutomation還可以做以下事情:
And most importantly for UI test tools, the UiAutomation
APIs work across application boundaries, unlike those inInstrumentation
.
最為用戶界面自動(dòng)化測(cè)試工具,最重要的一點(diǎn)是,UiAutomation
APIs 可以跨應(yīng)用工作,而不像Instrumentation提供了的那些APIs.
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。