對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
呀,這麼認真都看到這裡啦,幫忙點個推薦支援一下唄,灰常感謝~