Introduction
UI Automator contains 3 main classes: UISelector class, Ui Object class, UiDevice class
- UISelector class: element positioning, call directly after the combination, chained call
- UiObject class: group object operations, such as click, input
- UiDevice class: on the device Operations, such as getting device information, pressing, sliding, dragging
Usage of UISelector class
- resource-id attribute positioning –>> full matching, regular matching
new UiSelector().resourceId("id attribute value") full matching
new UiSelector().resourceIdMatches("id attribute value") regular matching
Code language: JavaScript (javascript)
- text attribute positioning –>> full match, regular, contain, start match
new UiSelector().text("text") full match
new UiSelector().textContains("text") include
new UiSelector().textMatches("text ”) regular matching
new UiSelector().textStartsWith(“text”) matching at the beginning
Code language: PHP (php)
- content-desc attribute positioning –>> full match, regular, contain, start match
new UiSelector().description("text") full match
new UiSelector().descriptionContains("text") include
new UiSelector().descriptionMatches( "text") regular matching
new UiSelector().descriptionStartsWith("text") matching at the beginning
Code language: PHP (php)
- Automatically slide to find the element and click
'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("{}").instance(0));'.format (text).click()
Code language: JavaScript (javascript)
- Get instand by resource id
new UiSelector().resourceId("com.yang.designsupportdemo:id/CollapsingToolbarLayout");
Code language: JavaScript (javascript)
- Get instand by description
new UiSelector().description("Navigate up")
Code language: JavaScript (javascript)
- Get className by description
new UiSelector().className("android.support.v7.widget.RecyclerView")
Code language: CSS (css)
UiDevice:
The device object is obtained through the getInstance(instrumentation)
method of UiDevice
.
Various properties of the device can be detected through the UiDevice
instance, such as obtaining the orientation and size of the screen, and device-level operations can also be performed through the UiDevice
instance
- Clicking the Home button
uiDevice.pressHome();
Code language: CSS (css)
- Clicking the return key
uiDevice.pressBack();
Code language: CSS (css)
UiObject
Represents a UI control, which is obtained through the findObject(UiSelector)
method of uiDevice
. After obtaining the UiObject
instance, you can perform related operations on the UI control, such as clicking, long pressing, etc.:
- Click return button
UiObject back = uiDevice.findObject(new UiSelector().description("Navigate up"));
back.click();
Code language: JavaScript (javascript)
UiCollection
Represents a collection of UI controls, equivalent to ViewGroup
. For example, when there are multiple CheckBoxes in the interface, you can get all CheckBoxes under the current interface through the class name
, and then get the specified CheckBox object through the control id:
- Get specified Checkbox instance
UiCollection uiCollection = new UiCollection(new UiSelector().className("Class Name"));
UiObject checkBox = uiCollection.getChild(new UiSelector().resourceId(""));
Code language: JavaScript (javascript)
UiScrollable
Represents scrollable controls, such as opening the About Phone option in Settings:
- Scroll to end then click About phone options
UiScrollable settings = new UiScrollable(new UiSelector().className("android.support.v7.widget.RecyclerView"));
UiObject about = settings.getChildByText(new UiSelector().className("android.widget.LinearLayout"), "About phone");
about.click();
Code language: JavaScript (javascript)