Android Automation Test (4) <uiautomator> Unit test

Source: Internet
Author: User

In the previous series, I introduced Java for Android Automation test (1) How to install and uninstall an application (Java),Android Automation Test (2) Find objects by ID (Java),and then introduce the Python language for Android Automated Testing (3) Finds object &touch&type (python) by ID. Also said that follow-up to write about unit tests and code coverage test articles. The unit test is to be introduced today.

1. Overview

As a classmate of Java unit testing, using Android unit tests is simple, see how to do Android unit testing, in this way, the business logic of testing in many cases, has been resolved. Another obvious flaw is that the test interface is inconvenient. For Android applications, the interface occupies an important part.

You can use the Uiautomator.jar class library at this time. Here I do not elaborate on the specific Android Uiautomator class library how to use. The specific use can be found in the Android UI testing (English version), and the Android Uiautomator using the official introductory tutorial (Chinese version).

2. Core class

I would like to mention the most important of the core classes Uidevice,uiselector and UiObject , how to find objects 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 Curre NT orientation or display size. Your tests also can use the Uidevice instance-perform device level actions, such as forcing the device into a specifi C rotation, pressing the D-PAD hardware button, or pressing the Home and Menu buttons.
Uidevice represents the device status. When testing, you can call the methods of the Uidevice instance to check the status of different properties, such as the current screen rotation direction of the cargo display size. The test code can also use Uidevice instances to perform device-level operations, such as forcing the device to the screen, pressing the D-PAD hardware button, or pressing the home key and menu key.
To get the Uidevice instance, the code that simulates pressing the main screen key is as follows: 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 was found, the first matching element in the layout hierarchy is returned as the target U Iobject. 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 Displa Yed UI, then search within, this ListView to find a UI element with the Text property Apps.
Uiselector represents a search criterion that allows you to query and get a handle to a particular element on the current display interface. If more than one matching element is found, the first matching element on the layout hierarchy is returned as the target UIObject. When constructing a Uiselector object, you can use chaining to invoke multiple properties to narrow the query. If there is no matching element, the exception uiautomatorobjectnotfoundexception is returned. You can also use the Childselector () method to nest multiple Uiselector instances. For example. The following code shows how to make a query to locate the first ListView in the current interface, and then locate an interface element with the apps Text property within 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 this describes how to search for, or select, the UI element.
The following code example shows how to construct UiObject instances this represent a Cancel button and a okbutton in yo UR application.
UIObject represents a UI element. To create an UIObject instance, use a Uiselector instance that describes how to search for, 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 so you had created in other parts of your app testing, as Neede D. Note the Uiautomator test framework searches the current display for a match every time your test uses a  uio Bject instance to click in 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 was enabled, the framework simulates a user click action on the element. You can also restrict the search for find only elements of a specific class. For example, to find matches of the  button class:
      If necessary, you can reuse UIObject instances that have already been created in the test project. Note that each time a test case uses a UIObject instance to click on a UI element or query property, the Uiautomator test framework searches the current interface for a match. In the following code, the Uiautomator test framework searches for UI elements with an OK Text property. If a match is found and the element is enabled, the framework simulates the The user's click action on the element.  

if (OKButton. Exists ()   &&  OKButton. IsEnabled ()) {    okbutton. Click ();}

You can also limit the search to find elements in several specific classes, for example, to find a match for 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, detailed example, note that the class library needs to refer to Android.jar and Uiautomator.jar package AH

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 screen device.wakeup (); Asserttrue ("Screenon:can ' t Wakeup", Device.isscreenon ());//Return to Homedevice.presshome (); sleep (1000);//Start calculator Apptry { Runtime.getruntime (). EXEC ("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 Equalbutton = new UiObject (new Uiselector (). Text ("="); Asserttrue ("Equalbutton not Found", equalbutton.exists ()); Onebutton.click (); Sleep (100);p lusbutton.click (); Sleep (n); Onebutton.click (); Equalbutton.click ); 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. Summarize

Unit testing is simple, but effective, and unit testing is important for teams that want to do automated testing and to deliver consistent and quality delivery. Android has a very well-developed unit test support, and UI testing is OK


5, the following will introduce some use Robotium framework for Android Unit Test & android Code Coverage test

Android Automation Test (4) <uiautomator> Unit test

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.