對Airtest報告的步驟標題做內容定製?實用速學

1. 前言

今天我們來聊一個非常實用的話題!有很多同學提過,我能不能修改Airtest報告顯示的步驟名稱,不想要 touch 全部顯示成 點擊 ,控件點擊全部顯示成 Poco Click 之類的:

那今天我們就利用 --plugins 參數傳入插件,來實現同學們的這個需求。

2. --plugins參數簡介

可能還有很多同學不那麼熟悉 --plugins 這個參數,這裏簡單解釋一下。在生成Airtest報告的命令 airtest report + 腳本路徑 後面,支持添加 --plugins 參數,傳入報告插件,用來對報告內容做一些簡單的定製。

如果我們生成的是純Airtest腳本的報告,其實是不用理會這個參數的(定製除外)。

但如果我們生成帶有poco或者airtest-selenium腳本的報告,就需要帶上這個參數,傳入項目給出的對應插件,用於對poco/airtest-selenium語句的解析和處理,並修改一些顯示效果。

1)生成poco腳本報告的插件示例

我們以包含Poco語句的腳本報告爲例,來看下傳入項目給出的插件和不傳入插件的差別:

# -*- 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="網易雲音樂").click()
  • 不添加 --plugins 參數生成的報告:

  • 添加 --plugins 參數生成的報告:

這就是命令行裏 --plugins 這個參數的作用,可以傳入指定的插件,用來修改報告內容。

具體它做了什麼事,可以直接看源碼: https://github.com/AirtestProject/Poco/blob/master/poco/utils/airtest/report.py

我們也可以在AirtestIDE的AirtestIDE\poco\utils\airtest\report.py路徑下找到源碼文件:

同理,給含有airtest-selenium語句的腳本生成報告,也可以使用下述方式傳入對應的插件:

--plugins airtest_selenium.report

我們也可以在AirtestIDE的AirtestIDE\airtest_selenium\report.py路徑下面找到airtest-selenium的報告插件文件:

3. 對Airtest報告做步驟標題的內容定製

那我們瞭解瞭如何利用 --plugins 參數傳入插件來修改Airtest報告內容之後,這裏再以一個最簡單的修改範例,看下 如何寫出自己的插件來對Airtest報告做標題定製

1)查看插件源碼找到用來顯示報告左側標題的內容

以修改Airtest的 touch 步驟標題爲例。我們可以先查看一下,airtest的report.py的源碼:https://github.com/AirtestProject/Airtest/blob/master/airtest/report/report.py

可以看到有個叫 _translate_title 的方法,是專門用來顯示報告左側標題內容的:

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)

也就是說,假如腳本里面調用了 touch 函數,報告裏會對應地用函數名稱找到對應的標題 Touch

2)如何自定義插件

我們可以寫一個插件,來替換掉 _translate_title 的返回值,可以去模仿一下 poco.utils.airtest.report 的源碼是怎麼寫的:

假如我們想把touch步驟對應的標題Touch,修改成Click,可以自定義1個這樣的插件 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

這段代碼的意思是,用一個新的函數 new_translate_title,來替換掉原本airtest模塊裏的LogToHtml當中的 _translate_title方法。

3)通過--plugins傳入自定義插件

寫好報告插件之後,爲了快速演示效果,我們把插件保存到與當前 .air 腳本同層目錄下,並且按住 shift+右鍵 在當前目錄下打開cmd/PowerShell:

不傳入我們自定義的插件生成的報告,touch步驟依舊是按舊插件的內容顯示:

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

傳入我們自定義的插件,步驟標題就會按照我們自定義的插件內容來顯示了:

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

可以看到步驟標題上,原本的 Touch 已經被替換成了 Click

4. 拓展:報告插件加載的原理

關於報告插件加載的原理,我們可以直接看源碼: https://github.com/AirtestProject/Airtest/blob/master/airtest/report/report.py#L73-L82

Airtest使用了python的 __import__ 來嘗試導入插件模塊,比如poco中的airtest報告插件,在 import 的時候是 import poco.utils.airtest.report ,因此在命令行中我們用了--plugins poco.utils.airtest.report 來導入。

如果想要在非當前目錄下導入自定義的報告插件,直接傳入路徑是不行的,比如試圖傳入 --plugins d:\test\report\new_report.py 這樣的參數,會發現無法加載成功。

我們可以開個python終端試一下:

>>> __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'

但是如果嘗試把文件所在路徑加入到系統PATH中,sys.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()

這時候再去打開生成的html文件,就發現成功生效了,可以看到剛纔執行完 LogToHtml 之後airtest自動打了一條加載插件的log出來,說明成功加載到了。
注意plugins的參數是一個list,因爲支持多個插件傳入

5. 小結

瞭解瞭如何 自定義插件 並且知道了 插件的加載原理 之後,我們就可以着手“定製”自己的Airtest報告了。

舉個例子,我們在進行一些控件點擊的時候,通常會使用如下的腳本:

poco(text="網易雲音樂").click()

假設我們自定義了1個poco插件,可以讓這條腳本的步驟標題顯示成“點擊控件:網易雲音樂”,會不會比統一的Poco Click看着要更清晰明瞭一些呢?

當然,這種定製款的Airtest報告就有待同學們去深入挖掘啦,畢竟每位同學的需求或者閱讀習慣都是不一樣的,希望大家早日“定製”出讓自己滿意的Airtest報告。


Airtest官網https://airtest.netease.com/
Airtest教程官網https://airtest.doc.io.netease.com/
搭建企業私有云服務https://airlab.163.com/b2b

官方答疑 Q 羣:654700783

呀,這麼認真都看到這裏啦,幫忙點個推薦支持一下唄,灰常感謝~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章