Customize the step title of Airtest report? Practical speed learning

Keywords: Python Airtest

1. Preface

Today, let's talk about a very practical topic! Many students mentioned that can I modify the step name displayed in the Airtest report instead of displaying all the touch es as clicks and all the controls as poco clicks:

Today, we will use the -- plugins parameter to pass in the plug-in to meet the students' needs.

2. Introduction to -- plugins parameter

Many students may not be so familiar with the parameter -- plugins. Here is a brief explanation. After the command airtest report + script path for generating airtest report, you can add the -- plugins parameter and pass it into the report plug-in to customize the report content.

If we generate a pure Airtest script report, we actually ignore this parameter (except customization).

However, if we generate a report with poco or airtest selenium script, we need to take this parameter and pass it into the corresponding plug-in given by the project to parse and process poco / airtest selenium statements and modify some display effects.

1) Sample plug-in for generating poco script reports

Let's take the script report containing Poco statement as an example to see the difference between the plug-ins given by the incoming project and the plug-ins not imported:

# -*- encoding=utf8 -*-
__author__ = "AirtestProject"

from airtest.core.api import *
from airtest.report.report import simple_report,LogToHtml

auto_setup(__file__)

from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)

poco(text="NetEase cloud music").click()
  • Report generated without adding -- plugins parameter:

  • Report generated by adding -- plugins parameter:

This is the function of the -- plugins parameter in the command line. You can pass in the specified plug-in to modify the report content.

You can directly see the source code for what it does: https://github.com/AirtestProject/Poco/blob/master/poco/utils/airtest/report.py

We can also find the source file in the airtest ide \ poco \ utils \ airtest \ report.py path of airtest ide:

Similarly, to generate a report for a script containing an airtest selenium statement, you can also pass in the corresponding plug-in in the following way:

--plugins airtest_selenium.report

We can also use AirtestIDE \ airtest in AirtestIDE_ Selenium \ report.py find the report plug-in file of airtest selenium under the path:

3. Customize the content of the step title of the Airtest report

After we know how to use the -- plugins parameter to pass in the plug-in to modify the contents of the Airtest report, let's take the simplest modification example to see how to write our own plug-in to customize the title of the Airtest report.

1) Check the plug-in source code and find the content used to display the title on the left side of the report

Take modifying the touch step title of airtest as an example. We can check the source code of airtest's report.py first: https://github.com/AirtestProject/Airtest/blob/master/airtest/report/report.py .

You can see a man named_ translate_ The title method is specially used to display the title content on the left side of the report:

def _translate_title(self, name, step):
        title = {
            "touch": u"Touch",
            "swipe": u"Swipe",
            "wait": u"Wait",
            "exists": u"Exists",
            "text": u"Text",
            "keyevent": u"Keyevent",
            "sleep": u"Sleep",
            "assert_exists": u"Assert exists",
            "assert_not_exists": u"Assert not exists",
            "snapshot": u"Snapshot",
            "assert_equal": u"Assert equal",
            "assert_not_equal": u"Assert not equal",
        }

        return title.get(name, name)

In other words, if the touch function is called in the script, the corresponding title touch will be found in the report with the function name.

2) How to customize plug-ins

We can write a plug-in to replace it_ translate_ For the return value of title, you can imitate how the source code of poco.utils.airtest.report is written:

If we want to change the title touch corresponding to the touch step to Click, we can customize one such plug-in new_report.py :

# -*- coding: utf-8 -*-
import airtest.report.report as report

old_translate_title = report.LogToHtml._translate_title

def new_translate_title(self, name, step):
    title = old_translate_title(self, name, step)

    if title == "Touch":
        title = "Click"
    return title

report.LogToHtml._translate_title = new_translate_title

The meaning of this code is to use a new function new_translate_title to replace the LogToHtml in the original airtest module_ translate_title Method.

3) Pass in custom plug-ins through -- plugins

After writing the report plug-in, in order to quickly demonstrate the effect, we save the plug-in to the same directory as the current. air script, and press shift + right button to open cmd/PowerShell in the current directory:

Without importing the report generated by our customized plug-in, the touch step is still displayed according to the contents of the old plug-in:

airtest report D:\test_plu\song.air -log_root D:/test/test01\ed879c1f10fa732db3e5e2c417ca7221 --outfile  D:\test_plu\song.air\old_re.html

Pass in our customized plug-in, and the step title will be displayed according to our customized plug-in content:

python -m airtest report song.air --log_root D:/test/test01\ed879c1f10fa732db3e5e2c417ca7221 --outfile  D:\test_plu\song.air\new_re.html --plugins new_report

You can see that the original Touch has been replaced with Click in the step title.

4. Extension: the principle of report plug-in loading

For the principle of report plug-in loading, we can directly look at the source code: https://github.com/AirtestProject/Airtest/blob/master/airtest/report/report.py#L73-L82 .

Airtest uses python's__ import__ Try to import plug-in modules, such as the airtest report plug-in in poco. When importing, it is import poco.utils.airtest.report. Therefore, we use -- plugins poco.utils.airtest.report on the command line to import.

If you want to import a custom report plug-in in a directory other than the current directory, you can't import the path directly, such as trying to import -- plugins D: \ test \ report \ new_ For parameters such as report.py, it will be found that it cannot be loaded successfully.

We can open a python terminal to try:

>>> __import__("d:\\test\\report\\new_report.py")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'd:\\test\\report\\new_report'

However, if you try to add the PATH of the file to the system PATH, sys.path can find it after adding the PATH:

>>> import sys
>>> sys.path.append("d:\\test\\report")
>>> __import__("new_report")
<module 'new_report' from 'd:\\test\\report\\new_report.py'>

>>> import sys
>>> from airtest.report.report import LogToHtml
>>> sys.path.append("d:\\test\\report")
>>> rpt = LogToHtml(r"D:\test\report\yongli.air", r"D:\test\report\logs", plugins=["new_report"])
[14:32:22][DEBUG]<airtest.report.report> try loading plugin: new_report
>>> rpt.report()

At this time, you can open the generated html file and find that it has taken effect successfully. You can see that after LogToHtml has been executed just now, airtest automatically prints a log to load the plug-in, indicating that it has been successfully loaded.
Note that the parameter of plugins is a list, because multiple plug-ins are supported.

5. Summary

After knowing how to customize the plug-in and the loading principle of the plug-in, we can start to "Customize" our Airtest report.

For example, when clicking on some controls, we usually use the following script:

poco(text="NetEase cloud music").click()

Suppose we customize a poco plug-in to display the step title of this script as "click Control: Netease cloud music". Will it be clearer than the unified Poco Click?

Of course, this customized Airtest report needs to be deeply explored by the students. After all, each student's needs or reading habits are different. I hope you can "Customize" the Airtest report to your satisfaction as soon as possible.

Airtest official website: https://airtest.netease.com/
Airtest tutorial website: https://airtest.doc.io.netease.com/
Build enterprise private cloud services: https://airlab.163.com/b2b

Official Q-group: 654700783

Ah, I've seen it so seriously. Please give me a recommendation and support. Thank you very much~

Posted by harshilshah on Thu, 04 Nov 2021 10:02:55 -0700