uiautomator2 use

initialization

Deployment-related daemon.

Phone or a computer connected to the plurality of mobile phones, has been added to ensure adb environment variable, the following command will automatically install the library apparatus side program required: uiautomator-server, atx-agent, openstf / minicap, openstf / minitouch

python -m uiautomator2 init

The installation is complete, the device will be one more uiautomator application (ATX). Open the page as follows:

 test

Write the following script to verify:

python 
Import uiautomator2 AS U2 
d = u2.connect ('10 .59.8.231 ') // Check the phone connection to the IP address of the 
print (d.info) // Print phone message 
exit () // exit python

operation result:

Positioning elements:

We can help uiautomatorviewer Android SDK from the view element, which requires the phone to be connected to the PC's USB mode, used in front of me is a WIFI connection for connection. So, openatx weditor provides another tool to solve this problem.

GitHub Address: https://github.com/openatx/weditor

1, the installation:

pip install --pre --upgrade weditor

Successful installation as follows:

2, use:

python -m weditor

 The default page will automatically open a browser: HTTP: // localhost: 17310 /

Connection:

1, select Android, input device IP (10.59.8.231) in the upper left corner of the page, click the Connect button.

2. Select Android in the upper left corner page, enter the serial number (adb devices obtain the serial number), and click the Connect button.

When we finished operating the phone, you can click "Dump Hierarchy" button to refresh, to keep the interface on the device to keep pace. weditor can also help us generate code. To be studied ~

Summary: There will be no basic pit, according to the above process will be ok ~

 

Write test scripts

Here come directly from here take to be a careful study behind

U2 uiautomator2 AS Import 
from Import Time SLEEP 

D = u2.connect ( '192.168.31.234') 

# start the App 
d.app_start ( "com.meizu.mzbbs") 

# Search 
d (resourceId = "com.meizu.mzbbs: id / J0 ") the Click (). 

# enter keyword 
com.meizu.mzbbs d (resourceId =": the above mentioned id / P9. ") set_text (" Flyme ") 

# Search button 
d (resourceId =" com.meizu.mzbbs: id / TP "). the Click () 

SLEEP (2) 

# stop App 
d.app_stop (" com.meizu.mzbbs ")

app_start () and app_stop () is used to start and stop the application.

Commonly used targeting:

  • ResourceId positioning:
    • d(resourceId="com.meizu.mzbbs:id/tp").click()
  • Text positioning:
    • d (text = "selection") .click ()
  • Description Positioning:
    • d(description="..").click()
  • ClassName positioning:
    • d(className="android.widget.TextView").click()

Guess you like

Origin www.cnblogs.com/helloTerry1987/p/11440819.html