零基礎學會用Airtest-Selenium對Firefox進行自動化測試

1. 前言

本文將詳細介紹如何使用AirtestIDE驅動Firefox測試,以及脫離AirtestIDE怎麼驅動Firefox(VScode爲例)。

看完本文零基礎小白也能學會Firefox瀏覽器自動化測試!!!

2. 如何使用AirtestIDE驅動Firefox瀏覽器

對於Web自動化測試,目前AirtestIDE支持chrome瀏覽器和Firefox2種瀏覽器,關於如何驅動chrome瀏覽器,之前我們的推文已經做了很多詳細介紹:

今天我們重點聊一下,如何使用AirtestIDE驅動Firefox瀏覽器。

1)基礎知識

AirtestIDE內置python環境,環境裏面裝好了Airtest-Selenium庫,是基於Selenium封裝的。

GeckoDriver是與Firefox瀏覽器配合使用的驅動程序,安裝GeckoDriver需要注意兩者的版本兼容,請查看 https://github.com/mozilla/geckodriver/releases

詳細步驟參考下文:

2)下載GeckoDriver(注意選擇對應操作系統的版本)

3)解壓獲得geckodriver.exe

解壓剛纔下載的文件,得到geckodriver.exe 。

4)給geckodriver.exe設置爲系統環境變量

把geckodriver.exe放到你電腦某個目錄下,之後給它設置系統環境變量:

5)在AirtestIDE驅動Firefox做簡單的自動化測試

# -*- encoding=utf8 -*-

from airtest.core.api import *
from airtest.cli.parser import cli_setup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from airtest_selenium.proxy import WebFirefox
import time

#創建Chrome瀏覽器驅動實例
browser = WebFirefox(firefox_binary=r"C:\Program Files\MozillaFirefox\firefox.exe")

# 訪問網頁
browser.get('https://www.baidu.com')

#打印頁面標題
print(browser.title)

#休眠3秒
time.sleep(3)

#關閉瀏覽器
browser.quit()
① 版本說明

【AirtestIDE】1.2.15:https://airtest.netease.com/changelog.html

【python】3.6.5:AirtestIDE內置

【Firefox】113.0.2 (64 位)

【geckodriver】0.33.0(win64):https://github.com/mozilla/geckodriver/releases/tag/v0.33.0

② 啓動報錯

在運行上述demo時,一開始遇到了一個類似這樣的報錯:

selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

查詢後發現,是找不到Firefox可執行文件的意思,於是在創建WebFirefox時,將firefox.exe可執行文件路徑當做參數傳遞給WebFirefox,之後就可以正常運行了。

3. 如何脫離AirtestIDE驅動Firefox(vscode爲例)

1)前置條件:安裝VScode、配置python環境

2)python配置airtest、airtest-selenium

可參考:如何部署本地python環境

使用命令如下:

pip install airtest

pip install airtest-selenium

小編的pip版本太舊了,還需更新pip,使用命令:python -m pip install --upgrade pip

3)在cmd控制檯測試環境是否配置成功

4)測試腳本demo,解決報錯

① No module named pynput

缺少 pynput庫,直接 pip install安裝上即可。

② TypeError: init() got an unexpected keyword argument......

Ctrl+鼠標左鍵到proxy.py文件;

出現 argument 'timeout'報錯的話刪掉timeout=timeout的參數;

出現 argument 'firefox_options'報錯的話刪掉firefox_options=firefox_options的參數。

5)脫離AirtestIDE驅動Firefox

4. 小結

今天關於驅動Firefox的教程就到這裏啦,更多關於Airtest想看的內容,可以給我們評論/留言。


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

官方答疑 Q 羣:117973773

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

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