Selenium 実践プロジェクト

現代のソフトウェア開発において、自動化テストはソフトウェア品質を確保するための重要な手段の一つとなっています。

Selenium は広く使用されている自動化テストツールで、複数のプログラミング言語とブラウザをサポートし、ユーザー操作をシミュレートして、機能テストやリグレッションテストなどを行うことができます。

本記事では、Selenium を使用して自動化テストを行う方法を詳しく紹介し、実践プロジェクトを通じて、ログイン、登録、検索(百度を例とする)などの一般的な機能の自動化テストを実装する方法をデモンストレーションします。

1. 実践プロジェクト:自動化テストケース

1.1 ログイン機能テスト

ユーザー名とパスワードの入力欄およびログインボタンを含むログインページがあると仮定して、ユーザーのログイン操作をシミュレートする自動化テストスクリプトを作成します。

例

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

chrome_options = Options()
# 正しいドライバーパスを設定
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
driver = webdriver.Chrome(service=service, options=chrome_options)

# ログインページを開く
driver.get("https://example.com/login")

# ユーザー名入力欄を特定してユーザー名を入力
username_input = driver.find_element(By.ID, "username")
username_input.send_keys("testuser")

# パスワード入力欄を特定してパスワードを入力
password_input = driver.find_element(By.ID, "password")
password_input.send_keys("password123")

# ログインボタンを特定してクリック
login_button = driver.find_element(By.ID, "login-button")
login_button.click()

# ログインが成功したか検証
assert "Welcome" in driver.page_source

# ブラウザを閉じる
driver.quit()

1.2 登録機能テスト

次に、ユーザーの登録操作をシミュレートする自動化テストスクリプトを作成します。

例

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

# Chrome オプションを設定
chrome_options = Options()

# 正しいドライバーパスを設定
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
driver = webdriver.Chrome(service=service, options=chrome_options)

# 登録ページを開く
driver.get("https://example.com/register")

# ユーザー名入力欄を特定してユーザー名を入力
username_input = driver.find_element(By.ID, "username")
username_input.send_keys("newuser")

# メールアドレス入力欄を特定してメールアドレスを入力
email_input = driver.find_element(By.ID, "email")
email_input.send_keys("newuser@example.com")

# パスワード入力欄を特定してパスワードを入力
password_input = driver.find_element(By.ID, "password")
password_input.send_keys("newpassword123")

# 確認用パスワード入力欄を特定してパスワードを入力
confirm_password_input = driver.find_element(By.ID, "confirm-password")
confirm_password_input.send_keys("newpassword123")

# 登録ボタンを特定してクリック
register_button = driver.find_element(By.ID, "register-button")
register_button.click()

# 登録が成功したか検証
assert "Registration successful" in driver.page_source

# ブラウザを閉じる
driver.quit()

1.3 検索機能テスト(百度)

最後に、ユーザーが百度の検索ボックスにキーワードを入力して検索する操作をシミュレートする自動化テストスクリプトを作成します。

例

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

# Chrome オプションを設定
chrome_options = Options()

# 正しいドライバーパスを設定
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
driver = webdriver.Chrome(service=service, options=chrome_options)

# 百度のホームページを開く
driver.get("https://www.baidu.com")

# 検索ボックスを特定してキーワードを入力
search_box = driver.find_element(By.NAME, "wd")
search_box.send_keys("サンプル")

# Enterキーを押して検索する操作をシミュレート
search_box.send_keys(Keys.RETURN)

# 検索結果ページにキーワードが含まれているか検証
assert "サンプル" in driver.page_source

# ブラウザを閉じる
driver.quit()
その他の拡張