STEPHAN LINZNER: Hi.
My name Stephan Linzner.
And I'm a developer of [INAUDIBLE] on the Android
team here at Google.
Today, I'm going to talk about a new testing API which we've
introduced with Android 4.3, UiAutomation.
UiAutomation is a new API that introduces full screen
introspection support into the Android
instrumentation framework.
Why is this important?
Because it enables testing across application boundaries
within your instrumentation tests.
But let's have a look at a quick example.
Here are some screenshots of the new Google Maps App.
Let's say we want to write a functional test for this
scenario using the instrumentation framework.
What we would do is to start activity on the left.
Click on the Navigation icon.
Wait for the dialog to appear.
Then click the Enable button.
But, as of now, there was no easy way to
actually turn a GPS on.
The problem is that the instrumentation framework is
limited to test within its target package only.
With UiAutomation, this restriction no longer holds.
And now we can test the complete user flow.
Go to the Settings screen, enable the GPS by checking the
box, and press the Back button.
So let's have a quick look at the API supported by
UiAutomation.
UiAutomation enables full screen introspection support
using the accessibility attributes.
It allows for taking screenshots from right within
your instrumentation tests.
And I know this was a feature that was requested
for a very long time.
It also supports the change of the device orientation and
injection of raw input events.
Maybe one more note here.
UiAutomation is a very low level API, but this was done
on purpose to encourage development of your iTesting
tools such as UiAutomator, as well as to allow developers to
use it in their instrumentation tests.
And in fact, the UiAutomator framework, which we introduced
with Jelly Bean 4.1, is built on top of the
UiAutomation APIs.
So let's have a look at some code.
What do you have to do to get started with your automation?
It's very simple.
You just have to inherit from instrumentation test case or
from any other subcloud like activity
instrumentation test case two.
Then, in your test method just call getInstrumentati
on.getUiAutomation to get a reference to the
UiAutomation object.
The question is, how do you run these tests?
It's not so different from what it was before.
You just have to use the adb shell am Instrument command,
with providing the minus w option, your target package,
and your test runner.
So let me show you how you could introspect any visible
screen content from within your instrumentation tests.
First, we get a reference to the UiAutomation object, but
as the window content is presented as a tree of
accessibility notes, in this case, we have to obtain the
active window route accessibility note first.
This is done by calling, get root in activity window.
From there, we can continue and create for any note by
text or ID.
But note, this search is always relative to the
retrieved root accessibility note.
Another cool feature, which UiAutomation introduces, is to
capture screenshots using the instrumentation framework.
In order to retrieve a bitmap of the current screen content,
we have to call take screenshot, which returns a
bitmap object, which then can be written to the file system.
UiAutomation also allows for the injection of raw
ImputEvents into the system.
In order to inject an Event, you have to call the
injectInputEvent method.
For example, if you want to inject a MotionEvent, just
obtain an Event object from the pool and
pass it as a parameter.
But note that it's your responsibility to recycle the
ImputEvent and return it to the pool.
Furthermore, you can execute commands and listen for their
corresponding accessibility events using an
AccessibilityEvent filter.
For example, we can pass a runnable as a parameter to
execute and wait for event, which taps on the screen, and
then verify this tap by listening for a
CorrespondingAcc essibilityEvent.
But what you also can do is listen for all
AccessibilityEvents in the EventStream.
This is also very easy by just passing an instance of
UnaccessibilityEventListener to the set on
AccessibilityEventListener method.
You're listener interface then receives a call back each time
an accessibility event occurs.
Thank you very much for listening.
And happy testing.