如何正確使用Airtest報告插件?報告小tips上線

1. 前言

在使用Airtest做自動化測試時,默認生成的報告,其實是airtest的專屬報告。

它對於poco語句(控件測試場景)、airtest-selenium語句(web測試場景)的支持不夠完善,因此我們需要用 插件的形式 來補充支持poco語句和airtest-selenium語句。

Airtest的報告插件,目前有2個:

  • 用於支持poco語句的,poco.utils.airtest.report
  • 用於支持airtest-selenium語句的,airtest_selenium.report

2. Airtest報告出現一個未知的record_ui

我們從新手同學的一個常見問題中,來具體看下我們報告插件的作用:

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

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

LOG_PA = r"D:\report\log"

auto_setup(__file__, logdir=LOG_PA, devices=["android://127.0.0.1:5037/QV720N"])

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

poco("信息功能").click()

simple_report(__file__,logpath=LOG_PA,output=r"D:\report\poco_report.html")

這是一個非常簡單的純py腳本,運行腳本之後,也順利生成了測試報告,但是這個測試報告顯示的步驟,卻不是我們所期望的那樣。理論上,這裏應該只有一個poco點擊的步驟,但實際上是有2個步驟,其中一個record_iu,還是我們不熟知的步驟名:

其實,這裏是因爲在生成包含poco語句的報告時,沒有帶上支持poco語句的報告插件的原因,我們可以把這個插件在生成報告時,加上看下:

# 換一種帶插件的報告生成方式
h1 = LogToHtml(script_root=__file__, log_root=LOG_PA, lang='zh', plugins=['poco.utils.airtest.report'])
h1.report(output_file=r"D:\report\poco_report2.html")

可以看到,我們的報告就能正常顯示poco點擊的步驟了。

3. airtest-selenium報告不顯示圖片

還有一個常見問題是在生成包含airtest-selenium語句的報告時,出現步驟沒有截圖的情況:

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

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

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from airtest_selenium.proxy import WebChrome
driver = WebChrome()
driver.implicitly_wait(20)

LOG_PA = r"D:\report\log2"

auto_setup(__file__, logdir=LOG_PA)

driver.get("https://www.baidu.com/")
driver.snapshot()

simple_report(__file__,logpath=LOG_PA,output=os.path.join(LOG_PA, "web_report.html"))

這個問題,也是因爲沒有加上支持airtest-selenium語句的報告插件:

# 換一種帶插件的報告生成方式
h1 = LogToHtml(script_root=__file__, log_root=LOG_PA, lang='zh', plugins=['airtest_selenium.report'])
h1.report(output_file=os.path.join(LOG_PA, "web_report2.html"))

可以看到,這時候我們步驟裏的截圖,就可以正常顯示出來了。

PS:airtest-selenium的html格式的報告,需要跟log截圖與log.txt在同級目錄下,截圖才能正常顯示。

4. 如何添加我們的報告插件

簡單瞭解過Airtest報告插件的作用之後,我們可以看下,在腳本生成報告以及命令行生成報告時,我們都是如何添加報告插件的。

1)如何在腳本里添加報告插件

Airtest給我們提供了2種腳本生成測試報告的方式:

  • 簡化參數的方式,simple_report
  • 完整參數的方式,LogToHtml

只有在完整參數的方式,即使用LogToHtml時,我們才能加入報告的插件參數plugins

# 生成包含poco腳本的Airtest報告時
h1 = LogToHtml(script_root=__file__, log_root=LOG_PA, lang='zh', plugins=['poco.utils.airtest.report'])
h1.report(output_file=r"D:\report\poco_report2.html")

# 生成包含airtest-selenium腳本的Airtest報告時
h1 = LogToHtml(script_root=__file__, log_root=LOG_PA, lang='zh', plugins=['airtest_selenium.report'])
h1.report(output_file=r"D:\report\web_report2.html")
2)如何在命令行裏添加報告插件

命令行運行腳本和生成報告是單獨的2條命令,所以我們在執行完腳本運行的命令之後,就可以使用生成報告的命令來生成html格式的報告了,命令行添加報告插件的方式也非常簡單,加上--plugins即可:

# 生成包含poco腳本的Airtest報告時
airtest report "D:/demo/test111.py" --log_root "D:/report/log2" --lang "zh" --outfile "D:/report/poco_report2.html" --plugins "poco.utils.airtest.report"

# 生成包含airtest-selenium腳本的Airtest報告時
airtest report "D:/demo/test222.py" --log_root "D:/report/log2" --lang "zh" --outfile "D:/report/selenium_report.html" --plugins "airtest_selenium.report"
3)補充:使用AirtestIDE自帶的查看報告功能

AirtestIDE的快捷菜單欄中,有一個查看報告的功能,使用這種方式生成的Airtest報告,會默認帶上插件,無需我們特別關注。

Generating HTML log:

D:\AirtestIDE\AirtestIDE reporter D:\test\untitled.air --log_root D:/log\6fe87b11c --outfile D:\log\6fe87b11c\log.html --static_root D:\AirtestIDE\airtest\report --lang zh --plugin airtest_selenium.report poco.utils.airtest.report

5. 小結

那關於報告插件的內容就到這裏啦,下次生成帶有poco語句或者airtest-selenium語句的Airtest報告時,別忘了檢查下是否添加了報告插件哈~


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

官方答疑 Q 羣:117973773

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

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