In this article, we explain the use of Airtest. The main learning objectives are as follows:
(1) Know Airtest
(2) Understand what Airtest can do
(3) Airtest installation and environment construction
(4) Master the use of Airtest graphical api
(5) Airtest realizes automated testing of APP e-commerce
(6) Airtest connects to the ios real machine to realize automated testing
Table of contents
Introduction to Airtest and Environment Construction
Airtest connects with Android emulator
Airtest Image Library (Touch, Script Run, Test Report)
Airtest Image API (text, snapshot, sleep, keyevent)
Airtest Image API - Assertion Method
Airtest image framework actual combat (app login, exit)
Airtest-actual iOS real machine (environment construction)
Airtest-actual iOS real machine (connected device)
Airtest-actual combat iOS real machine (DingTalk login, logout)
Introduction to Airtest and Environment Construction
1. Introduction to Airtest
Produced by the Netease team, Airtest is a cross-platform UI automation testing framework based on the principle of image recognition, suitable for games and applications.
Features:
(1) Cross-platform: Airtest can execute game and APP automation on almost all platforms
(2) Easy to operate: use image recognition technology to locate UI elements, and automate testing of games and applications without embedding any code.
(3) Scalability: By using the command line and Python API interfaces provided by Airtest, scripts can be easily run on large-scale device clusters
(4) GUI tool: AirtestIDE is a powerful GUI tool that can help you record and debug scripts.
Extension: In-depth comparison of Airtest's 3 image algorithms SURFMatching, TemplateMatching and BRISKMatching.
(1) Template matching TemplateMatching
- The advantage is that the speed is very fast, and the disadvantage is that it cannot be recognized across resolutions
- There must be a relatively best matching result
- Method name: "tpl"
(2) Feature point matching
- Recognition across resolutions
- not necessarily a match
- List of method names: ["kaze", "brisk", "akaze", "orb", "sift", "surf", "brief"]
The performance comparison conclusion of the feature point matching algorithm:
Memory usage: kaze > sift > akaze > surf > brief > brisk > orb
CPU占用:kaze > surf > akaze > brisk > sift > brief > orb
Running time: kaze > sift > akaze > surf > brisk > brief > orb
Recognition effect: sift > surf > kaze > akaze > brisk > brief > orb
2. What can Airtest do?
Airtest supports: automated testing of Game, Windows applications, Andriod applications, iOS applications, and web applications.
Features:
Game: With the help of image recognition and UI hierarchy, it supports all game engines and applications, multi-platform, and easy to use.
Android: In the testing process of Android source application testing, Air test can play a vital role, plug and play.
Wino dws: support Windows applications, write once, run across platforms.
iOS: Airtest supports convenient and quick testing of iOS. The Poco library can be used to quickly obtain the location and information of the native elements in the iOS device page.
Web: Based on the Chrome Devtools Protocol, automatically record and generate selenium scripts, precisely locate and operate interface elements.
Advantages of Airtest:
1) The framework is based on image recognition, UI and control recognition, with simple operation and concise functions.
2) It does not require high coding ability and is easy to use. Combined with the script recording function of the tool itself, the script development speed is fast, which is suitable for the requirements of rapid iteration of the version.
3) Python third-party libraries can be introduced to support Python for personalized script programming.
4) The test report can be generated with one click, and the report is beautiful and clear.
Airtest Disadvantages:
1) The biggest disadvantage is that the positioning of images and controls is not accurate enough. If the size and resolution of different devices are different, or the background color of the image changes, the control pattern is modified.
2) Because it is based on the framework of image recognition, the execution speed of the code is slow, and it is easy to cause the image to be unrecognizable.
Summary: The advantages outweigh the disadvantages, and there are many ways to improve the accuracy of image recognition.
3. Airtest library
Airtest has three categories of libraries: image recognition, Poco, and selenium.
4. Airtest environment construction
(1)Python
Enter cmd to open the command line window and execute the command: pip install -U airtest
Description: Install the Airtest library of Python, and directly call the API method of the Airtest library through Python code.
Tip: This method requires a certain foundation of Python. AirtestIDE has built-in Python3.6.5, and you can install airtest with Python3.6.5.
(2) GUI tools
Tool: AirtestIDE
Note: The AirtestIDE tool comes with a Python environment, which can be used after downloading and decompressing, without installation.
Tip: You can use the tool to record the operation, or you can manually write and call the API.
Learn GUI tools in this video tutorial.
(3) Download AirtestIDE
Official website download: https://airtest.netease.com/home/changelog.html
(4) Unzip
(5) start
Double-click the AirtestIDE.exe executable program.
(6) Airtest page layout
Airtest connects with Android emulator
Commonly used Android emulators: NetEase's MUMU, Yeshen, Raiden, etc. We use NetEase MUMU here, download and install it directly to C:\Program Files\MuMu
1. Preparation
(1) Turn on the developer mode
Ordinary Android phones: Go to Settings—>System (or About Phone)—>Find the version number, click a few more times, and you can turn on the developer mode.
Xiaokang equipment: pull down the home page to enter the navigation bar —> click time 5 times in a row —> click native settings —> system —> about AIV —> click version number 7 times in a row
Note: If the device has turned on the developer mode, clicking the version number will prompt: You are in the developer mode.
(2) Turn on the USB debugging mode
First open the developer mode, enter the developer options, and enable USB debugging.
NOTE: Be sure to select the USB configuration: MIDI (open file transfer)! ! !
(3) Connection settings
a. Start the Android emulator
b. Click [Refresh ADB] or [Remote Device Connection] in the Airtest window
c. Use alternate connection parameters, set compatibility mode
AirtestIDE provides 3 alternate connection parameters: Use javacap, Use ADB orientation and Use ADB touch
① The first Use javacap is for some mobile phones or devices that cannot see the mobile phone screen normally, or the minicap initialization fails, so you can consider checking this option when the emulator sees a black screen, some special tablets and other devices.
② The second Use ADB orientation is for screen rotation. If there is a problem with the detection of the screen rotation orientation of the Android phone, or some special tablets cannot display the correct screen orientation, you can check it.
③ The third Use ADB touch is to send adb commands to click on the screen, the effect is very poor and the speed is very slow, it is not recommended to check it, it is only needed on some special Android devices that cannot touch the screen (such as smart rearview mirror, On special models of tablets and other devices) Under normal circumstances, mobile phones can be clicked. If they cannot be clicked (such as Xiaomi devices), it is generally because there is an option in the phone settings that is missing to open, especially for Xiaomi devices. Pay attention to enabling the simulated click set up.
(4) Modify the device address and port number (because the real machine or the emulator is divided into different manufacturers)
Airtest remote connection, the port number of Netease MUMU is displayed by default. If you use an emulator from another manufacturer, you need to modify the port number.
The default IP addresses and port numbers of common emulators are as follows:
Airtest Image Library (Touch, Script Run, Test Report)
1. Learning objectives
Master the touch method application
Master the script run
Master Test Report View
Understanding Image Recognition Extensions (Location, Matching Degree)
2. Airtest image framework API to view parameter information
Method: Hover the cursor over the API in the left column, and the bubbles will display the parameters that need to be passed in to call the method.
3. Touch method
Function: touch/click action
Common parameters:
v : the image or coordinates of the clicked object
times: the number of clicks, the default is 1
duration: click time, the default is 0.01 seconds
(1) Requirements:
Use the Touch method to click to start the Xiaokang desktop application (TV home)
Run the script to test for success
(2) Requirement realization steps:
a. New script type selection
.air Airtest project: Generate a directory of [project name.air], and there is a file of [project name.py] in the directory
.py Pure Python (advanced users): generate a pure Python [project name.py] file
The difference and connection between the two:
The same point: both are Python files
The difference: the initialization code of the script file is not used, one cannot see the default value, and the other can see the default value and can modify it.
Tip: Airtest comes with a Python interpreter and standard library, so there is no need to install Python separately.
b. Use Touch to click Xiaokang desktop application (TV home)
Click Touch on the left
Pull and select the TV home icon on the right side
c. Run the script
Click the triangular [Run] button, or use the shortcut key F5
Stop running: Shift + F5
Run a single line of code: select the line of code, right click, select and click "Run only the selected code"
d. View report
Method 1: Click [Run] on the menu bar --> open the report directory
Method 2: Right-click the script file name Tab --> open the report file directory
Method 3: Use the shortcut key: Ctrl + L
Method 4: Open the command line window with cmd, enter the path where the script is located, and execute the following naming:
airtest report D:\zxt\AirtestIDE\xiaokang.air\xiaokang2_auto_script.py --log_root D:\zxt\AirtestIDE\xiaokang.air\log --outfile D:\zxt\AirtestIDE\xiaokang.air\log\xiaokang2_auto_script.log\log.html --static_root D:\zxt\AirtestIDE\airtest\report --lang zh --export D:/zxt/AirtestIDE/xiaokang.air/log
Method 5: Right-click the script file name Tab --> Export Report
In this way, image resources and static resources with relative paths can be packaged as a whole, and then sent to others for viewing.
Method 6:
# generate html report
from airtest.report.report import simple_report
simple_report(__file__,logpath=True,output='D:\zxt\AirtestIDE\xiaokang.air\report\log.html')
4. Image recognition extension
In the script editing area, double-click the image to display the Image Editor popup, as follows:
filename: image file name, automatically generated
threshold: Threshold or critical value, only when the set threshold is reached, image recognition can be successful.
target pos: The target position of the click position, the default is position 5. The target position of the picture is divided into upper, middle and lower 9 o'clock positions, and the middle area is position 5.
Airtest Image API-wait
wait() method
Function: Waiting for the interface elements to appear, the default is 0.5s to search once, and the maximum search time is 20s. If found, return the coordinates of the center point of the image; otherwise, raise TargetNotFoundError
Common parameters:
v: image
timeout: wait timeout time, the default is 20s
interval: the time interval for each match
Requirements: Enter the Xiaokang homepage, wait for the "Entertainment" icon to appear, search once every 0.1s, and last for 3s, if found, click the "Entertainment" icon.
summary:
(1) What problem does the wait method solve?
Solve the problem that the interface elements exist, but it takes time to load.
Lesson 6, Airtest image API-swipe
The swipe method basically uses:
Function: Swipe from one position on the screen to another
Common parameters:
v1: image or coordinates (x,y)
v2: image or coordinates (x, y), slide from v1 to v2
vector: [x,y] is automatically generated when recording, and records the sliding ratio on the screen. The right direction is the positive direction of the x-axis, and the downward direction is the positive direction of the y-axis.
Airtest Image API (text, snapshot, sleep, keyevent)
1. Text method
Function: input text operation
Common parameters:
text: the text to enter
(Note: The input position is the position where the cursor focus is on the current page, and it is generally used together with the touch method)
enter: Automatically execute the Enter operation after completing the input, the default is True
2. keyevent method
Function: Simulate keyboard button input, support key codes, such as 3 for home key
Common parameters:
keyname: fixed key name or key code, refer to: https://www.cnblogs.com/vip136510786/p/14705567.html
3. snapshot method
Function: Capture the current screen picture, which can be displayed in the test report.
Common parameters:
filename: Save the screenshot as the specified file.
msg: Describes the test point, which can be rendered in the html report.
4. sleep method
Function: pause time
Common parameters:
secs: Pause time, in seconds, default 1.0s
Example requirements:
Launch Centennial Outlet app
Click on the search input box
Input: Headphones
Complete screenshot
Pause for 3 seconds
Press the home button to return to the desktop
Airtest Image API - Assertion Method
1. assert_exists method
Function: Assert that an element exists on the page, and the result is a Boolean value
Common parameters:
v: picture
msg: describe the test point
return: If the picture is found, return the coordinates of the center point of the picture; otherwise, raise AssertionError
2. assert_not_exists method
Function: Assert that an element does not exist on the page, and the result is a Boolean value
Common parameters:
v: picture (note: judge that the specified picture does not exist in the current page, if it does not exist, it will pass, if it exists, it will not pass)
msg: describe the test point
3. assert_equal method
Function: judge that the first value is equal to the second value
Common parameters:
first: the first value
second: the second value
msg: Describe the content of the test point corresponding to this assertion statement.
4. assert_not_equal method
Function: judge that the first value and the second value are not equal
Common parameters:
first: the first value
second: the second value
msg: Describe the content of the test point corresponding to this assertion statement.
need:
Launch Centennial Outlet APP
Determine whether there is a search icon on the homepage
Click [Category] TabBar at the bottom
Assert that the search icon does not exist
Assert whether the values of False and assert_exists (search icon image) are equal
View the test report as follows:
Airtest image framework actual combat (app login, exit)
1. Requirements:
Start DingTalk APP
Realize login and exit business automation, and assert whether the login and exit are successful
2. Expansion:
How to solve the problem of not being able to enter the account: MUMU emulator settings --> language and input method --> change the input method to nemu-vinput
Switch code mode: In the code editing area, select the code line, right-click to select and click "Switch Image/Code Mode".
Airtest-actual iOS real machine (environment construction)
1. Environment construction needs
(1) Hardware
An Apple computer (running xcode)
An iphone (run APP)
(2) software
iOS-Tagent (WebDriver server)
xcode (iOS integrated development tool, running iOS-Tagent)
iproxy (proxy work, do port mapping)
AirtestIDE (image recognition automated testing tool)
2. Software function
(1)iOS-Tagent
Function: Create a WebDriver server on the mobile phone, which can be used to remotely control iOS devices and locate UI elements.
Download: https://github.com/facebook/archive/WebDriverAgent
Running dependency: xcode
(2)xcode
Function: iOS integrated development tool, the main function is to run the WebDriverAgent file to the mobile phone
Download: appStore search xcode
Running dependency: developer account
xcode settings:
Premise: Connect the real machine to the mac computer with a data cable
Test run WebDriverAgentRunner to phone
If it fails, check ideas:
①After clicking Test in xcode, the phone needs to trust the project (Settings -> General ->
device management)
②Enable UI automation in the mobile phone (Settings->Developer->EnableUIAutomation)
③If there are other abnormalities, refer to Baidu or visit
https://github.com/appium/appium/blob/master/docs/en/drivers/ios-xcuitest-real-devices.md
Check out related solutions
Operations required by xcode:
a. Add a developer account, just a normal apple ID
b. Deployment WebDriverAgent (Team, Product Bundle Identifier)
c. Test run WebDriverAgentRunner to the mobile phone
Airtest-actual iOS real machine (connected device)
1. Precautions for connecting to the real device
(1) Before clicking Test in xcode, check whether the default terminal of the project is a real device.
(2) After clicking Test in xcode, you need to view the control information. If there is no information output on the console, you can wait or test several times until the console outputs startup-related information.
(3) Xcode configures the iOS-T agent only for the first time, and there is no need to configure it separately for later use, and do not modify the parameters indiscriminately;
(4) In the real device, it is necessary to open the automated test and trust the iOS-T agent project
2. Steps to connect to the real device
(1) Connect the real device to the computer with a data cable
(2) Start xcode and open the configured iOS-T agent project (menu-Product->Test to start the automation service program)
(3) Open the terminal and run: i proxy 81008100 (start port mapping service program)
(4) Start the Air Test IDE tool (select [connect ios device] -> click [connect])
Airtest-actual combat iOS real machine (DingTalk login, logout)
Summary:
1. API usage is no different from Android
2. The speed of the real machine is fast, and it needs to pause for a while when entering the user name and password
3. The difference between ISO and Android lies in the environment construction
Finally, I would like to thank everyone who has read my article carefully. Looking at the fans’ growth and attention all the way, there is always a need for reciprocity. Although it is not a very valuable thing, you can take it away if you need it:
These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can help you too!
In my QQ technology exchange group (technical exchange and resource sharing, no advertising)