Android4.3引入的UiAutomation新框架官方簡單介紹


譯者序:Google在Android 4.3公布時提供了一套新的UiAutomation框架來支持用戶界面自己主動化測試。該框架通過運用已有的Accessibility APIs來模擬用戶跟設備用戶界面的交互:比方獲取窗體界面控件和注入事件等。

如在4.3之前UiAutomator工具是通過InputManager或者更早的WindowManager來注入KeyEvent等。4.3之后用的就是新框架UiAutomation使用的Accessibility APIs來注入事件了。

Class Overview/概覽


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.

這是一個通過模擬用戶操作來與設備用戶界面交互以及獲取屏幕內容的類。它依賴於平台的輔助功能APIs來在遠程的控件樹上獲取屏幕內容以及運行一些操作。同一時候它也同意通過注入原生事件(譯者注:指的就是InputEvent. KeyEvent也是繼承於InputEvent的,所以說它是原生事件)來模擬用戶的按鍵和觸屏操作。我們能夠覺得UiAutomation就是一個特殊類型的AccessibilityService,其既不會為控制服務的生命周期而提供鈎子函數,也不會暴露不論什么其它能夠直接用於用戶界面測試自己主動化的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).

這個類暴露出來的APIs是非常低層的,目的就是為了在開發用戶界面測試自己主動化框架和庫時提供最大的彈性。總的來說,一個UiAutomationclient應該使用一些(基於UiAutomation的)更高層次的庫或者實現更高層次的方法。

比方。模擬一個用戶在屏幕上的點擊事件須要構造並注入一個按下和一個彈起事件。然后必須調用UiAutomation的一個injectInputEvent(InputEvent, boolean)的調用來發送給操作系統。

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.

這個類暴露出來的APIs能夠跨應用,這樣用戶就能夠編寫能夠跨越多個應用的測試用例腳本了。比方。打開系統的設置應用去改動一些設置然后再與另外一個依賴於該設置的應用進行交互(譯者注:這個在instrumentation這個框架能夠做不到的)。

Testing and Debugging(來自android 4.3的APIs官方修改文檔)


Automated UI testing/用戶界面測試自己主動化

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這個類提供了一系列的APIs來同意你在測試自己主動化時模擬用戶的操作。通過封裝使用了平台上的AccessibilityService  APIs, UiAutomation APIs同意你獲取窗體(控件)內容而且注入按鍵和觸屏事件。

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.

你能夠通過調用Instrumentation.getUiAutomation()來獲得UiAutomation的一個實例。

為了讓它工作起來,當你在adb shell上執行你的InstrumentationTestCase的時候你還須要為instrument命令提供-w這個選項。

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.

通過UiAutomation的實例,你能夠調用其executeAndWaitForEvent()對你的應用注入不同的事件來進行測試:該函數會接受一個可運行 Runnable線程對象用來運行事件注入操作,一個操作超時,以及一個實現了UiAutomation.AccessibilityEventFilter的類的實例。正是在這個UiAutomation.AccessibilityEventFilter實現類里面你會收到一個回調來讓你過濾那些你喜歡的事件並決定你的測試用例是否通過。

To observe all the events during a test, create an implementation of UiAutomation.OnAccessibilityEventListenerand 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.

假設要在測試時監控全部的事件,你須要創建一個UiAutomation.OnAccessibilityEventListener的實現類然后把它的實例傳遞給setOnAccessibilityEventListener()。你的監聽接口將會在每次有事件觸發的時候接收到一個發送給onAccessibilityEvent()的回調,里面的參數就是一個描寫敘述該事件的AccessibilityEvent 的對象

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還暴露了非常多其它的低層次的操作來鼓舞大家去開發如uiautomator這種用戶界面測試工具。

比方UiAutomation還能夠做下面事情:

  • Inject input events/注入事件
  • Change the orientation of the screen/改變屏幕的方向
  • Take screenshots/截屏

And most importantly for UI test tools, the UiAutomation APIs work across application boundaries, unlike those inInstrumentation.

最為用戶界面自己主動化測試工具。最重要的一點是,UiAutomation APIs 能夠跨應用工作。而不像Instrumentation提供了的那些APIs.


 

作者

自主博客

微信

CSDN

天地會珠海分舵

http://techgogogo.com


服務號:TechGoGoGo

掃描碼:

http://blog.csdn.net/zhubaitian





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM