Seleniumテストフレームワーク統合
Selenium は unittest や pytest などの複数のテストフレームワークと統合でき、より構造化されたテストコード管理とテストレポート生成を実現できます。
とunittest統合
unittestは Python 標準ライブラリのテストフレームワークで、豊富なアサーションメソッドとテスト整理の方法を提供します。
私たちはSeleniumをunittest統合して、構造化されたテストケースを作成できます。
基本構造
以下は、Selenium とunittestを統合した簡単な例です:
例
import unittest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
class TestGoogleSearch(unittest.TestCase):
def setUp(self):
# 正しいドライバのパスを設定する
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
self.driver.get("https://www.baidu.com")
def test_search(self):
search_box = self.driver.find_element(By.NAME, "wd")
search_box.send_keys("Selenium")
search_box.submit()
self.assertIn("Selenium", self.driver.title)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
class TestGoogleSearch(unittest.TestCase):
def setUp(self):
# 正しいドライバのパスを設定する
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
self.driver.get("https://www.baidu.com")
def test_search(self):
search_box = self.driver.find_element(By.NAME, "wd")
search_box.send_keys("Selenium")
search_box.submit()
self.assertIn("Selenium", self.driver.title)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
コード解析
setUpメソッド:各テストケースの実行前に実行され、テスト環境を初期化します。ここでは、ブラウザを起動してGoogleのホームページを開きます。test_searchメソッド:これはGoogleの検索機能をテストするためのテストケースです。検索ボックスを見つけて「Selenium」と入力し、検索を送信します。最後に、ページのタイトルに「Selenium」が含まれているかどうかをアサートします。tearDownメソッド:各テストケースの実行後に実行され、テスト環境をクリーンアップします。ここでは、ブラウザを閉じます。
テストの実行
次のコマンドでテストを実行できます:
python -m unittest test_baidu_search.py
とpytest統合
pytestは、もう1つの人気のあるPythonテストフレームワークで、より簡潔な構文とより強力な機能を提供します。Seleniumをpytest統合して、より柔軟なテストケースを作成できます。
基本構造
以下は、Selenium とpytestを統合した簡単な例です:
例
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
@pytest.fixture
def browser():
# 正しいドライバのパスを設定する
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
yield driver
driver.quit()
def test_baidu_search(browser):
browser.get("https://www.baidu.com")
search_box = browser.find_element(By.NAME, "wd")
search_box.send_keys("Selenium")
search_box.submit()
assert "Selenium" in browser.title
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
@pytest.fixture
def browser():
# 正しいドライバのパスを設定する
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
yield driver
driver.quit()
def test_baidu_search(browser):
browser.get("https://www.baidu.com")
search_box = browser.find_element(By.NAME, "wd")
search_box.send_keys("Selenium")
search_box.submit()
assert "Selenium" in browser.title
コード解析
browserフィクスチャ:これはpytestフィクスチャで、各テストケースの実行前にブラウザを起動し、テストケースの実行後にブラウザを閉じるために使用します。test_baidu_search関数:これはGoogleの検索機能をテストするテストケースです。検索ボックスを見つけて「Selenium」と入力し、検索を送信します。最後に、ページのタイトルに「Selenium」が含まれているかどうかをアサートします。
テストの実行
次のコマンドでテストを実行できます:
pytest test_baidu_search.py
テストレポートの生成
自動テストでは、テストレポートの生成は非常に重要です。私たちはpytest-htmlプラグインを使用してHTML形式のテストレポートを生成できます。
インストールpytest-html
次のコマンドでpytest-htmlプラグインをインストールできます:
例
pip install pytest-html
テストレポートの生成
テスト実行時に、次のコマンドでHTML形式のテストレポートを生成できます:
pytest --html=report.html
生成されたreport.htmlファイルには、合格したテストケース、失敗したテストケース、エラー情報など、テストの詳細な結果が含まれます。
テストコード
Selenium 4 は、相対ロケータ、改善されたウィンドウ管理、より優れた DevTools サポートなど、多くの新機能を導入しました:
例
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
def test_baidu_search():
# 正しいドライバのパスを設定する
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# 百度(Baidu)のホームページを開く
driver.get("https://www.baidu.com")
# 検索ボックスを特定し、キーワードを入力する
search_box = driver.find_element(By.NAME, "wd") # 百度の検索ボックスのnameは「wd」です
search_box.send_keys("Selenium")
# 検索ボタンを特定してクリックする
search_button = driver.find_element(By.ID, "su") # 百度の検索ボタンのidは「su」です
search_button.click()
# ページの読み込みが完了するまで待機します(単純な待機です。実際のプロジェクトでは明示的な待機を使用することをお勧めします)
driver.implicitly_wait(5)
# 検索結果ページにキーワードが含まれているかどうかをアサートする
assert "Selenium" in driver.page_source
# ブラウザを閉じる
driver.quit()
# テストを実行する
if __name__ == "__main__":
test_baidu_search()
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
def test_baidu_search():
# 正しいドライバのパスを設定する
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# 百度(Baidu)のホームページを開く
driver.get("https://www.baidu.com")
# 検索ボックスを特定し、キーワードを入力する
search_box = driver.find_element(By.NAME, "wd") # 百度の検索ボックスのnameは「wd」です
search_box.send_keys("Selenium")
# 検索ボタンを特定してクリックする
search_button = driver.find_element(By.ID, "su") # 百度の検索ボタンのidは「su」です
search_button.click()
# ページの読み込みが完了するまで待機します(単純な待機です。実際のプロジェクトでは明示的な待機を使用することをお勧めします)
driver.implicitly_wait(5)
# 検索結果ページにキーワードが含まれているかどうかをアサートする
assert "Selenium" in driver.page_source
# ブラウザを閉じる
driver.quit()
# テストを実行する
if __name__ == "__main__":
test_baidu_search()