译者序: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.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.
假设要在測试时监控全部的事件,你须要创建一个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.