This article comes from the official official account of the project: “AirtestProject” copyright statement: reprint is allowed, but reprint must retain the original link; Do not use for commercial or illegal purposes

preface

In the last episode of the Airtest interface feature and example summary, we summarized the following 4 aspects of the API features and examples:

  • 1. The script initializes the interface
  • 2. The device connects to and uses related interfaces
  • 3. Operate interfaces related to applications
  • 4. Common simulation operations

This week, we’ve covered the rest of the Airtest API and Settings:

  • 5. Assert related interfaces
  • 6. Log Indicates the interface for recording and generating reports
  • 7. Global Settings for Airtest

5. Assert related interfaces

1) Assert_exists ()

There is an assertion target on the device screen, and you need to pass in 1 assertion target (screenshot) and the assertion step information displayed on the report, as shown in the example:

Assert_exists (Template(r"tpl1607324047907.png", record_pos=(-0.382, 0.359), resolution=(1080, 1920)), assert_exists(Template(r"tpl1607324047907.png", record_pos=(-0.382, 0.359), resolution=(1080, 1920)),Copy the code

After running the script, the assertion step of the Airtest report displays our custom information:

The API documentation of the interface links: airtest. Readthedocs. IO/zh_CN/lates…

2) Assert_not_exists ()

There is no assertion target on the device screen. As with assert_EXISTS (), you need to pass in an assertion target (screenshot) and the assertion step information displayed on the report, for example:

Assert_not_exists (Template(r"tpl1607325103087. PNG ", record_pos=(-0.005, 0.356), resolution=(1080, 1920)), "Icon of Tmall International does not exist on current page ")Copy the code

The API documentation of the interface links: airtest. Readthedocs. IO/zh_CN/lates…

3) Assert_equal ()

To assert that two values are equal, you need to pass in the values of both assertions, along with a short description of the assertions to be recorded in the report:

Assert_equal (" Actual value ", "predicted value "," Please fill in a short description of the assertion ")Copy the code

Assertions are often made in conjunction with poCO attribute fetching scripts, as shown in the following example:

Assert_equal (POco ("com.taobao. Taobao :id/dx_root").get_text(), "New tmall ", Assert_equal (STR (POco (text=" New Tmall ").attr(" Enabled ")), "True", "The value of the control's Enabled property is True")Copy the code

The API documentation of the interface links: airtest. Readthedocs. IO/zh_CN/lates…

4) Assert_not_equal ()

Assert that two values are not equal. Like assert_equal(), you need to pass in the values of both assertions, along with a short description of the assertions to be recorded in the report:

Assert_not_equal (" Actual value ", "predicted value "," Please fill in a short description of the assertion ") asserT_not_EQUAL ("1", "2", "Assertions 1 and 2 are not equal ")Copy the code

The API documentation of the interface links: airtest. Readthedocs. IO/zh_CN/lates…

PS: If the assertion fails, an AssertionError is raised and the script stops running. If the assertion fails and the script continues to run, students can use statements such as try-except to catch the AssertionError.

For more assertions tutorials, check out our previous tweet: Testing What Everyone should Know about Assertions…

6. Log Indicates the interface for recording and generating reports

1) log()

The log() interface facilitates the insertion of user-defined log information that will be displayed in the Airtest report. In version 1.1.6 of Airtest, the log interface supports passing four parameters:

  • args, which can be a string, non-string, ortracebackObject;
  • timestamp, used to customize the timestamp of the current log;
  • desc, used to customize the title of the log;
  • snapshot, indicating whether a current screen image needs to be captured and displayed in the report:

The following is an example:

# Pass string log("123",desc=" this is header 01") # pass non-string data = {"test": 123, "time": Traceback try: 0 except Exception as e: log(e, desc=" this is header 03")Copy the code

Note that if timestamp is not passed, the current time is recorded by default:

Log ("123", timestamp=time.time(), desc=" this is the title 04", snapshot=True)Copy the code

The API documentation of the interface links: airtest. Readthedocs. IO/zh_CN/lates…

