1. 前言
很多同學選擇使用airtest-selenium來做Web自動化,是因為想使用airtest-selenium封裝的一些關於圖像識別的方法,像圖像點擊、圖像斷言、截圖等等。
但是在實際應用過程中,同學們可能會遇到一些問題,這里我們詳細聊一聊同學們在使用airtest-selenium進行Web自動化測試時,容易遇到的一些問題及其解決辦法。
2. 基本概念和用法
1)airtest-selenium是標准的selenium
其實airtest-selenium就是標准的selenium,只不過我們在這個庫里額外封裝了一些關於圖像識別和標簽頁切換的方法:
airtest_touch
:圖像點擊assert_template
:斷言圖像存在snapshot
:給網頁截圖switch_to_previous_tab
:切換到上一個打開的標簽頁switch_to_new_tab
:切換到最新打開的標簽頁- ......
2)使用AirtestIDE提供的輔助窗
為了讓同學們快速上手,我們在AirtestIDE提供了對應的輔助窗,默認情況下不展示。
但我們需要編寫Web自動化測試腳本時,可以在IDE的頂部菜單欄,窗口選項下勾選上Selenium Window:
使用這些快捷功能,可以幫我們錄制/快速生產Web自動化測試腳本。不過需要注意的是,為了使用輔助窗的錄制/快捷功能,我們需要點擊 初始化按鈕
打開1個chrome瀏覽器窗口,並且在該窗口上錄制腳本。
關於airtest-selenium的詳細入門教程,可以參考我們的往期推文:
3)結合更多selenium的API
另外,我們非常建議大家先學習一些selenium基礎,再來使用airtest-selenium進行Web自動化的測試工作。
這樣我們就可以結合selenium更加豐富的API實現更加豐富和復雜的自動化測試腳本。(網上有非常豐富的關於selenium的教程文檔,同學們可以自行查找)
3. 常見問題
1)使用airtest-selenium封裝的圖像方法報錯
有些同學在使用airtest-selenium封裝的一些圖像方法時,出現類似 name 'Template' is not defined
的報錯:
通常是因為忘記/誤刪了引入Airtest庫的方法,我們可以在腳本開頭添加:
from airtest.core.api import *
2)混淆airtest-selenium的WebChrome()和selenium的webdriver.Chrome()
在airtest-selenium庫里,我們可以通過這樣的方式實例化一個chrome對象:
from airtest_selenium.proxy import WebChrome
driver = WebChrome()
而在selenium庫里,我們則是通過下述方式對chrome進行實例化的:
from selenium import webdriver
driver = webdriver.Chrome()
如果我們要使用airtest-selenium封裝的圖像方法,像 airtest_touch
、snapshot
和assert_template
等,我們就必須實例化airtest-selenium提供的 WebChrome
類。
否則會出現類似 AttributeError: 'WebDriver' object has no attribute 'snapshot'
的報錯:
3)暫不支持在Mac上使用airtest-selenium
目前暫不支持在Mac上使用airtest-selenium,使用時我們也會彈出如下的提示:
Current OS is not 'Windows'! You can't use airtest function of Airtest-Selenium. > <
4)airtest-selenium報告插件
在生成airtest-selenium(Web自動化測試)的報告時,我們需要加載專用的報告插件。
如果我們使用AirtestIDE的 查看報告
按鈕來生成,會自動幫我們加載這個插件:
# 命令行添加airtest-selenium報告插件的方式
--plugin airtest_selenium.report
如果我們使用腳本生成測試報告,則需要手動指定 plugins
參數:
from airtest.report.report import LogToHtml
h1 = LogToHtml(script_root=r'D:\test\report01.air', log_root=r"D:\test\report01.air\log", export_dir=r"D:\test\report02" , lang='en', plugins=["airtest_selenium.report"])
h1.report()
5)unknown error: cannot find Chrome binary
這是同學們在使用airtest-selenium進行Web測試 最常問 的一個問題了。通常情況是,我們在AirtestIDE的Selenium Window輔助窗中,點擊打開瀏覽器的按鈕,可以正常打開一個chrome瀏覽器的窗口(因為我們事先在選項-設置
中設置了chrome.exe的路徑):
但是在執行airtest-selenium腳本,運行到打開瀏覽器窗口的代碼時,卻會報類似下述的報錯:
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
(Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.19043 x86_64)
如果我們的chrome瀏覽器是安裝在默認路徑下的(我們也非常建議將chrome瀏覽器安裝到默認路徑下,可以省去很多問題),出現上述報錯時,我們需要檢查以下2點:
- chrome瀏覽器匹配的驅動(chromedriver)有沒有放在環境變量目錄位置上
- chrome瀏覽器版本與驅動(chromedriver)版本是否匹配
如果沒有放在正確目錄下或者與現有瀏覽器版本不匹配,我們可以在查看安裝好的chrome瀏覽器版本后,到這個路徑下下載匹配的chromedriver:http://chromedriver.storage.googleapis.com/index.html 。
如果我們的chrome瀏覽器沒有安裝在默認路徑下,也很有可能因為Selenium找不到二進制的chrome.exe文件而拋出上述的異常,通常我們可以選擇以下方式來解決:
- 卸載已安裝的chrome瀏覽器,然后重新安裝到默認路徑下
- 不重新安裝的話,可以直接配置chrome瀏覽器的安裝目錄到環境變量中
當然我們也支持使用使用代碼來指定chrome.exe或者chromedriver(2者的版本必須匹配)的路徑:
# -*- encoding=utf8 -*-
__author__ = "AirtestProject"
from airtest.core.api import *
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from airtest_selenium.proxy import WebChrome
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
driver = WebChrome(options=opt,executable_path=r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
6)脫離AirtestIDE運行airtest-selenium腳本時未安裝庫
如果我們使用AirtestIDE自帶的環境來運行Web自動化腳本,則直接運行即可,因為內嵌的python環境已經事先安裝好了airtest-selenium庫。
但是如果我們指定使用本地python環境或者脫離AirtestIDE運行Web自動化腳本,則需要我們在對應的python環境里安裝好airtest-selenium庫:
pip install airtest-selenium
否則會出現 no module named 'airtest_selenium'
的報錯:
另外,也有些同學在安裝這個庫時,可能會出現 no module named 'pynput'
的報錯,此時我們需要先安裝好 pynput
庫,再來安裝airtest-selenium:
pip install pynput
pip install airtest-selenium
7)Web自動化的輸入與鍵盤操作
與Android、iOS、Windows平台不一樣的是,在Web自動化腳本中,我們並非使用 text
進行文本輸入操作,也不是使用 keyevent
模擬鍵盤操作。
而是使用 send_keys
,以下是一個簡單的例子,我們打開百度首頁后,定位到搜索文本輸入框,輸入一定的文本,然后執行鍵盤回車操作:
# -*- encoding=utf8 -*-
__author__ = "AirtestProject"
from airtest.core.api import *
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from airtest_selenium.proxy import WebChrome
driver = WebChrome()
driver.implicitly_wait(20)
auto_setup(__file__)
driver.get("https://www.baidu.com/")
sleep(1.0)
# 輸入文本
driver.find_element_by_id("kw").send_keys("abc")
# 鍵盤回車操作
driver.find_element_by_id("kw").send_keys(Keys.ENTER)
Airtest官網:https://airtest.netease.com/
Airtest教程官網:https://airtest.doc.io.netease.com/
搭建企業私有雲服務:https://airlab.163.com/b2b
官方答疑 Q 群:654700783
呀,這么認真都看到這里啦,幫忙點個推薦支持一下唄,灰常感謝~