Python的端到端測試框架SeleniumBase使用解讀
SeleniumBase詳細(xì)介紹及用法指南
什么是 SeleniumBase?
SeleniumBase 是一個基于 Python 的端到端測試框架,它構(gòu)建在 Selenium 和 pytest 之上,提供了更簡單、更強(qiáng)大的 Web 自動化測試和爬蟲開發(fā)體驗(yàn)。
它簡化了 Selenium 的許多復(fù)雜操作,并添加了大量有用的功能。
SeleniumBase 的主要特點(diǎn)
- 簡化語法:比原生 Selenium 更簡潔的 API
- 內(nèi)置等待機(jī)制:自動處理元素加載等待
- 豐富的斷言方法:提供多種驗(yàn)證方式
- 可視化測試:支持實(shí)時瀏覽器操作觀察
- 截圖和日志:自動記錄測試過程
- 多瀏覽器支持:Chrome, Firefox, Edge, Safari 等
- 無頭模式:支持無頭瀏覽器測試
- 移動設(shè)備模擬:可以模擬移動設(shè)備測試
- 代理支持:方便使用代理服務(wù)器
- 與 pytest 集成:充分利用 pytest 的強(qiáng)大功能
安裝 SeleniumBase
pip install seleniumbase
基本用法
1. 簡單測試示例
from seleniumbase import BaseCase
class MyTestClass(BaseCase):
def test_basic(self):
self.open("https://www.example.com")
self.assert_title("Example Domain")
self.assert_element("div h1")
self.type("input[name='q']", "SeleniumBase\n")
self.assert_text("Results for", "h3")2. 元素定位與操作
SeleniumBase 提供了多種元素定位和操作方法:
def test_element_operations(self):
self.open("https://www.example.com")
# 點(diǎn)擊元素
self.click("button#submit")
# 輸入文本
self.type("input#username", "testuser")
# 清除輸入框
self.clear("input#username")
# 獲取元素文本
text = self.get_text("h1")
# 獲取元素屬性
attr = self.get_attribute("img#logo", "src")
# 檢查元素是否存在
self.assert_element("div.container")
# 檢查元素是否可見
self.assert_element_visible("div.message")3. 斷言方法
SeleniumBase 提供了豐富的斷言方法:
def test_assertions(self):
self.open("https://www.example.com")
# 標(biāo)題斷言
self.assert_title("Example Domain")
self.assert_title_contains("Example")
# 文本斷言
self.assert_text("Example Domain", "h1")
self.assert_exact_text("Example Domain", "h1")
# URL 斷言
self.assert_url("https://www.example.com/")
self.assert_url_contains("example.com")
# 元素?cái)嘌?
self.assert_element("div h1")
self.assert_element_present("div h1")
self.assert_element_absent("div.nonexistent")
# 其他斷言
self.assert_true(1 + 1 == 2)
self.assert_false(1 + 1 == 3)4. 等待機(jī)制
SeleniumBase 自動處理大多數(shù)等待場景,但也提供了顯式等待方法:
def test_waiting(self):
self.open("https://www.example.com")
# 等待元素出現(xiàn)
self.wait_for_element("div.loading")
# 等待元素可點(diǎn)擊
self.wait_for_element_clickable("button.submit")
# 等待文本出現(xiàn)
self.wait_for_text("Welcome back", "h2")
# 自定義等待時間
self.wait_for_element("div.result", timeout=20)
# 等待元素消失
self.wait_for_element_absent("div.loading")高級功能
1. 無頭模式測試
def test_headless(self):
self.open("https://www.example.com")
# 斷言代碼...
# 運(yùn)行時使用 --headless 參數(shù)
# pytest test_file.py --headless2. 截圖和日志
def test_screenshot(self):
self.open("https://www.example.com")
self.save_screenshot("example.png")
self.save_screenshot_to_logs() # 保存到日志目錄3. 移動設(shè)備模擬
def test_mobile_emulation(self):
mobile_emulation = {
"deviceMetrics": {"width": 360, "height": 640, "pixelRatio": 3.0},
"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"
}
self.open_with_options("https://www.example.com", mobile_emulation=mobile_emulation)4. 使用代理
def test_with_proxy(self):
proxy_string = "127.0.0.1:8080"
self.open_with_proxy("https://www.example.com", proxy_string)5. iframe 操作
def test_iframe(self):
self.open("https://www.example.com")
self.switch_to_frame("iframe#content")
# 在 iframe 中操作元素
self.click("button.submit")
self.switch_to_default_content() # 切換回主文檔測試運(yùn)行選項(xiàng)
SeleniumBase 測試可以使用 pytest 運(yùn)行,并支持多種參數(shù):
# 基本運(yùn)行 pytest test_file.py # 無頭模式 pytest test_file.py --headless # 指定瀏覽器 pytest test_file.py --browser=firefox # 慢動作模式(便于觀察) pytest test_file.py --demo_mode # 保存失敗的測試截圖 pytest test_file.py --screenshot_on_failure # 并行測試 pytest test_file.py -n 4
與 CI/CD 集成
SeleniumBase 測試可以輕松集成到 CI/CD 流程中。例如,在 GitHub Actions 中的配置示例:
name: SeleniumBase Tests
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install seleniumbase
- name: Run tests
run: |
pytest tests/ --headless --browser=chrome最佳實(shí)踐
- 使用 Page Object 模式:將頁面元素和操作封裝成類
- 合理使用等待:優(yōu)先使用 SeleniumBase 的自動等待
- 清晰的測試命名:使測試目的明確
- 適當(dāng)?shù)臄嘌?/strong>:每個測試驗(yàn)證一個明確的功能點(diǎn)
- 維護(hù)測試數(shù)據(jù):使用外部文件或數(shù)據(jù)庫管理測試數(shù)據(jù)
- 定期維護(hù)測試:隨著應(yīng)用更新調(diào)整測試用例
- 利用鉤子和固件:使用 pytest 的固件功能減少重復(fù)代碼
總結(jié)
SeleniumBase 是一個功能強(qiáng)大且易于使用的測試框架,它簡化了 Selenium 的復(fù)雜性,同時提供了豐富的功能。無論是簡單的網(wǎng)站測試還是復(fù)雜的 Web 應(yīng)用程序測試,SeleniumBase 都能提供高效的解決方案。通過結(jié)合 pytest 的強(qiáng)大功能,它可以滿足從簡單到復(fù)雜的所有測試需求。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)自動計(jì)算特定格式的時間差
這篇文章主要介紹了利用Python實(shí)現(xiàn)在輸入一個特定格式的時間后,自動獲取前進(jìn)或者后退多少小時之后的時間。感興趣的朋友可以了解一下2021-12-12
Python基于matplotlib實(shí)現(xiàn)繪制三維圖形功能示例
這篇文章主要介紹了Python基于matplotlib實(shí)現(xiàn)繪制三維圖形功能,涉及Python使用matplotlib模塊進(jìn)行三維圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
Python標(biāo)準(zhǔn)庫shutil用法實(shí)例詳解
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫shutil用法,結(jié)合實(shí)例形式分析了shutil庫針對文件與文件夾各種常見操作技巧與相關(guān)使用注意事項(xiàng),需要的朋友可以參考下2018-08-08
python3 讀取Excel表格中的數(shù)據(jù)
這篇文章主要介紹了python3 讀取Excel表格中的數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2018-10-10
python 進(jìn)程池的兩種不同實(shí)現(xiàn)方法示例
這篇文章主要為大家介紹了python 進(jìn)程池的兩種不同實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Django初步使用Celery處理耗時任務(wù)和定時任務(wù)問題
這篇文章主要介紹了Django初步使用Celery處理耗時任務(wù)和定時任務(wù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
基于Python實(shí)現(xiàn)主機(jī)遠(yuǎn)程控制
這篇文章主要介紹了基于Python實(shí)現(xiàn)主機(jī)遠(yuǎn)程控制,本文為?HITwh?網(wǎng)絡(luò)空間安全專業(yè)網(wǎng)絡(luò)空間安全設(shè)計(jì)與實(shí)踐選題,主要實(shí)現(xiàn)了遠(yuǎn)程監(jiān)控局域網(wǎng)內(nèi)的主機(jī)桌面與網(wǎng)絡(luò)情況、簡單鍵鼠控制、遠(yuǎn)程斷網(wǎng)(ARP?攻擊)、數(shù)據(jù)加密傳輸?shù)裙δ?,下面來看看具體實(shí)現(xiàn)過程吧2022-01-01

