How to produce compatible automated test scripts¶
Before reading this tutorial, make sure to read Our Quick Start Tutorial。
By reading this tutorial, you will know the followings: - How to choose Airtest/Poco - Improve the compatibility of screenshot script - Improve the running speed of the script - How to quickly verify the compatibility of the script
How to choose Airtest/Poco¶
In the test script, we can use both image script (Airtest) and UI script (Poco).Compared with image scripts, UI scripts are more accurate and run faster.How should we use these two types of scripts when writing scripts?
This depends on the type of the application being tested: - Native apps: - It is recommended to use Poco's UI script directly, and use image script for some necessary interface judgment. - Games: - For games that have been integrated with poco-SDK, you can use Poco's UI script, and use the image script for the necessary interface. - For games that cannot be integrated with the SDK for the time being, image scripts can be used.
Improve the compatibility of screenshot script¶
In the writing of test scripts, many students will find it difficult to guarantee the compatibility of image scripts. Here are some common tips:
1、Reasonably adjust the threshold¶
When each line of image recognition script runs, it includes three steps: ① the preliminary recognition result of image recognition; ② calculating the credibility of the result; ③ filtering the result by the threshold in the script.
The threshold in the script plays the role of screening results here. The level of the threshold will directly affect the recognition results returned.
If the threshold is too high, all low-confidence results may be filtered out, and eventually no valid image recognition results will be obtained.
If the threshold is too low, incorrect results may be returned (because the threshold is too low to allow false results to pass the filter).
When we improve the compatibility of image scripts, the most important part is to set reasonable thresholds. Default recognition threshold is 0.7 in Airtest framework, and we can make appropriate adjustments.
1.1 Adjust Threshold: Use the Image Editor to adjust the threshold of the single-line script¶
We generally use "Photo Editor" to modify the threshold. Double-click the picture in the script box to open the “Photo Editor”. We can adjust the
threshold value on the right side in real time and click the
Snapshot + Recognition button to verify the recognition result in real time. The credibility of the recognition result will be directly displayed on the status bar below, and the recognition result will be returned when the credibility is greater than the threshold:
Note: It is recommended to set the threshold between [0.6, 0.9]. In this way, it is possible to avoid mixing the wrong result with the threshold being too low, and also avoid the situation that the result is not found because the threshold is too high.
1.2 Adjusting Thresholds: Setting Global Thresholds for Scripts¶
If we want to set the threshold for the image scripts of the entire test script, we can set the global threshold at the beginning of the script:
# The range of the global threshold is [0, 1] from airtest.core.setting import Settings as ST ST.THRESHOLD_STRICT = 0.7 # The default threshold of the assert_exists statement, which is generally higher than THRESHOLD ST.THRESHOLD = 0.7 # Default threshold for other statements
2.1 More feature points!¶
For example in this interface:
I need to click the second [Install] button in the middle.If I take a screenshot like this.The program may not click at all, or click the wrong button.
But if we change it another way, we can take pictures with more features:
, The result returned this time will be correct.
2.2 Set the position of the click -
The setting of the click position is actually to be able to click to the correct position when there are many feature points in the screenshot.For example, in the pop-up screenshot below, you need to click the close button:
If you just take a picture like this.The success rate of image recognition is not high.
But if we take a screenshot of the entire window and double-click the picture to set
traget_pos = 2. In this way,the recognition success rate is relatively high. The screenshot is below:
The setting of the parameter
target_pos can refer to our document: Image Click Position
3、Custom statements improve the compatibility of image script¶
For some situations, such as different device aspect ratios, different device resolutions, multiple fonts, etc., we can improve the compatibility through syntax.This method requires connecting the device with script compatibility issues and including the corresponding screenshots in the search list. The code is as follows:
picList = [pic1,pic2,pic3] # List of picture objects for screenshots for pic in picList: pos = exists(pic) if pos: touch(pos) break # As long as any picture in the picture list is found, touch is executed
Note: If there is no
break statement in the
for loop, it will cause all the images to be searched (the
touch is executed after finding), instead of returning immediately after finding a suitable result.
Convenient operation in some scenarios¶
In some scenarios, through some alternative operations (such as directly operating coordinates, using button instructions, memorizing element positions, etc.), we can eliminate compatibility debugging for image recognition and even speed up script execution.
1、 Operate coordinates¶
1.1 Case 1: cutscenes¶
For example, cutscenes will be slow, we can choose to skip them. At this point we can operate the coordinates. The game cutscenes are shown below:
# perform a simple click touch([10, 10])
1.2 Case 2: App introduction pages¶
After the koala app is opened, there are 4 introduction pages that need to be swiped to enter.If you use airtest/poco UI test statements, it will take a long time to run.But if you use a fixed coordinate position to slide, you can enter the APP in four executions. It should be noted that the continuous operation of the coordinate script is too fast, and the device may not even respond. Generally, you need to add
sleep (1.0) after each statement and wait for the device to respond.
If there are many such situations in the script, you can encapsulate it into a general function and call it when you need it. This saves code and is fast.
# get the device width & height width, height = device().get_current_resolution() # cal swipe (start/end) point coordinate start_pt = (width * 0.9, height / 2) end_pt = (width * 0.1, height / 2) # swipe for 5 times: for i in range(5): swipe(start_pt, end_pt) sleep(1) # wait for the device's response
2、 Use key commands¶
In many cases, the BACK button can be used flexibly.When we open a page and want to return to the previous page, we can choose to press the UI button. But generally
keyevent ("BACK") can also achieve the purpose. This is not only simple and straightforward, but also compatible.
3、 Memory positions¶
There are also some special situations, such as in Hearthstone, where the enemy and our heroes' positions are fixed. These positions are needed every time we attack the enemy hero.
You can consider saving this global location, and then call the corresponding location when you need to use it.
It should be noted in the above figure that you need to use the
wait statement to wait to get the hero position, not
exists. Because we must get the position of the corresponding hero. So we need to wait for it to appear (no occurrence will trigger an error directly), and the
exists statement will not report an error if no hero appears.
In the picture above, when you need to use the follower to attack the opponent's hero at any time, you can
swipe the picture of the follower to the position of the opponent's hero.
Validate script compatibility with multi-device operation¶
Verify the script compatibility with our new feature, Multi-Script Multi-Device Plug-In. The same test script is run on multiple devices with different resolutions at the same time to ensure the compatibility of the script.
Please pay attention to our document "Run test scripts in batch using AirtestIDE".