Python的端到端測試框架SeleniumBase使用解讀
SeleniumBase詳細介紹及用法指南
什么是 SeleniumBase?
SeleniumBase 是一個基于 Python 的端到端測試框架,它構(gòu)建在 Selenium 和 pytest 之上,提供了更簡單、更強大的 Web 自動化測試和爬蟲開發(fā)體驗。
它簡化了 Selenium 的許多復(fù)雜操作,并添加了大量有用的功能。
SeleniumBase 的主要特點
- 簡化語法:比原生 Selenium 更簡潔的 API
- 內(nèi)置等待機制:自動處理元素加載等待
- 豐富的斷言方法:提供多種驗證方式
- 可視化測試:支持實時瀏覽器操作觀察
- 截圖和日志:自動記錄測試過程
- 多瀏覽器支持:Chrome, Firefox, Edge, Safari 等
- 無頭模式:支持無頭瀏覽器測試
- 移動設(shè)備模擬:可以模擬移動設(shè)備測試
- 代理支持:方便使用代理服務(wù)器
- 與 pytest 集成:充分利用 pytest 的強大功能
安裝 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")
# 點擊元素
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")
# 元素斷言
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. 等待機制
SeleniumBase 自動處理大多數(shù)等待場景,但也提供了顯式等待方法:
def test_waiting(self):
self.open("https://www.example.com")
# 等待元素出現(xiàn)
self.wait_for_element("div.loading")
# 等待元素可點擊
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")
# 斷言代碼...
# 運行時使用 --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() # 切換回主文檔測試運行選項
SeleniumBase 測試可以使用 pytest 運行,并支持多種參數(shù):
# 基本運行 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最佳實踐
- 使用 Page Object 模式:將頁面元素和操作封裝成類
- 合理使用等待:優(yōu)先使用 SeleniumBase 的自動等待
- 清晰的測試命名:使測試目的明確
- 適當(dāng)?shù)臄嘌?/strong>:每個測試驗證一個明確的功能點
- 維護測試數(shù)據(jù):使用外部文件或數(shù)據(jù)庫管理測試數(shù)據(jù)
- 定期維護測試:隨著應(yīng)用更新調(diào)整測試用例
- 利用鉤子和固件:使用 pytest 的固件功能減少重復(fù)代碼
總結(jié)
SeleniumBase 是一個功能強大且易于使用的測試框架,它簡化了 Selenium 的復(fù)雜性,同時提供了豐富的功能。無論是簡單的網(wǎng)站測試還是復(fù)雜的 Web 應(yīng)用程序測試,SeleniumBase 都能提供高效的解決方案。通過結(jié)合 pytest 的強大功能,它可以滿足從簡單到復(fù)雜的所有測試需求。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python機器學(xué)習(xí)應(yīng)用之基于決策樹算法的分類預(yù)測篇
所謂決策樹,就是一個類似于流程圖的樹形結(jié)構(gòu),樹內(nèi)部的每一個節(jié)點代表的是對一個特征的測試,樹的分支代表該特征的每一個測試結(jié)果,而樹的每一個葉子節(jié)點代表一個類別。樹的最高層是就是根節(jié)點2022-01-01
Python爬蟲爬取新浪微博內(nèi)容示例【基于代理IP】
這篇文章主要介紹了Python爬蟲爬取新浪微博內(nèi)容,結(jié)合實例形式分析了Python基于代理IP實現(xiàn)的微博爬取與抓包分析相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
Python實現(xiàn)批量讀取圖片并存入mongodb數(shù)據(jù)庫的方法示例
這篇文章主要介紹了Python實現(xiàn)批量讀取圖片并存入mongodb數(shù)據(jù)庫的方法,涉及Python文件讀取及數(shù)據(jù)庫寫入相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
如何使用VSCode愉快的寫Python于調(diào)試配置步驟
從我的使用經(jīng)驗出發(fā),可以說VSCode用來寫Python真的是再合適不過了,你將體驗到絲滑的編程體驗和無限擴展的可能。而且,如果你的項目是包含多種語言的,比如Web開發(fā),你不必再開多個編輯器和其他工具,因為這一切都可以在VSCode里完成了2018-04-04

