blob: 3f4bf7a586423129d3584dbeb6cd60a1c55595f5 [file] [log] [blame]
Quddus Chong94b3f8e2012-10-05 15:30:20 -07001page.title=UI Testing
2parent.title=Testing
3parent.link=index.html
4@jd:body
5
6<div id="qv-wrapper">
7 <div id="qv">
8 <h2>In this document</h2>
9 <ol>
10 <li><a href="#overview">Overview</a></li>
11 <ul>
12 <li><a href="#workflow">Workflow</a></li>
13 </ul>
14 <li><a href="#uianalysis">Analyzing Your UI</a></li>
15 <li><a href="#prepare">Preparing to Test</a>
16 <ul>
17 <LI><a href="#loading">Load the App</a></LI>
18 <LI><a href="#identifyUI">Identify UI Components</a></LI>
19 <LI><a href="#accessibility">Ensure Accessibility</a></LI>
20 <LI><a href="#configure">Configure Development Environment</a></LI>
21 </ul>
22 </li>
23 <li><a href="#creating">Creating Tests</a>
24 <ul>
25 <LI><a href="#classes">uiautomator API</a></LI>
26 <li><a href="#sample">Sample Test Case</a>
27 </ul>
28 </li>
29 <li><a href="#builddeploy">Building and Deploying Tests</a></li>
30 <li><a href="#running">Running Tests</a></li>
31 <li><a href="#bestpractices">Best Practices</a></li>
32 </ol>
33 <h2>Key classes</h2>
34 <ol>
35 <li><a href="{@docRoot}tools/help/uiautomator/IAutomationSupport.html">IAutomationSupport</a></li>
36 <li><a href="{@docRoot}tools/help/uiautomator/UiAutomatorTestCase.html">UiAutomatorTestCase</a></li>
37 <li><a href="{@docRoot}tools/help/uiautomator/UiCollection.html">UiCollection</a></li>
38 <li><a href="{@docRoot}tools/help/uiautomator/UiDevice.html">UiDevice</a></li>
39 <li><a href="{@docRoot}tools/help/uiautomator/UiObject.html">UiObject</a></li>
40 <li><a href="{@docRoot}tools/help/uiautomator/UiScrollable.html">UiScrollable</a></li>
41 <li><a href="{@docRoot}tools/help/uiautomator/UiSelector.html">UiSelector</a></li>
42 </ol>
43 <h2>See Also</h2>
44 <ol>
45 <li>
46 <a href="{@docRoot}tools/help/uiautomator/index.html">uiautomator (reference)</a>
47 </li>
48 </ol>
49 </div>
50</div>
51
52<p>
53In addition to unit testing the individual components that make up your Android application (such as activities, services, and content providers), it is also important that you test the behavior of your application’s user interface (UI) when it is running on a device. UI testing ensures that your application returns the correct UI output in response to a sequence of user actions on a device, such as entering keyboard input or pressing toolbars, menus, dialogs, images, and other UI controls.
54</p>
55<p>
56Functional or black-box UI testing does not require testers to know the internal implementation details of the app, only its expected output when a user performs a specific action or enters a specific input. This approach allows for better separation of development and testing roles in your organization.
57</p>
58<p>One common approach to UI testing is to run tests manually and verify that the app is behaving as expected. However, this approach can be time-consuming, tedious, and error-prone. A more efficient and reliable approach is to automate the UI testing with a software testing framework. Automated testing involves creating programs to perform testing tasks (test cases) to cover specific usage scenarios, and then using the testing framework to run the test cases automatically and in a repeatable manner.</p>
59
60<h2 id="overview">Overview</h2>
61<p>The Android SDK provides the following tools to support automated, functional UI testing on your application:
62<ul>
63<LI>{@code uiautomatorviewer} - A GUI tool to scan and analyze the UI components of an Android application.</LI>
64<LI>{@code uiautomator} - A Java library containing APIs to create customized functional UI tests, and an execution engine to automate and run the tests.</LI>
65</ul></p>
66
67<p>To use these tools, you must have the following versions of the Android development tools installed:
68<ul>
69<LI>Android SDK Tools, Revision 21 or higher</LI>
70<LI>Android SDK Platform, API 16 or higher</LI>
71</ul>
72</p>
73
74<h3 id="workflow">Workflow for the the uiautomator testing framework</h3>
75<p>Here's a short overview of the steps required to automate UI testing:
76<ol>
77<LI>Prepare to test by installing the app on a test device, analyzing the app’s UI components, and ensuring that your application is accessible by the test automation framework.</LI>
78<li>Create automated tests to simulate specific user interactions on your application.</li>
79<li>Compile your test cases into a JAR file and install it on your test device along with your app.</li>
80<li>Run the tests and view the test results.</li>
81<li>Correct any bugs or defects discovered in testing.</li>
82</ol>
83</p>
84
85<h2 id="uianalysis">Analyzing Your Application's UI</h2>
86<p>Before you start writing your test cases, it's helpful to familiarize yourself with the UI components (including the views and controls) of the targeted application. You can use the {@code uiautomatorviewer} tool to take a snapshot of the foreground UI screen on any Android device that is connected to your development machine. The {@code uiautomatorviewer} tool provides a convenient visual interface to inspect the layout hierarchy and view the properties of the individual UI components that are displayed on the test device. Using this information, you can later create {@code uiautomator} tests with selector objects that target specific UI components to test.</p>
87
88<a href="{@docRoot}images/testing/UIAutomatorViewer.png">
89 <img src="{@docRoot}images/testing/UIAutomatorViewer.png"
90 alt="User interface of uiautomatorviewer tool" height="327px" id="figure1"/>
91</a>
92<p class="img-caption">
Mark Doliner9525f2a2014-01-02 11:17:47 -080093 <strong>Figure 1.</strong> The {@code uiautomatorviewer} showing the captured interface of a test device.
Quddus Chong94b3f8e2012-10-05 15:30:20 -070094</p>
95
96<p>To analyze the UI components of the application that you want to test:</p>
97<ol>
98<li>Connect your Android device to your development machine.</li>
Neil Fuller71fbb812015-11-30 09:51:33 +000099<li>Open a terminal window and navigate to {@code <android-sdk>/tools/}.</li>
Quddus Chong94b3f8e2012-10-05 15:30:20 -0700100<LI>Run the tool with this command:<pre>$ uiautomatorviewer</pre></LI>
101<li><p>To capture a screen for analysis, click the <strong>Device Screenshot</strong> button in the GUI of the {@code uiautomatorviewer} tool.</p>
102<p class="note"><strong>Note: </strong>If you have more than one device connected, specify the device for screen capture by setting the {@code ANDROID_SERIAL} environment variable:
103 <ol type="a">
104 <li>Find the serial numbers for your connected devices by running this command:<pre>$ adb devices</pre> </li>
105 <li>Set the {@code ANDROID_SERIAL} environment variable to select the device to test:
106 <ul>
107 <li>In Windows: <pre>set ANDROID_SERIAL=&lt;device serial number&gt;</pre></li>
108 <li>In UNIX: <pre>export ANDROID_SERIAL=&lt;device serial number&gt;</pre></li>
109 </ul>
110 </li>
111 </ol>
112If you are connected to only a single device, you do not need to set the ANDROID_SERIAL environment variable.</p>
113</li>
114<li>View the UI properties for your application:
115 <ul>
116 <LI>Hover over the snapshot in the left-hand panel to see the UI components identified by the {@code uiautomatorviewer} tool. You can view the component’s properties listed in the lower right-hand panel, and the layout hierarchy in the upper right-hand panel.</LI>
117 <li>Optionally, click on the <strong>Toggle NAF Nodes</strong> button to see UI components that are not accessible to the {@code uiautomator} testing framework. Only limited information may be available for these components.</li>
118 </ul>
119</li>
120</ol>
121
122<h2 id="prepare">Preparing to Test</h2>
123<p>Before using the {@code uiautomator} testing framework, complete these pre-flight tasks:
124</p>
125<h3 id="loading">Load the application to a device</h3>
Scott Main188315c2013-01-03 18:02:21 -0800126<p>If you are reading this document, chances are that the Android application that you want to test has not been published yet. If you have a copy of the APK file, you can install the APK onto a test device by using the {@code adb} tool. To learn how to install an APK file using the {@code adb} tool, see the <a href="{@docRoot}tools/help/adb.html#move">{@code adb}</a> documentation. </p>
Quddus Chong94b3f8e2012-10-05 15:30:20 -0700127
128<h3 id="identifyUI">Identify the application’s UI components</h3>
129<p>Before writing your {@code uiautomator} tests, first identify the UI components in the application that you want to test. Typically, good candidates for testing are UI components that are visible and that users can interact with. The UI components should also have visible text labels, <a href="{@docRoot}reference/android/view/View.html#attr_android:contentDescription">{@code android:contentDescription}</a> values, or both.
130
131<p>You can inspect the visible screen objects in an application conveniently by using the {@code uiautomatorviewer} tool. For more information about how to analyze an application screen with this tool, see the section <a href="#uianalaysis">Analyzing Your Application’s UI</a>. For more information about the common types of UI components provided by Android, see <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a>.</p>
132
133<h3 id="accessibility">Ensure that the application is accessible</h3>
134<p>This step is required because the {@code uiautomator} tool depends on the accessibility features of the Android framework to execute your functional UI tests. You should include these minimum optimizations to support the {@code uiautomator} tool:
135<ul>
136<LI>Use the <a href="{@docRoot}reference/android/view/View.html#attr_android:contentDescription">{@code android:contentDescription}</a> attribute to label the {@link android.widget.ImageButton}, {@link android.widget.ImageView}, {@link android.widget.CheckBox} and other user interface controls.</LI>
Quddus Chong7b2483f2012-11-12 15:33:30 -0800137<li>Provide an <a href="{@docRoot}reference/android/widget/TextView.html#attr_android:hint">{@code android:hint}</a> attribute <em>instead</em> of a content description for {@link android.widget.EditText} fields</li>
138<li>Associate an <a href="{@docRoot}reference/android/widget/TextView.html#attr_android:hint">{@code android:hint}</a> attribute with any graphical icons used by controls that provide feedback to the user (for example, status or state information).</li>
Quddus Chong94b3f8e2012-10-05 15:30:20 -0700139<li>Make sure that all the user interface elements are accessible with a directional controller, such as a trackball or D-pad.</li>
140<li>Use the {@code uiautomatorviewer} tool to ensure that the UI component is accessible to the testing framework. You can also test the application by turning on accessibility services like TalkBack and Explore by Touch, and try using your application using only directional controls. </li>
141</ul>
142</p>
143
Scott Kennedy7ed189e2013-01-11 22:31:43 -0800144<p>For more information about implementing and testing accessibility, see <a href="{@docRoot}guide/topics/ui/accessibility/apps.html">Making Applications Accessible</a>.</p>
Quddus Chong94b3f8e2012-10-05 15:30:20 -0700145
146<p class="note"><strong>Note: </strong>To identify the non-accessible components in the UI, click on the <strong>Toggle NAF Nodes</strong> option in the {@code uiautomatorviewer} tool.</p>
147
Scott Kennedy7ed189e2013-01-11 22:31:43 -0800148<p>Generally, Android application developers get accessibility support for free, courtesy of the {@link android.view.View} and {@link android.view.ViewGroup} classes. However, some applications use custom view components to provide a richer user experience. Such custom components won't get the accessibility support that is provided by the standard Android UI components. If this applies to your application, ensure that the application developer exposes the custom drawn UI components to Android accessibility services, by implementing the {@link android.view.accessibility.AccessibilityNodeProvider} class. For more information about making custom view components accessible, see <a href="{@docRoot}guide/topics/ui/accessibility/apps.html#custom-views">Making Applications Accessible</a>.</p>
Quddus Chong94b3f8e2012-10-05 15:30:20 -0700149
150<h3 id="configure">Configure your development environment</h3>
151<p>If you're developing in Eclipse, the Android SDK provides additional tools that help you write test cases using {@code uiautomator} and buiild your JAR file. In order to set up Eclipse to assist you, you need to create a project that includes the {@code uiautomator} client library, along with the Android SDK library. To configure Eclipse:</p>
152<ol>
153<li>Create a new Java project in Eclipse, and give your project a name that is relevant to the tests you’re about to create (for example, "MyAppNameTests"). In the project, you will create the test cases that are specific to the application that you want to test.</li>
154<li>From the <strong>Project Explorer</strong>, right-click on the new project that you created, then select <strong>Properties > Java Build Path</strong>, and do the following:
155 <ol type="a">
156 <LI>Click <strong>Add Library > JUnit</strong> then select <strong>JUnit3</strong> to add JUnit support.</LI>
157 <li>Click <strong>Add External JARs...</strong> and navigate to the SDK directory. Under the platforms directory, select the latest SDK version and add both the {@code uiautomator.jar} and {@code android.jar} files.</li>
158 </ol>
159</li>
160</ol>
Neil Fuller71fbb812015-11-30 09:51:33 +0000161<p>If you did not configure Eclipse as your development environment, make sure that the {@code uiautomator.jar} and {@code android.jar} files from the {@code <android-sdk>/platforms/<sdk>} directory are in your Java class path.</p>
Quddus Chong94b3f8e2012-10-05 15:30:20 -0700162<p>Once you have completed these prerequisite tasks, you're almost ready to start creating your {@code uiautomator} tests. </li>
163
164<h2 id="creating">Creating uiautomator Tests</h2>
165<p>To build a test that runs in the {@code uiautomator} framework, create a test case that extends the <a href="{@docRoot}tools/help/uiautomator/UiAutomatorTestCase.html">{@code UiAutomatorTestCase}</a> class. In Eclipse, the test case file goes under the {@code src} directory in your project. Later, you will build the test case as a JAR file, then copy this file to the test device. The test JAR file is not an APK file and resides separately from the application that you want to test on the device.</p>
166
167<p>Because the <a href="{@docRoot}tools/help/uiautomator/UiAutomatorTestCase.html">{@code UiAutomatorTestCase}</a> class extends {@code junit.framework.TestCase}, you can use the JUnit {@code Assert} class to test that UI components in the app return the expected results. To learn more about JUnit, you can read the documentation on the <a href="http://www.junit.org/">junit.org</a> home page.</p>
168
169<p>The first thing your test case should do is access the device that contains the target app. It’s also good practice to start the test from the Home screen of the device. From the Home screen (or some other starting location you’ve chosen in the target app), you can use the classes provided by the {@code uiautomator} API to simulate user actions and to test specific UI components. For an example of how to put together a {@code uiautomator} test case, see the <a href="#sample">sample test case</a>.</p>
170
171<h3 id="classes">uiautomator API</h3>
Neil Fuller71fbb812015-11-30 09:51:33 +0000172<p>The {@code uiautomator} API is bundled in the {@code uiautomator.jar} file under the {@code <android-sdk>/platforms/} directory. The API includes these key classes that allow you to capture and manipulate UI components on the target app:</p>
Quddus Chong94b3f8e2012-10-05 15:30:20 -0700173<dl>
174<DT><a href="{@docRoot}tools/help/uiautomator/UiDevice.html">{@code UiDevice}</a></DT>
175<dd><p>Represents the device state. In your tests, you can call methods on the <a href="{@docRoot}tools/help/uiautomator/UiDevice.html">{@code UiDevice}</a> instance to check for the state of various properties, such as current orientation or display size. Your tests also can use the <a href="{@docRoot}tools/help/uiautomator/UiDevice.html">{@code UiDevice}</a> instance to perform device level actions, such as forcing the device into a specific rotation, pressing the d-pad hardware button, or pressing the Home and Menu buttons.</p>
Scott Main188315c2013-01-03 18:02:21 -0800176<p>To get an instance of <a href="{@docRoot}tools/help/uiautomator/UiDevice.html">{@code UiDevice}</a> and simulate a Home button press:
Quddus Chong94b3f8e2012-10-05 15:30:20 -0700177<pre>
178getUiDevice().pressHome();
179</pre></p></dd>
180
181<dt><a href="{@docRoot}tools/help/uiautomator/UiSelector.html">{@code UiSelector}</a></dt>
182<dd>Represents a search criteria to query and get a handle on specific elements in the currently displayed UI.
183If more than one matching element is found, the first matching element in the layout hierarchy is returned as the target {@code UiObject}. When constructing a <a href="{@docRoot}tools/help/uiautomator/UiSelector.html">{@code UiSelector}</a>, you can chain together multiple properties to refine your search. If no matching UI element is found, a <a href="{@docRoot}tools/help/uiautomator/UiAutomatorObjectNotFoundException.html">{@code UiAutomatorObjectNotFoundException}</a> is thrown. You can use the <a href="{@docRoot}tools/help/uiautomator/UiSelector.html#childSelector(com.android.uiautomator.core.UiSelector)">{@code childSelector()}</a> method to nest multiple <a href="{@docRoot}tools/help/uiautomator/UiSelector.html">{@code UiSelector}</a> instances. For example, the following code example shows how to specify a search to find the first {@link android.widget.ListView} in the currently displayed UI, then search within that {@link android.widget.ListView} to find a UI element with the text property {@code Apps}.
184<pre>
185UiObject appItem = new UiObject(new UiSelector()
186 .className("android.widget.ListView").instance(1)
187 .childSelector(new UiSelector().text("Apps")));
188</pre>
189</dd>
190
191<dt><a href="{@docRoot}tools/help/uiautomator/UiObject.html">{@code UiObject}</a></dt>
192<dd>Represents a UI element. To create a <a href="{@docRoot}tools/help/uiautomator/UiObject.html">{@code UiObject}</a> instance, use a {@code UiSelector} that describes how to search for, or select, the UI element.
193<p>The following code example shows how to construct <a href="{@docRoot}tools/help/uiautomator/UiObject.html">{@code UiObject}</a> instances that represent a <strong>Cancel</strong> button and a <strong>OK</strong> button in your application.</p>
194<pre>
195UiObject cancelButton = new UiObject(new UiSelector().text("Cancel"));
196UiObject okButton = new UiObject(new UiSelector().text("OK"));
197</pre>
198<p>You can reuse the <a href="{@docRoot}tools/help/uiautomator/UiObject.html">{@code UiObject}</a> instances that you have created in other parts of your app testing, as needed. Note that the {@code uiautomator} test framework searches the current display for a match every time your test uses a <a href="{@docRoot}tools/help/uiautomator/UiObject.html">{@code UiObject}</a> instance to click on a UI element or query a property.</p>
199<p>In the following code example, the {@code uiautomator} test framework searches for a UI element with the text property {@code OK}. If a match is found and if the element is enabled, the framework simulates a user click action on the element.</p>
200<pre>
201if(okButton.exists() && okButton.isEnabled())
202{
203 okButton.click();
204}
205</pre>
206<p>You can also restrict the search to find only elements of a specific class. For example, to find matches of the {@link android.widget.Button} class:</p>
207<pre>
208UiObject cancelButton = new UiObject(new UiSelector().text("Cancel")
209 .className("android.widget.Button"));
210UiObject okButton = new UiObject(new UiSelector().text("OK")
211 .className("android.widget.Button"));
212</pre>
213</dd>
214
215<dt><a href="{@docRoot}tools/help/uiautomator/UiCollection.html">{@code UiCollection}</a></dt>
Scott Main188315c2013-01-03 18:02:21 -0800216<dd>Represents a collection of items, for example songs in a music album or a list of emails in an inbox. Similar to a <a href="{@docRoot}tools/help/uiautomator/UiObject.html">{@code UiObject}</a>, you construct a <a href="{@docRoot}tools/help/uiautomator/UiCollection.html">{@code UiCollection}</a> instance by specifying a <a href="{@docRoot}tools/help/uiautomator/UiSelector.html">{@code UiSelector}</a>. The <a href="{@docRoot}tools/help/uiautomator/UiSelector.html">{@code UiSelector}</a> for a <a href="{@docRoot}tools/help/uiautomator/UiCollection.html">{@code UiCollection}</a> should search for a UI element that is a container or wrapper of other child UI elements (such as a layout view that contains child UI elements). For example, the following code snippet shows how to construct a <a href="{@docRoot}tools/help/uiautomator/UiCollection.html">{@code UiCollection}</a> to represent a video album that is displayed within a {@link android.widget.FrameLayout}:
Quddus Chong94b3f8e2012-10-05 15:30:20 -0700217<pre>
218UiCollection videos = new UiCollection(new UiSelector()
219 .className("android.widget.FrameLayout"));
220</pre>
221<p>If the videos are listed within a {@link android.widget.LinearLayout} view, and you want to to retrieve the number of videos in this collection:</p>
222<pre>
223int count = videos.getChildCount(new UiSelector()
224 .className("android.widget.LinearLayout"));
225</pre>
226<p>If you want to find a specific video that is labeled with the text element {@code Cute Baby Laughing} from the collection and simulate a user-click on the video:</p>
227<pre>
228UiObject video = videos.getChildByText(new UiSelector()
229 .className("android.widget.LinearLayout"), "Cute Baby Laughing");
230video.click();
231</pre>
232<p>Similarly, you can simulate other user actions on the UI object. For example, if you want
233to simulate selecting a checkbox that is associated with the video:</p>
234<pre>
235UiObject checkBox = video.getChild(new UiSelector()
236 .className("android.widget.Checkbox"));
237if(!checkBox.isSelected()) checkbox.click();
238</pre>
239</dd>
240
241<dt><a href="{@docRoot}tools/help/uiautomator/UiScrollable.html">{@code UiScrollable}</a></dt>
242<dd>Represents a scrollable collection of UI elements. You can use the <a href="{@docRoot}tools/help/uiautomator/UiScrollable.html">{@code UiScrollable}</a> class to simulate vertical or horizontal scrolling across a display. This technique is helpful when a UI element is positioned off-screen and you need to scroll to bring it into view.
243<p>For example, the following code shows how to simulate scrolling down the Settings menu and clicking on an <strong>About tablet</strong> option:</p>
244<pre>
245UiScrollable settingsItem = new UiScrollable(new UiSelector()
246 .className("android.widget.ListView"));
247UiObject about = settingsItem.getChildByText(new UiSelector()
248 .className("android.widget.LinearLayout"), "About tablet");
249about.click()
250</pre>
251</dd>
252</dl>
253<p>For more information about these APIs, see the <a href="{@docRoot}tools/help/uiautomator/index.html">{@code uiautomator}</a> reference.</p>
254
255<h3 id="sample">A sample uiautomator test case</h3>
256<p>The following code example shows a simple test case which simulates a user bringing up the Settings app in a stock Android device. The test case mimics all the steps that a user would typically take to perform this task, including opening the Home screen, launching the <strong>All Apps</strong> screen, scrolling to the <strong>Settings</strong> app icon, and clicking on the icon to enter the Settings app.</p>
257<pre>
258package com.uia.example.my;
259
260// Import the uiautomator libraries
261import com.android.uiautomator.core.UiObject;
262import com.android.uiautomator.core.UiObjectNotFoundException;
263import com.android.uiautomator.core.UiScrollable;
264import com.android.uiautomator.core.UiSelector;
265import com.android.uiautomator.testrunner.UiAutomatorTestCase;
266
267public class LaunchSettings extends UiAutomatorTestCase {
268
269 public void testDemo() throws UiObjectNotFoundException {
270
271 // Simulate a short press on the HOME button.
272 getUiDevice().pressHome();
273
274 // We’re now in the home screen. Next, we want to simulate
275 // a user bringing up the All Apps screen.
276 // If you use the uiautomatorviewer tool to capture a snapshot
277 // of the Home screen, notice that the All Apps button’s
278 // content-description property has the value “Apps”. We can
279 // use this property to create a UiSelector to find the button.
280 UiObject allAppsButton = new UiObject(new UiSelector()
281 .description("Apps"));
282
283 // Simulate a click to bring up the All Apps screen.
284 allAppsButton.clickAndWaitForNewWindow();
285
286 // In the All Apps screen, the Settings app is located in
287 // the Apps tab. To simulate the user bringing up the Apps tab,
288 // we create a UiSelector to find a tab with the text
289 // label “Apps”.
290 UiObject appsTab = new UiObject(new UiSelector()
291 .text("Apps"));
292
293 // Simulate a click to enter the Apps tab.
294 appsTab.click();
295
296 // Next, in the apps tabs, we can simulate a user swiping until
297 // they come to the Settings app icon. Since the container view
298 // is scrollable, we can use a UiScrollable object.
299 UiScrollable appViews = new UiScrollable(new UiSelector()
300 .scrollable(true));
301
302 // Set the swiping mode to horizontal (the default is vertical)
303 appViews.setAsHorizontalList();
304
305 // Create a UiSelector to find the Settings app and simulate
306 // a user click to launch the app.
307 UiObject settingsApp = appViews.getChildByText(new UiSelector()
308 .className(android.widget.TextView.class.getName()),
309 "Settings");
310 settingsApp.clickAndWaitForNewWindow();
311
312 // Validate that the package name is the expected one
313 UiObject settingsValidation = new UiObject(new UiSelector()
314 .packageName("com.android.settings"));
315 assertTrue("Unable to detect Settings",
316 settingsValidation.exists());
317 }
318}
319</pre>
320
321<h2 id="builddeploy">Building and Deploying Your uiautomator Tests</h2>
322<p>Once you have coded your test, follow these steps to build and deploy your test JAR to your target Android test device:</p>
323<ol>
324<li>Create the required build configuration files to build the output JAR. To generate the build configuration files, open a terminal and run the following command:
Quddus Chong4e58eca2012-11-30 16:26:35 -0800325<pre>&lt;android-sdk&gt;/tools/android create uitest-project -n &lt;name&gt; -t 1 -p &lt;path&gt;</pre>
Neil Fuller71fbb812015-11-30 09:51:33 +0000326The {@code <name>} is the name of the project that contains your {@code uiautomator} test source files, and the {@code <path>} is the path to the corresponding project directory.
Quddus Chong94b3f8e2012-10-05 15:30:20 -0700327</li>
328<LI>From the command line, set the {@code ANDROID_HOME} variable:
329<ul>
330<li>In Windows:
331<pre>set ANDROID_HOME=&lt;path_to_your_sdk&gt;</pre>
332</li>
333<li>In UNIX:
334<pre>export ANDROID_HOME=&lt;path_to_your_sdk&gt;</pre>
335</li>
336</ul>
337</LI>
338<li>Go to the project directory where your {@code build.xml} file is located and build your test JAR. <pre>ant build</pre></li>
339<li>Deploy your generated test JAR file to the test device by using the {@code adb push} command: <pre>adb push &lt;path_to_output_jar&gt; /data/local/tmp/</pre>
340<p>Here’s an example: <pre>adb push ~/dev/workspace/LaunchSettings/bin/LaunchSettings.jar /data/local/tmp/</pre></p>
341</li>
342</ol>
343
344<h2 id="running">Running uiautomator Tests</h2>
345<p>Here’s an example of how to run a test that is implemented in the {@code LaunchSettings.jar} file. The tests are bundled in the {@code com.uia.example.my} package:</p>
346<pre>adb shell uiautomator runtest LaunchSettings.jar -c com.uia.example.my.LaunchSettings</pre>
347<p>To learn more about the syntax, subcommands, and options for {@code uiautomator}, see the <a href="{@docRoot}tools/help/uiautomator/index.html">{@code uiautomator}</a> reference.</p>
348
349
350<h2 id="bestpractices">Best Practices</h2>
351<p>Here are some best practices for functional UI testing with the {@code uiautomator} framework: </p>
352<ul>
353<LI>Ensure that you validate the same UI functions on your application across the various types of devices that your application might run on (for example, devices with different screen densities).</LI>
354<li>You should also test your UI against common scenarios such as in-coming phone calls, network interruptions, and user-initiated switching to other applications on the device.</li>
355</ul>
356