Original address: http://blog.sina.com.cn/s/blog_ae2575ff01018b2o.html
Uiautomator api:http://android.toolib.net/tools/help/uiautomator/index.html
Starting with Android SDK API 16, the Android SDK starts supporting two new tools for functional UI testing.
Uiautomatorviewer, a tool for scanning and analyzing UI widgets for Android applications.
And Uiautomator, a Java library that provides APIs for customizing UI testing.
To apply the above two tools, in addition to the Android SDK API 16 above the prerequisites, but also requires Android SDK tools 21 or more.
Uiautomator mainly involves a few classes, Most are located under the Com.android.uiautomator.core of the source package, where the bold part is the main contact class, familiar with the role of these 5 classes, you can generally smoothly write uiautomator test cases.
Uiautomatortestcase
Uidevice
Uiselector
Uiscrollable
UiObject
Uicollection
Uitestautomationbridge, Uiautomatorbridge
Interactioncontroller, Querycontroller
Uiwatcher
Uiautomatortestcase
APP Test TestCase (Junit), uiautomatortestcase
Each test case needs to inherit uiautomatortestcase to achieve the Setup,teardown equivalent of the test environment. Uiautomatortestcase, by inheriting the TestCase class in Junit3, is in the setup (), TearDown (), and Getparams () functions. The main is to use bundles to achieve the communication between Android activity. In Uiautomatortestcase, Getuidevice () and other functions on Uidevice are added to enable Uidevice () to be called anywhere in the test.
Uidevice
This class mainly contains the acquisition of device state information, and analog users as to the operation of the device two types of APIs.
can be obtained through GETDISPLAYSIZEDP (), Getdisplaywidth (), Getdisplayheight (), Getproductname (), Getcurrentactivityname (), Getcurrentpackagename () To obtain information about the device.
Pressmenu (), Pressback (), Presshome (), Presssearch (), Pressdpadcenter (), Pressdpadright (), Pressdpadleft (), Pressdpadup (), Pressdpaddown (), Pressdelete (), Pressenter (), Presskeycode (), Pressrecentapps (), click (), swipe (), Getdisplayrotation () setorientationleft () ... wakeUp (), Sleep (), Dumpwindowhierarchy (), Waitforwindowupdate () and other APIs to manipulate the device flexibly.
Takescreenshot () allows the device to be intercepted at any time.
Uiselector
Mainly through a certain query way, to navigate to the UI elements to manipulate.
General UI elements can be positioned using the following API: Text (), textmatches (String regex), Textstartswith (), Textcontains (), ClassName (), classnamematches (string regex), ClassName (Class type), Description (), descriptionmatches (string regex), Descriptionstartswith (), Descriptioncontains (), PackageName (), packagenamematches (String regex).
It is worth noting that index () and instance () two functions, where Index () is the ID number of the current page, and instance () is the first element of the set of child elements that is obtained under certain search results. Such as:
New Uiselector (). ClassName ("Android.widget.ImageView"). Enabled (TRUE). instance (2);
also enabled (), focused (), focusable (), scrollable (), selected (), checked (), clickable (), longclickable (), Childselector ( ) and other search conditions, as the name implies.
UiObject
UIObject can represent any element of a page, and its various property positioning is usually done through uiselector.
A more commonly used API, such as Clickandwaitfornewwindow (), means clicking on the element and waiting for the new window to appear. This process is supported by the Android UI testing framework and does not require additional control wait times.
UIObject allows you to click on a specific part of the element, API such as Clicktopleft (), Longclickbottomright (),...
by GetText (), Getcontentdescription (), Getvisiblebounds (),... API to get the relevant properties of UIObject, Getpackagename () can be used to determine whether the target test is open for the app.
SetText (), Cleartextfield () can be used to set and empty the associated input box.
Waitforexists () can be used to manipulate related waits or validations.
uicollection
Uicollection is generally associated with Uiselector, as its constructor also requires uiselector:uicollection (Uiselector selector).
It has fewer APIs and is used primarily to pick out the elements from the uiselector filtered elements: Getchildbydescription (), Getchildbyinstance (), Getchildbytext (), and the number of statistical element sets Getchildcount ()
uiscrollable
UiObject, Uicollection->uiscrollable
The uiscrollable is used to represent an interface element that can be slid, as shown in its inheritance relationship.
In its API, Setasverticallist (), Setashorizontallist () sets the UI element list to be based on horizontal scrolling or portrait scrolling. It can then be used Getmaxsearchswipes (), Flingforward (), Flingbackward (), Scrollforward (), Scrollbackward (), Scrolltoend (), Scrolltobeginning () function controls the slide, as well as getchildbydescription (), Getchildbyinstance (), Getchildbytext (), scrollIntoView (), Scrolltextintoview (),... To select whether you have converted to a page that has a target element. Such as:
Uiscrollable appviews = new Uiscrollable (new Uiselector (). Scrollable (true));
Appviews.setashorizontallist ();
UiObject Helperapp;
Helperapp = Appviews.getchildbytext (New Uiselector ()
. ClassName (Android.widget.TextView.class.getName ()), "91 assistants"); If the current page does not have 91 assistant apps, the test will automatically swipe the page until the 91 helper app appears.
The following are the important classes that make up the UI testing framework:
Uitestautomationbridge
This is the basis of the entire testing framework, which is responsible for connecting the system, recording the latest linked events (accessibilityevent), window content query API, and so on. Can be called by the Android app, or Java programs are called from the shell.
Here are two concepts to note:
1.accessibilityevent: All UI elements can be manipulated, because these are accessibilityevent. For how the page elements can be manipulated, so that the related events are accessibilityevent, see the Uiautomator entry-"Confirmation program can be tested" section.
2.accessibilitynodeinfo: The Component tree node in the window, that is, the nodes displayed in Uiautomtorviewer.
Connect () in the API, disconnect () is responsible for establishing the actual connection to the device.
Executecommandandwaitforaccessibilityevent () performaccessibilityaction () Findaccessibilitynodeinfosbytext (), Findaccessibilitynodeinfobyviewidinactivewindow () is one of the most important APIs.
Uiautomatorbridge
Uiautomatorbridge is a subclass of Uitestautomationbridge, with the main difference being the invocation of both Interactioncontroller and Querycontroller objects in the constructor. And some constant definitions, and so on. In addition to the above differences, Uiautomatorbridge also defines executecommandandwaitforaccessibilityevent (), Onaccessibilityevent (), WaitForIdle (), Functions such as Addaccessibilityeventlistener ().
Interactioncontroller
Introduction Interactioncontroller, need to mention Interactionprovider, it is responsible for injecting user events (such as click, input, etc.), and response to the corresponding coordinates of the event.
Interactioncontroller defines almost all the basic operations of the cell phone, such as runandwaitforevents (), Clickandwaitforevents (), click (), Longtap (), Scrollswipe (), Swipe (), Clickandwaitfornewwindow (), TouchUp (), TouchDown (), TouchMove (), Isnaturalrotation (), Setrotationright (), Setrotationleft (), Freezerotation (), Wakedevice (), Sleepdevice (), etc.
Querycontroller
Querycontroller is responsible for turning Uiselector's search information into Accessibilitynodeinfo.
The specific API is as follows: Findnodepatternrecursive (), Translatepatternselector (), Translatereqularselector (), Translatecompoundselector (), Getrootnode (), Findaccessibilitynodeinfo ().
Uiwatcher
Uiwatcher is called only when Uiselector cannot find a match, and is intended to retry, wait for page updates (such as a popup dialog), and so on. There is only one main function: Checkforcondition ().
Its related functions are in uidevice, such as: Uidevice.registerwatcher (), Uidevice. Resetwatchertriggers (), Uidevice.runwatchers (), Uidevice.removewatcher ()
A brief analysis of the "turn" Uiautomator API