当前位置:网站首页>APP automation testing with Uiautomator2

APP automation testing with Uiautomator2

2022-08-10 15:06:00 zljun8210

1. Install Uiautomator2

My environment is Python, which can be installed with the pip command:

pip install uiautomator2

Note: The source code of Uiautomator2: GitHub - openatx/uiautomator2: Android Uiautomator2Python Wrapper

2. Configure the device

First the device is connected to the PC and can be discovered by adb devices.Execute the following command to automatically install the device-side programs required by this library: uiautomator-server, atx-agent

# init is to install uiautomator2 on all mobile phones connected to the computer via USBpython -m uiautomator2 init# Specify the phone to install uiautomator2, use --mirrorpython -m uiautomator2 init --mirror --serial $SERIAL# If you dislike being slow, you can use a domestic mirrorpython -m uiautomator2 init --mirror

Note: It is said that the latest version of uiautomator2 does not require manual configuration. When the code runs uiautomator2.connect(), the device side will be configured first.

3. Before writing code: identify elements

There is monitor.bat in the Andorid SDK. Running it will start uiautomatorviewer, which is a tool that comes with the SDK.

You can also use third-party tools, here we recommend weditor, installed as follows:

pip install editor

Start the Editor:

python -m editor
The browser will automatically launch a web page http://atx.open.netease.com,See the following interface

Set the device seen by adb device to connect.

4. Officially write code

import unittestimport uiautomator2 as u2import timeclass AppTest(unittest.TestCase):def EditWO(self):d = u2.connect()d.app_start('com.xxxxxx.xxxxxx')time.sleep(5)d(text='Work Order').click() # Click on the Work order menutime.sleep(3)d(text='Completed').click() # Click Completedtime.sleep(2)d(text='Open').click()time.sleep(2)d(text='300').click()time.sleep(1)d(text='Save').click()d.press('back')def Mapview(self):d = u2.connect()d.app_start('com.xxxxxx.xxxxxx')time.sleep(5)d.xpath('//android.widget.RelativeLayout/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]').set_text('char')d.press('enter')d(text='CHARIOTOFFIRE').click()d(text='Cancel').click()d.xpath('//android.widget.RelativeLayout/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[5]/android.widget.ImageView[1]').click()d(text='+ ADD LOCATIONS').click()d.xpath('//androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.Button[3]').click()d.click(0.503, 0.498)d.xpath('//androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.Button[3]').click()d.click(0.081, 0.801)d.xpath('//androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.widget.Button[3]').click()d.click(0.914, 0.273)d(text='GET DIRECTIONS').click()d.press("back")def test_something(self):self.EditWO()if __name__ == '__main__':unittest.main()
原网站

版权声明
本文为[zljun8210]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101428002688.html