1 Introduction to U2 automatic fetching tool

1.1 What is Uiautomator

Uiautomator is a Java library provided by Google for android automation testing, based on the Accessibility service

AccessibilityService (AccessibilityService) is a Service provided by the Android system, which itself descends from the Service class. This Service provides an enhanced user interface designed to help people with disabilities or temporarily unable to fully interact with their devices

We can use the AccessibilityService, can realize the page monitoring and simulation click control and other automatic operations

1.2 disadvantages

1 Test scripts can only use the Java language

2 Test scripts can run only after being packaged as JAR or APK packages and uploaded to the device.

1.3 The birth of Uiautomator2

You want to be able to write code logic in Python

You want to be able to control your phone while it’s running on your computer

You run an HTTP RPC service on your phone, inherit functions from UIAutomator, and then wrap those HTTP interfaces into Python libraries

1.4 Advantages of Uiaitomator2

1 Convenient environment construction

2 UI space identification with professional tools (Weditor), good visualization

3 UI automatic programming using Python, low learning cost

4 UI automatic scripts run stably

5 The document is in Chinese

1.5 Uiautomator2 project address

Github.com/openatx/uia…

1.6 Environments supported by Uiautomator2

1 Android Version 4.4+

2 Python 3.6+ (3.8.0 not supported, 3.8.2 ok)

1.7 Development Environment

Development environment: Windows10

Operating environment: Ubuntu 18.04

Vm: VMware® Workstation 15 Pro

SSH:SecureCrt Version 7.0.0

IDE: PyCharm 2019.3.2 (Profession Edition)

Python version: Python3.6.9

1.8 Development Environment Precautions

Do not use the VirtualBox virtual machine

2 Introduction to U2 communication process

2.1 Communication Process

At project initialization, the PC sends files to the mobile device over Wifi or USB or ADB Wifi

These files will contain UIAutomator-Server, ATX-Agent,Minicap, and Minitouch

At this time, atX-Agent will start the UIAutomator service on the mobile device. The default port number is 7912

Run the script and send an HTTP request to the system device

An HTTP service that runs and encapsulates UIAutomator2 on a mobile device parses the received request and translates it into UIAutomator2 code

2.2 After starting the project

Procedure 1 Install the ATX-Agent (daemon process) on the mobile device, and then the ATX-Agent starts the UIAutomator service (default port 7912) to listen

2 Write and execute the test script on PC (equivalent to sending HTTP request to the server of mobile device)

3 The mobile device receives the HTTP request from the PC over wifi or USB and performs the specified operation

2.3 Request Process

import uiautomator2 as u2
IP is the address of the phone in the LAN
d = u2.conntec_wifi("192.168.18.200")
Enable debug mode
d.debug = True
View simple device information
print(d.info)

"" "the execution result curl -x post - d '{" jsonrpc ":" 2.0 ", "id" : "dasfasfessddfsf}"... 'curl' is a command used in Linux to send a request. The request method is posed. The request parameter is a JSON parameter whose ID is the md5 value of the request method and current timestamp.
Copy the code

2.4 How Can I View the Port Number on the Device

The adb shell ps | grep atx # to check the process busybox netstat pt | grep 7956 # can see the port numberCopy the code

3 u2 automatic grab development environment construction

Download pan.baidu.com/s/10j7J6-6g…

Extraction code: I0W5

1 vmware Workstation

Open WMware Workstation -> File -> Open -> Find Ubuntu 64-bit. Ovf, ok -> Browse -> Create a folder D: process vm Ubuntu -> Import

If the import process fails, click no longer to display this message, because there may be Chinese characters in the directory -> click Retry

If the wifi network connection in Ubuntu fails, open it on the local computer – Control panel \ Network and Internet\ Network Connection

Right click on Properties –> Share, check on both, and select Home Network Connection

If you still can’t connect to the Internet, check out this blog post

Blog.csdn.net/b_bigstar/a…

2 secureCRT

It is good to install

The rest is the same as xshell

3 Installing Python in Ubuntu

Install a Python environment first, 3.6.9
pip3 install virtualenv -i https://pypi.doubanio.com/simple
    
# in a directory
python3 -m virtualenv venv

# View third-party packages for the Python environment
pip3 freeze

# Make the environment work
source venv/bin/activate

If virtualEnv fails to install, uninstall it, and
pip3 install virtualenv==20.023. -i https://pypi.douban.com/simple

Copy the code

4 PyCharm and Ubuntu get through

Coded in PyCharm, using the Ubuntu environment, the code is automatically synchronized to the Ubuntu environment

steps

File-->Settings -->Project: Project name -->Python Interpreter -->Python Interpreter right-most pinionClick Add -->SSH Interpreter-->Existing Server Environment --> Click the three dots to the far rightCopy the code

And then I’m gonna hit Next

And then click Finish

After doing this, any coding you do locally will be synchronized to the LINu system, so you can use the Remote Python environment, and your code will be synchronized to the Linux environment

5 adb

Android Debug Bridge (ADB)

"Adb" is used to run the ADB command adb server from a shell or script: ADB Server is a daemon adBD running on a PC: the program "ADBD" runs as a daemon on an Android device or emulatorCopy the code

