uiautomator2, an app automation testing framework that is better than appium

When it comes to app automation testing, the first reaction will think of the appium framework, which is the same as selenium on the web, but for appium, setting up an environment is very important. Many people are discouraged by the environment and abandoned appium.
The framework of uiautomator2 mentioned below, the environment is much simpler than appium, it can be worth learning

1. Introduction and principle analysis of uiautomator2

uiautomator2 is an open source tool for automated testing that only supports native application testing on the Android platform. It was originally a Java library for automated testing provided by Google. Later, python-uiautomator2 was developed, which encapsulated Google’s own uiautomator testing framework and provided a convenient python interface. It can be used to easily write python scripts to automate the app. Test
principle analysis:
Python side: run the script and send HTTP request to the
mobile side. Mobile side: install atx-agent, then atx-agent starts the uiautomator2 service to listen, recognize the python script, and convert it to uiautomator2 code.
The mobile device receives the HTTP request from the PC via WIFI (same network segment) or USB, and executes the specified operation

2. Installation

1. Like appium, first install adb,
download androidsdk, and configure environment variables

2. Installing uiautomator2 is
not easy to install, it is recommended to use Tsinghua source or Douban source to install

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --pre -U
uiautomator2

3. Device initialization: First, the device is connected to the PC, and the device can be found by adb devices.
I am using the night god simulator here
Insert picture description here

4. Install atx-agent on the device and
execute python -m uiautomator2 init, and the
night god simulator will be automatically installed. The real phone may need to click to agree to the installation

3. Verify that the device is successfully connected

First see if the device is connected to the
guide package and rename import uiautomator2 as ut The
connected device is ut.connect('127.0.0.1:62001')

import uiautomator2 as ut
d=ut.connect('127.0.0.1:62001')
print(d.device_info)

Print out all the information of this device,
Insert picture description here
which shows that the connection is successful.
Next, you need to locate the elements. Here is a useful positioning tool: weditor, which can not only locate in real time, but also write debugging code in it.

4. Install weditor

Executing pip install --pre weditor
may time out, here is recommended to install Douban source, pro test is available

pip install -i https://pypi.douban.com/simple weditor

Successful installation
Insert picture description here
execution

python -m weditor

A webpage will be opened in the browser.
Insert picture description here
This is what happens after opening
Insert picture description here
. Run the app on the simulator, click Dump Hierarchy, and you can display the screen of the phone in the browser. Open real-time, you can see in real-time

5. Open the app and write code

The command for uiautomator2 to view the currently running app is uiautomator2 current. For
example, when I open Toutiao in the simulator and execute uiautomator2 current in cmd, I
will see the package name and the current class name.
Insert picture description here
uiautomator2 is easier to open the app than appium, just use the app_start method , Now to write code in pycharm

d=ut.connect('127.0.0.1:62001')
print(d.device_info)
d.app_start('com.ss.android.article.lite','com.ss.android.article.lite.activity.SplashActivity') #打开app

I want to click to the login page, find the element location in weditor, and then use d(), which is simpler than find_element_by_xpath in appium.
Below is the code I wrote

import uiautomator2 as ut
import time
d=ut.connect('127.0.0.1:62001')

print(d.device_info)
d.app_start('com.ss.android.article.lite','com.ss.android.article.lite.activity.SplashActivity') #打开app
d.implicitly_wait(10)
d(text='热榜').click()
d(text='未登录').click()
d(text="登录").click()
d(text="手机号").send_keys('13409012321')
d(text='请输入验证码').send_keys('123456')
d(text='进入头条').click()

There are more fun and useful methods in uiautomator2, learn slowly, and you will fall in love with this framework

Guess you like

Origin blog.csdn.net/shenshenruoxi/article/details/108632507