1. 程式人生 > 其它 >聊一聊使用airtest-selenium做Web自動化的常見問題

聊一聊使用airtest-selenium做Web自動化的常見問題

很多同學選擇使用airtest-selenium來做Web自動化,是因為想使用airtest-selenium封裝的一些關於影象識別的方法,像影象點選、影象斷言、截圖等等。 但是在實際應用過程中,同學們可能會遇到一些問題,這裡我們詳細聊一聊同學們在使用airtest-selenium進行Web自動化測試時,容易遇到的一些問題及其解決辦法。

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_touchsnapshotassert_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

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