What can ADB be used for

Installing and uninstalling APK Mobile device Copying files between PCS Viewing application information installed on the device File management key operationCopy the code

Common ADB applications

Various brush tools various mobile phone root toolsCopy the code

Windows installation adb

First download the Simulator on your computer, find the installation path: D:\ Process \Nox\bin, and add the environment variables

Click on the Nocturne opener and click on the Settings of each emulator to change the startup Settings to mobile

Not recommended mobile phone brands

Huawei series, honor seriesCopy the code

Recommended phone brand

Xiaomi, Oppo and so onCopy the code

For the phone’s Settings, open Settings, tap the version number several times, and then click Developer options to open USB debugging

Adb operates in the emulator

#Install apKAdb install The full path of apK that needs to be installed on the computer
#View all installed software
adb shell pm list packages

#Uninstall the apkAdb uninstall com.tal. Kaoyan # adb uninstall com.talCopy the code

Install ADB in Ubuntu

Run the following command to copy the preceding files to Linux: sudo cp adb /usr/local/sbin/sudo cp fastboot /usr/local/sbin/sudo unzip -d /usr/local/sbin/ Platform - tools_r29. 0.6 Linux. Zip#Add executable permissions
sudo chmod +x /usr/local/sbin/platform-tools/adb /usr/local/sbin/adb
sudo chmod +x /usr/local/sbin/platform-tools/fastboot /usr/local/sbin/fastboot

#Adding environment variablesSudo vim /etc/profile At the bottom of the file, enter export PATH=/usr/local/sbin:$PATHCopy the code

How do I connect to Windows mobile devices in Ubuntu

adb devices
#Change the connection mode to TCP connection mode for convenient connection to Ubuntu
adb tcpip 5555

Copy the code

Open the Simulator of Night god, click Settings, and click Mobile phone and Network. It is better to set the mobile phone model to OPPO, and check open network bridge mode

If it fails, just reset it

If the wifi of the simulator is not connected, please refer to the following connection, change the network and delete the driver

Bbs.yeshen.com/forum.php?m…

Finally, use your phone’s wifi to connect, but before connecting, be sure to change the adapter options and remove VirtualBox NDiS6 from the other connection properties

We type in Ubuntu

The adb connection 192.168.3.135Copy the code

We will see our device when we enter adb Devices again

Let’s install apK

Abd install kaoyanbang_3. 5.6.272. Apk
#Then look at all the packages
adb shell pm list packages

#Uninstall packages
adb uninstall com.tal.kaoyan
Copy the code

6 Connect a real mobile device and install the APK-Windows environment

Plug in your phone, install the driver, open developer mode, and tap on usb to debug

! (E:\IMOOC\img\14.png)

Install APK on a real mobile device

Adb install apk pathCopy the code

If the installation fails, click the Developer option to revoke USB debug authorization, then reconnect the phone and install the driver

Uninstall APK on a real mobile device

adb shell pm list packages
adb uninstall com.tal.kaoyan
Copy the code

7 Connect to real mobile devices in the Ununtu system

After the phone is connected, select Connect to virtual machine

You can view the device on the VM

Then we can see our device in our command line

Ubuntu installs APK on real mobile devices

adb install kaoyanbang.apk
#Uninstall the apk
adn uninstall com.tal.kaoyan

Copy the code

4 U2 project initialization

How to install U2 Automation package and how to control our mobile devices

pip install uiautomator2 -i https://pypi.doubanio.com/simple
Copy the code

To initialize a mobile device, you need to install the Agent on it

python -m uiautomator2 init

Copy the code

My Huawei phone reported an error when using the above command in a VIRTUAL machine, so it ran successfully on my local computer

5 Three ways for U2 to connect to mobile devices

Connect via wifi on your phone

The current network environment is on a LAN

import uiautomator2 as u2
d = u2.connect_wifi('192.168.1.7')
print(d.info)
Copy the code

Usb Connection

To connect, enter the serial number of adb Devices in the command line interface
import uiautomator2 as u2
d = u2.connect_usb("XMKDU19403004799")
print(d.info)
Copy the code

Connect via ADB_wifi

Also known as ADB TCP IP mode, be careful to drop the port call

Start by typing it on the command line

adb tcpip 5555
Copy the code

And then enter it in the code

import uiautomator2 as u2
d = u2.connect_adb_wifi('192.168.1.7:5555')
print(d.info)
Copy the code

Obtain detailed device information

d.device_info
Copy the code

Launch the app on your phone

First, get the package name of the app

Aapt. exe, aapt dump badging apk, aapt dump badging apk, aapt dump badging apk, aapt dump badging apk Name ="com.ss.android.ugc. Aweme "Fetch the value after nameCopy the code

Start the app

# open
a.app_start('com.ss.android.ugc.aweme')
# run
time.sleep(5)
# exit
d.app_stop('com.ss.android.ugc.aweme')
Copy the code

Install aapt in Ubuntu

sudo apt-get install aapt
Copy the code

When we Install apps on phones in Ubuntu, if Performing Install comes on, we just Install on Windows

Disconnect the mobile phone from the VM

VMS > Mobile DevicesCopy the code