1 Introduction to uiautomator2
uiautomator2 is an Android automated testing framework that provides features such as clicking, long pressing, text input, sliding, dragging, and screenshots, which can simulate various actions of users. The user can locate the control through attributes such as the id or text of the control
2 Prepare the environment
python environment variable configuration
After installing python(3.7 for example), add [D:\Program Files\Python 3.7
] and [D:\Program Files\Python 3.7\Scripts
] to the system path env var
pycharm encoding settings
After installing pycharm, you need to set the encoding format UTF-8:
From
File --> Settings --> Editor --> File Encodings
Set Global Encodig / Project Encoding / Default encoding for properties files to UTF-8
Install uiautomator2
Open pycharm and run the following code.
pip install --pre -U uiautomator2
pip install --pre weditor
python -m uiautomator2 init #install atx-agent to device
Code language: CSS (css)
Notice:[python -m uiautomator2 init]
requires a USB cable to connect the device), the port 5037 may be occupied by other processes. execute the following command:
adb kill-server adb start-server adb remount python -m uiautomator2 init
3 Information viewer
The user can control the button by clicking the coordinates of the button, but the control of the text box cannot be obtained only by the coordinates. Fortunately, the controls on the device interface generally have attributes such as resourceId, text, description, className, etc. Through these attributes, the user can easily locate the specified control, thereby realizing the control of the control.
weditor
Open weditor by executing the [python -m weditor]
:

Note: you need to open the [ATX] software on the device, click [Start UIAUTOMATOR], make sure that the device and the PC are connected to the same network, when the icon next to [Connect] turns green, the connection is successful, the user can click Dump HierarchySync device
uiautomatorviewer
uiautomatorviewer is a page information viewing tool provided in the Android SDK, located at [D:\Program\sdk\tools\uiautomatorviewer.bat]
, by click uiautomatorviewer.bat you can start it

Notice
- update to the latest platform-tools and tools using Android Studio
- Configure system environment variables: ANDROID_SWT –
D:\Program\sdk\tools\lib\x86_64
, and set [D:\Program\sdk\tools
] and [D:\ Program\sdk\platform-tools
] is added to the path of the system environment variable. - must exit [ATX] from device
4.uiautomator2 common API
When applying the uiautomator2 framework, you need to import the package, as follows:
import uiautomator2 as u2
Code language: JavaScript (javascript)
Device connection
Device connection is mainly divided into wired connection and wireless connection, as follows:
wired connection
d = u2.connect_usb(id) # id is the device id from adb device
Code language: PHP (php)
wireless connection
d = u2.connect(ip)
Returned d is the connection handle
Application Control
Install the app
d.app_install('http://domain.com/xxx.apk')
Code language: JavaScript (javascript)
Note: can only be installed from a URL
Open the app
d.app_start("app_package_name")
Code language: JavaScript (javascript)
Close the app
d.app_stop("app_package_name")
Code language: JavaScript (javascript)
Close all apps
d.app_stop_all()
#Clear all apps by clicking the recent button
d.press("recent")
sleep(1)
d(resourceId="com.huawei.android.launcher:id/clear_all_recents_image_button").click()
Code language: PHP (php)
Element positioning
Since page elements (controls) generally have attributes such as resourceId, text, description, className, etc., specific controls can be located through these attributes.
resourceId: control id
text: the text displayed on the control
description: description of the control
className: the class to which the control belongs
element = d(resourceId=value) element = d(text=value) element = d(description=value) element = d(className=value)
Description: d is the connection handle and value is the attribute value corresponding to the control ID type.
element is a list, because it may return multiple controls with the same attribute value, you can then pass element[index]
to control the specified control. When the attribute value is unique, index after element can be omitted
With the following command, you can determine whether the element corresponding to the attribute value exists.
element.exists
element[index].exists
Code language: CSS (css)
Element Control
Click the control
element[index].click()
element.click()
d.click(x, y)
Code language: CSS (css)
Double click the control
element[index].double_click()
element.double_click()
d.double_click(x, y)
Code language: CSS (css)
Long press the control
element[index].long_click() element.long_click() d.long_click(x, y, duration=0.5)
Text manipulation
- Enter text:
element[index].set_text(text)
element.set_text(text)
Code language: CSS (css)
Also, when there is only one edit box in the page, the following code can be used:
d.set_fastinput_ime(True)
d.send_keys(text)
d.set_fastinput_ime(False)
Code language: CSS (css)
- Clear text:
element[index].clear_text()
element.clear_text()
Code language: CSS (css)
- Get text:
element[index].get_text()
element.get_text()
Code language: CSS (css)
Gesture Control
Swipe
d.swipe(sx, sy, ex, ey, duration=0.5)
Drag and drop
d.drag(sx, sy, ex, ey, duration=0.5)
System Control
Button
key = {"back", "home", "recent", "power", "volume_up", "volume_down", "volume_mute", "enter",...}
d.press(key)
Code language: JavaScript (javascript)
Rotate
#Get screen orientation:
orientation = d.orientation
#Lock screen orientation:
d.freeze_rotation()
d.freeze_rotation(True)
#set screen orientation:
d.set_orientation("left")
d.set_orientation("right")
d.set_orientation("natural")
Code language: PHP (php)
Note: left, right, natural can be abbreviated as l, r, n respectively
Take a screenshot
image = d.screenshot()
image.save("path.jpg")
d.screenshot("path.jpg")
Code language: JavaScript (javascript)
push file to device
d.push(pc_file_path, phone_dir_path)
Code language: CSS (css)
pull
d.push(pc_file_path, phone_dir_path)
Code language: CSS (css)