This article is from the official account of the project: “AirtestProject” Copyright notice: It is allowed to be reproduced, but the original link must be retained. Do not use it for commercial or illegal purposes

1. Introduction

Novice students can write/run automated scripts by using the AirtestIDE. But after mastering Airtest and the Poco framework, you can write/run a script without AirtestIDE.

This article will tell you two ways to get out of the AirtestIDE running script and what to do if you are interested.

2. Run the automatic script using the command line

1) Run according to the IDE run script command

When we run the script using the IDE environment, a command to run the current script will appear at the top of the log view window. We can directly copy the command to run the command line terminal:

Using only this command to run the script, we actually use the AirtestIDE environment to run the script. The advantage is that we do not need to deploy a local Python environment:

There is one small detail that needs to be noticed, for example, the command line contains characters like &&, which need to be escaped:

# special characters under Windows escape && - > ^ & ^ & # special characters under Mac escape && - > / & / &Copy the code

If not escaped, the end result is that the command line is truncated. As shown in the figure, we are prompted to do not save log even though our run command specifies the path to save the log because the command is truncated after the special character &&.

And we’ll see something like ‘ori_method’ is not an internal or external command, or a runnable program or batch file in the final run log:

With escape, the command line is not truncated:

2) Deploy the local Python environment run

In addition to using the AirtestIDE’s own Python environment to run scripts, you can also deploy a local Python environment to run scripts.

First we need to prepare a suitable Python environment, greater than Python3, less than or equal to Python3.9.

Then install our automated test framework right here in the Python environment, as you would any other Python third-party library:

PIP install Airtest; PIP Install Pocoui # Install the Airtest-Selenium framework. PIP install Airtest-Selenium is required to write the airtest-Selenium statementCopy the code

If an error occurs or a timeout occurs during the installation of a third-party library, please ensure that your Python version is within the supported range before using tsinghua or other domestic sources:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple airtest
Copy the code

After a successful installation, we can use the PIP list command to check our installation status and framework version information:

(PS: Can not install poCO and Pocoui at the same time, they will conflict!)

Next, we can run the automation script we have written in this environment:

Airtest run "D: \ test_plu \ song. The air" - device android: / / 127.0.0.1:5037/127.0.0.1:7555 - log "D: / test/test01 \ ed879c1f10fa732db3e5e2c417ca7221" # or python - m airtest run "D: \ test_plu \ song. The air" - the device Android: / / 127.0.0.1:5037/127.0.0.1:7555 - log "D: / test/test01 \ ed879c1f10fa732db3e5e2c417ca7221"Copy the code

For details on how to run scripts and generate Airtest reports using commands, see our previous Airtest report “Walkthrough” tweet.

3. Run automated scripts using other editors

Using pychram, a common Python editor, as an example, this article details how to write/run automated scripts using this editor.

1) Environment deployment

Once we create a new project in PyCharm, we need to set up the Python interpreter for that project; Pycharm allows you to directly set up locally installed Python as the interpreter, as well as use a virtual Python environment as the interpreter.

The difference between the two is that if we directly set up locally installed Python as the interpreter, all third-party libraries installed locally in Python will be imported to be used by the PyCharm project:

If we create a new virtual environment, we will copy a clean Python virtual environment without any third-party packages, based on a local Python interpreter. This means that all third-party packages installed in the local Python interpreter will not be copied:

This is why many students could not find the third-party libraries they needed when they first started using PyCharm, even though they had already installed the third-party libraries in their local Python environment. (Note if virtual environment is used)

To install the third-party libraries in PyCharm, click the + sign on the right of the Python interpreter, enter the name of the package you want to add, and then click the Install package button:

With the environment deployed, we were ready to write/run scripts in PyCharm.

To copy the script directly from AirtestIDE to PyCharm, let’s see what we need to pay special attention to:

2) Log saving

We can write and run the script in AirtestIDE. We haven’t considered this problem because the AirtestIDE will automatically save the log content of the script running. The default path is “set” — “Airtest” — the default path to save the log:

When copying the code to the PyCharm run, to save the log content (in preparation for generating the test report later), we need to manually add this content:

# set logdir in the auto_setup interface to save log contents. # set logdir in the auto_setup interface to save log contents. Pass True to generate log contents in the current project directory auto_setup(__file__,logdir=True)Copy the code

Log content often contains screenshots of steps required for reporting and a log.txt file.

3) Device connection

The second common problem is that in the AirtestIDE, if the device connection window has already connected to the specified device, when you run the. Air script, you can automatically connect to the current device in the window to run the script without any extra processing.

Pycharm obviously doesn’t help us with the device connection, so we need to connect the device under test in the script:

# in auto_setup auto_setup interface to devices parameters (__file__, logdir = True, devices = [" android: / / 127.0.0.1:5037/127.0.0.1:7555 "])Copy the code

Of course, we also have a lot of interfaces that can be used to connect our various devices under test. Here are not one example. Interested students can refer to our previous tweets to solve the problem of automatic test device connection with 1 line of code.

4) Picture path

The default path of the image captured by the AirtestIDE is a relative path, which is saved in the. Air script and the same path as the. Py file:

Template(r"tpl1638179990578.png", record_pos=(0.179, -0.57), resolution=(810, 1440))Copy the code

But copy the pycharm executes, relative path probability of big change, lead to the students run back, often appear airtest. Aircv. Error. FileNotExistError: File not exist: Error tPL1638179990578.png

At this point we can either change the path to the correct relative path or the absolute path, just make sure pyCharm finds your screenshots in the path you set:

Touch (Template(r"D:\test_plu\ song-air \tpl1638179990578.png", record_pos=(0.179, -0.57), resolution=(810, 1440))Copy the code
5) File reference path

Many students will call some common scripts in their own automation scripts, which may be.air scripts, or.py scripts.

Suppose we have the following scripts and their storage relationships are as follows:

If we want to call common.air and im_ex.py within the song-air script, we can write the script like this:

To call the.air script, we can use the special interface provided by Airtest using; Call the.py script, just like standard Python, directly from… import … Can.

But when we open song-air in Pycharm and run it, we can find the script to run in AirtestIDE. We can’t find the module in Pycharm:

Pycharm can find the common.air script by manually adding sys.path:

6) Report generation

The last thing to note is report generation. After running the. Air script of the AirtestIDE, you can click the View Report button to generate and open a test report in HTML format.

Pycharm does not have this feature, so if we need to generate the Airtest report, we can only write the statement to generate the test report in the script:

# -*- encoding=utf8 -*- __author__ = "AirtestProject" from airtest.core.api import * from airtest.report.report import Simple_report auto_setup (__file__, logdir = True, devices = [" android: / / 127.0.0.1:5037/127.0.0.1:7555 "]) Touch (Template (r "tpl1638243250870. PNG," record_pos = (0.362, 0.13), the resolution = (810, 1440))) simple_report(__file__,logpath=True)Copy the code

When using scripts to generate Airtest reports, there are many considerations:

  • Report generation statements are placed after the concrete script to prevent empty test reports from being generated without running the concrete steps
  • When the script contains POCO or Airtest-Selenium statements, you need to add the corresponding reporting plug-inplugins
  • Regardless of whether the intermediate steps run successfully or not, a test report is generated that we can usetry-finallystatements
  • To generate a test report, you must save the log content because the report generation depends on the log content
  • If you want to send the report to others, you need to add export parameters in the statement that generates the report. Only the exported report can be sent to others

For more on using scripts to generate test reports, you can refer to our previous Airtest report, “The Whole walkthrough.” .


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

Official Q group: 654700783