如何用Python实现Selenium WebDriver多场景登录测试用例?
Hey there! Since you've already nailed CASE-1 (valid username/password login), let's dive into building out the rest of those login test scenarios using Python and Selenium WebDriver. I'll use pytest for test organization—it's clean, scalable, and widely used in the Python testing space.
Prerequisites
First, make sure you've got these installed:
selenium(for WebDriver interactions)pytest(for test running and fixtures)- A WebDriver executable matching your browser (e.g., ChromeDriver for Chrome, GeckoDriver for Firefox)
You can install the packages via pip:
pip install selenium pytest
Test Setup
Let's start with a base fixture to handle WebDriver initialization and cleanup—this avoids repeating code across test cases:
import pytest from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC @pytest.fixture(scope="function") def driver(): # Initialize ChromeDriver (swap with Firefox() if you prefer) driver = webdriver.Chrome() driver.maximize_window() # Navigate to your login page—replace with your actual URL driver.get("https://your-login-page-url.com") yield driver # Cleanup: close the browser after each test driver.quit() # Helper function to get login elements (adjust selectors to match your page!) def get_login_elements(driver): return { "email_input": driver.find_element(By.ID, "email"), # Replace with your email field selector "password_input": driver.find_element(By.ID, "password"), # Replace with your password field selector "login_button": driver.find_element(By.ID, "login-btn"), # Replace with your login button selector "error_message": WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CLASS_NAME, "error-message")) # Replace with your error message selector ) }
Test Cases Breakdown
Now let's implement each test case one by one. Note: You'll need to adjust element selectors (like IDs, class names) to match your actual login page's HTML.
CASE-2: Both Email and Password Fields Are Empty
def test_login_empty_credentials(driver): elements = get_login_elements(driver) # Click login without entering anything elements["login_button"].click() # Verify the expected error message (update this text to match your site's message) assert "Please enter your email and password" in elements["error_message"].text
CASE-3: Email Filled, Password Empty
def test_login_empty_password(driver): elements = get_login_elements(driver) # Enter valid email (you can use a test email here) elements["email_input"].send_keys("test@example.com") # Click login without entering password elements["login_button"].click() # Verify error message for empty password assert "Please enter your password" in elements["error_message"].text
CASE-4: Email Empty, Password Filled
def test_login_empty_email(driver): elements = get_login_elements(driver) # Enter valid password (use a test password here) elements["password_input"].send_keys("ValidPass123!") # Click login without entering email elements["login_button"].click() # Verify error message for empty email assert "Please enter your email" in elements["error_message"].text
CASE-5: Both Email and Password Are Invalid
def test_login_invalid_both_credentials(driver): elements = get_login_elements(driver) # Enter invalid email and password elements["email_input"].send_keys("wrong-email@example.com") elements["password_input"].send_keys("WrongPass456!") elements["login_button"].click() # Verify error for invalid credentials assert "Invalid email or password" in elements["error_message"].text
CASE-6: Invalid Email, Valid Password
def test_login_invalid_email_valid_password(driver): elements = get_login_elements(driver) # Enter invalid email but valid password elements["email_input"].send_keys("invalid-email@example.com") elements["password_input"].send_keys("ValidPass123!") elements["login_button"].click() # Verify error message (might be same as CASE-5, depending on your site) assert "Invalid email or password" in elements["error_message"].text
CASE-7: Invalid Email Format (e.g., Missing @ Symbol)
Let's add this common edge case as CASE-7—testing an email that doesn't follow basic format rules:
def test_login_invalid_email_format(driver): elements = get_login_elements(driver) # Enter email without @ symbol elements["email_input"].send_keys("testexample.com") elements["password_input"].send_keys("ValidPass123!") elements["login_button"].click() # Verify format error message assert "Please enter a valid email address" in elements["error_message"].text
Running the Tests
To run all these tests, just open your terminal, navigate to the directory with your test file, and run:
pytest login_tests.py -v
The -v flag gives you verbose output so you can see which tests passed/failed.
Key Notes
- Element Selectors: Always use stable selectors (like IDs or data attributes) instead of XPath or class names that might change.
- Explicit Waits: I used
WebDriverWaitfor the error message to ensure the element loads before we check its text—this prevents flaky tests. - Test Isolation: Each test starts with a fresh browser instance (thanks to the
function-scoped fixture), so one test's actions don't affect another.
内容的提问来源于stack exchange,提问作者klay123tw