2) Report generation: simple_report()

A simple interface for generating reports. The user only needs to call this interface in the script and pass in the necessary report parameters to automatically generate Airtest reports as required after the script is run:

simple_report(filepath, logpath=True, logfile='log.txt', output='log.html')
Copy the code

The four parameters that can be passed in are:

  • filepath, the path to the script file, and variables can be passed directly__file__
  • logpath, the path where the log contents are stored, such asTrueBy default, the log file is found in the path where the current script is located
  • logfile, log.txt file path
  • output, the report everywhere path must be to.htmlAt the end

The following is an example:

from airtest.report.report import simple_report auto_setup(__file__, Logdir =True) # Omit N use case scripts here simple_report(__file__,logpath=True,logfile=r"D:\test\1234.air\log\log.txt",output=r"D:\test\1234.air\log\log1234.html")Copy the code

It is also important to note that we need to call the reporting interface after the use case script. If we call it at the beginning of the script, it means that a report has been generated before the subsequent use case steps have been run, and we will end up with an empty test report no matter how well our script runs.

The API documentation of the interface links: airtest. Readthedocs. IO/zh_CN/lates…

3) Report generation: logtohtml()

The base class for reports, which is actually the same class as simple_report, but simple_report takes fewer arguments and is easier to use:

class LogToHtml(script_root, log_root='', static_root='', export_dir=None, script_name='', logfile='log.txt', lang='en', plugins=None)
Copy the code

As you can see, the logtoHTML class can pass a lot of arguments, which makes it a little more complicated to use:

  • script_root, script path
  • log_root, path of the log file
  • static_root, where static resources are deployed
  • export_dir, and the storage path of the exported report
  • script_name, script name
  • logfile, the path to the log file log.txt
  • lang, the language of the report (Chinese: zh; English: EN)
  • plugins, which can be used if you use POCO or AirTest-Selenium

When using LogtoHTML to generate a test report, we typically instantiate a LogToHTML object and then call the class method report() from that object to generate the report, as shown in the following example:

H1 = LogToHtml(script_root=r'd :\test\1234.air', log_root=r"D:\test\1234.air\log", export_dir=r"D:\test\1234.air" ,logfile=r'D:\test\1234.air\log\log.txt', lang='en', plugins=["poco.utils.airtest.report"]) h1.report()Copy the code

LogToHtml class API documentation links: airtest readthedocs. IO/zh_CN/lates…

PS: If you need to view the command line report generation and use the script to generate a report in detail tutorial, recommended to read the article Airtest report “overview”, see directly use the script to generate, export the report, really sweet!

7. Global Settings for Airtest

Airtest Settings module as shown in the document: Airtest readthedocs. IO/zh_CN/lates…

1) Log content Settings: LOGFILE, LOGDIR

LOGFILE Specifies the name of the TXT document that records the log content. LOGDIR is used to customize the path for storing logs. The following is an example:

from airtest.core.settings import Settings as ST from airtest.core.helper import set_logdir ST.LOG_FILE = "log123.txt" Set_logdir (r'd :\test\1234.air\logs') auto_setup(__file__Copy the code

2) Setting of image recognition algorithm: CVSTRATEGY

CVSTRATEGY is used to set Airtest’s image recognition algorithm. By default CVSTRATEGY = [“surf”, “TPL “, “brisk”] Airtest executes this set algorithm order every time it looks up an image. Until it finds a result that matches the threshold, or keeps iterating through the algorithm until it times out.

We can customize the image recognition algorithm of Airtest, as shown in the following example:

from airtest.core.settings import Settings as ST

ST.CVSTRATEGY = ["tpl", "sift","brisk"]
Copy the code

This article will teach you to choose the right image recognition algorithm in 3 minutes

3) Image THRESHOLD: THRESHOLD, THRESHOLD_STRICT

Both THRESHOLD and THRESHOLD_STRICT are thresholds for image recognition. After Airtest1.1.6, all interfaces that use image recognition use THRESHOLD as thresholds. The default value is 0.7 and ranges from 0,1.

