Android automated testing (4) (uiautomator)

Source: Internet
Author: User

Android automated testing (4) (uiautomator)

In the previous series of articles, I introduced how to use java to implement Android automated testing (1) how to install and uninstall an application (java) and Android automated testing (2) search for objects by ID (java); then introduce how to implement Android automated testing using python language (3) Search for objects by ID & touch & type (python ). I also said that I will write some articles about UI testing and code coverage testing later. Today we will introduce UI testing.

1. Summary

For students who have done java unit tests, it is relatively simple to use Android unit tests. For more information, see How to perform Android unit tests. In this way, the test on business logic is solved. The test interface is inconvenient, but there is an obvious defect. For android applications, the interface occupies a very important part.

In this case, you can use the class library uiautomator. jar. Here I will not detail how to use the Android uiautomator class library. For specific usage instructions, see Android UI Testing (English version) and Android uiautomator getting started official tutorial (Chinese version ).

2. Core class

I will mainly mention the most important core classes, including UiDevice, UISelector, and UiObject. How to find and act on objects is the core of the test.

UiDevice

Represents the device state. in your tests, you can call methods on the UiDevice instance to check for the state of various properties, such as current orientation or display size. your tests also can use the UiDevice 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.
UiDevice indicates the device status. During the test, you can call the method of the UiDevice instance to check the status of different properties, such as the current display size of goods in the orientation of the screen. The test code can also use a UiDevice instance to perform device-level operations, such as force the device to portrait, press the d-pad hardware button, or press the master screen key and menu key.
Obtain the UiDevice instance and run the following code to simulate pressing the master screen key: getUiDevice (). pressHome ();

UiSelector
Represents a search criteria to query and get a handle on specific elements in the currently displayed UI. if more than one matching element is found, the first matching element in the layout hierarchy is returned as the target UiObject. when constructing a UiSelector, you can chain together multiple properties to refine your search. if no matching UI element is found, a UiAutomatorObjectNotFoundException is thrown. you can use the childSelector () method to nest multiple UiSelector instances. for example, the following code example shows how to specify a search to find the first ListView in the currently displayed UI, then search within that ListView to find a UI element with the text property Apps.
UiSelector represents a search standard. You can query and obtain the handle of a specific element on the current display interface. If more than one matching element is found, the First Matching Element in the layout hierarchy is returned as the target UiObject. When constructing a UiSelector object, you can use the chain to call multiple attributes to narrow the query range. If no matching element exists, the system returns the error UiAutomatorObjectNotFoundException. You can also use the childSelector () method to nest multiple Uiselector instances. For example. The following code demonstrates how to develop a query to locate the first ListView on the current interface, and then locate an interface element with the Apps text attribute in the returned ListView.

UiObject  appItem  =   new   UiObject ( new   UiSelector () . className ( “android.widget.ListView” ). instance ( 1 ) . childSelector ( new   UiSelector (). text ( “Apps” )));
UiObject
Represents a UI element. To create a UiObject instance, use a UiSelector that describes how to search for, or select, the UI element.
The following code example shows how to construct UiObject instances that represent a Cancel button and a OKbutton in your application.
UiObject represents a UI element. To create a UiObject instance, use the UiSelector instance that describes how to search and select UI elements:
UiObject  cancelButton  =   new   UiObject ( new   UiSelector (). text ( “Cancel” )); UiObject  okButton  =   new   UiObject ( new   UiSelector (). text ( “OK” ));

You can reuse the UiObject instances that you have created in other parts of your app testing, as needed. note that the uiautomator test framework searches the current display for a match every time your test uses a UiObject instance to click on a UI element or query a property.
In the following code example, the uiautomator test framework searches for a UI element with the text property OK. if a match is found and if the element is enabled, the framework simulates a user click action on the element. you can also restrict the search to find only elements of a specific class. for example, to find matches of the Button class:
If necessary, you can reuse the UiObject instances created in the test project. Note: every time a test case uses a UiObject instance to click the UI element or query attributes, the uiautomator test framework searches the current interface for matching. In the following code, the uiautomator test framework searches for UI elements with OK text attributes. If a match is found and the element is enabled, the Framework simulates the click operation on the element.

if ( okButton . exists ()   &&  okButton . isEnabled ()) {    okButton . click ();}

You can also restrict the search to search for elements in several specific classes. For example, to find the matching of the Button class:

UiObject  cancelButton  =   new   UiObject ( new   UiSelector (). text ( “Cancel” ) .className ( “android.widget.Button” )); UiObject  okButton  =   new   UiObject ( new   UiSelector (). text ( “OK” ) .className ( “android.widget.Button” ));


3. For details, note that the android. jar and uiautomator. jar packages must be referenced in the class library.

Package xzy. test. uiautomator; import java. io. IOException; import android. OS. remoteException; import com. android. uiautomator. core. uiDevice; import com. android. uiautomator. core. uiObject; import com. android. uiautomator. core. uiObjectNotFoundException; import com. android. uiautomator. core. uiSelector; import com. android. uiautomator. testrunner. uiAutomatorTestCase; public class CalTest extends UiAutomatorTestCase {Public void testDemo () throws UiObjectNotFoundException, RemoteException {UiDevice device = getUiDevice (); // wake up the screen device. wakeUp (); assertTrue ("screenOn: can't wakeup", device. isScreenOn (); // return to HOMEdevice. pressHome (); sleep (1000); // start the calculator Apptry restart runtime.getruntime(cmd.exe c ("am start-n com. android. calculator2 /. calculator ");} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} Sleep (1000); UiObject oneButton = new UiObject (new UiSelector (). text ("1"); assertTrue ("oneButton not found", oneButton. exists (); UiObject plusButton = new UiObject (new UiSelector (). text ("+"); assertTrue ("plusButton not found", plusButton. exists (); sleep (100); UiObject upload button = new UiObject (new UiSelector (). text ("="); assertTrue ("response button not found", response button. exists (); oneButton. click (); sl Eep (100); plusButton. click (); sleep (100); oneButton. click (); button. click (); sleep (100); UiObject switcher = new UiObject (new UiSelector (). resourceId ("com. android. calculator2: id/display "); UiObject result = switcher. getChild (new UiSelector (). index (0); System. out. print ("text is:" + result. getText (); assertTrue ("result! = 2 ", result. getText (). equals (" 2 "));}}


4. Summary

Unit Testing is relatively simple but effective. For teams that want to perform automated testing and deliver stable and quality, unit testing is very important. Android already has a complete set of unit testing support, and UI testing is also OK.


5. We will introduce some Android UI Test & Android Code Coverage Test using the robotium framework later.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.