In the process of image matching, only when the credibility of the recognition result is greater than the threshold, the matching result is considered to be found:

In addition to changing the global image recognition THRESHOLD, we also support changing the THRESHOLD of a single image. Note that if we do not set a separate image THRESHOLD, the global THRESHOLD will be used by default, as shown in the following example:

From airtest.core. Settings import Settings as ST # Set global THRESHOLD to 0.8 st. THRESHOLD = 0.8 # Set individual image THRESHOLD to 0.9 Touch (Template(r"tpl1607424190850.png", threshold=0.9, record_pos=(-0.394, -0.176), resolution=(1080, 1920))Copy the code

Before Airtest1.1.6, asserT_EXISTS used an image threshold of THRESHOLD_STRICT. So if you want to change the threshold for an Assert_EXISTS image before 1.1.6, you need to set the THRESHOLD_STRICT value.

4) Query timeout duration: FIND_TIMEOUT, FIND_TIMEOUT_TMP

As we mentioned above, in the image matching, will cycle with several algorithms to identify, but loop identification is not unlimited, there is a query timeout value setting, once the query time is greater than the timeout value, or the result of credibility is greater than the threshold, was not found that decided the match fails, the default timeout value is as follows:

FIND_TIMEOUT = 20
FIND_TIMEOUT_TMP = 3
Copy the code

There are many interfaces that use FIND_TIMEOUT as the timeout duration, such as assert_EXISTS (), touch(), wait(), swipe(), and so on.

Interfaces that use FIND_TIMEOUT_TMP as timeout are rare, such as assert_not_exists() and exists().

Similar to the threshold, we can either change the global timeout period or set the single statement timeout period, as shown in the following example:

From airtest.core. Settings import Settings as ST # Set the global timeout period to 60s st.find_timeout = 60 st.find_timeout_tmp = 60 # Wait (Template(r"tpl1607425650104. PNG ", record_pos=(-0.044, -0.177), resolution=(1080, 1920)),timeout=120)Copy the code
5) Project root directory: PROJECT_ROOT

The project root directory is often used when invoking other.air scripts, as shown in the following example:

From airtest.core. Settings import Settings as ST # PROJECT_ROOT = "D:/test/user/project" Using ("test1.air") using("test2.air") We may have to so called test1. Air, test2. The air using (" D: / test/user/project/test1 air ") using (" D: / test/user/project/test2. Air ")Copy the code

In addition, the auto_setup() interface we introduced last time can also be passed into the project root directory:

auto_setup(__file__, project_root="D:/test/user/project")
Copy the code
6) Screenshot compression accuracy: SNAPSHOT_QUALITY

SNAPSHOT_QUALITY is used to set the accuracy of the global snapshot compression. Note that it is set to the accuracy of the Airtest report, not the accuracy of the screenshot taken by our screenshot script. The default value is 10. The value ranges from 1,100. The higher the value is, the more accurate and clear the screenshot is. The following is an example:

From airtest.core. Settings import Settings as ST #Copy the code

In addition, we can also define the compression accuracy of a single screenshot, for example:

# Set the snapshot accuracy to 90 for a single snapshot(quality=90)Copy the code
7) Screenshot size: IMAGE_MAXSIZE

In Airtest1.1.6, a new setting has been added to specify the maximum size of a screenshot: st.image_maxsize. If set to 1200, the length and width of the final saved screenshot will not exceed 1200, which is conducive to further reducing the size of the screenshot. Example:

Select * from airtest.core. Settings import Settings as ST # select * from airtest.core. Settings import Settings as ST # Select * from airtest.core. The default value is the global variable in ST, (filename="test2.png", MSG ="test02", quality=90) max_size=1200)Copy the code

Airtest website: airtest.netease.com/ Airtest tutorial website: airtest.doc.io.netease.com/ build enterprise private cloud service: airlab.163.com/b2b

Airtest official Q group: 654700783

Ah, so serious all see here, help in the left side of the article click like and collection, give me a support, ash often thanks ~