0
0
Selenium Pythontesting~15 mins

Parameterized tests in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality with multiple user credentials
Preconditions (5)
Step 1: Open the login page URL.
Step 2: Enter the email address from the test data into the email input field with id 'email'.
Step 3: Enter the password from the test data into the password input field with id 'password'.
Step 4: Click the login button with id 'loginBtn'.
Step 5: Wait for the page to load and verify the URL is 'https://example.com/dashboard'.
Step 6: Log out to return to the login page before the next test iteration.
✅ Expected Result: For each set of credentials, the user is successfully logged in and redirected to the dashboard page.
Automation Requirements - pytest with Selenium WebDriver
Assertions Needed:
Verify the current URL after login matches 'https://example.com/dashboard'.
Best Practices:
Use pytest parameterize decorator to run the test with multiple data sets.
Use explicit waits to wait for page elements or URL changes.
Use Page Object Model to separate page interactions from test logic.
Close the browser after all tests complete.
Automated Solution
Selenium Python
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

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.email_input = (By.ID, 'email')
        self.password_input = (By.ID, 'password')
        self.login_button = (By.ID, 'loginBtn')

    def load(self):
        self.driver.get('https://example.com/login')

    def login(self, email, password):
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.email_input)
        )
        self.driver.find_element(*self.email_input).clear()
        self.driver.find_element(*self.email_input).send_keys(email)
        self.driver.find_element(*self.password_input).clear()
        self.driver.find_element(*self.password_input).send_keys(password)
        self.driver.find_element(*self.login_button).click()

class DashboardPage:
    def __init__(self, driver):
        self.driver = driver

    def is_loaded(self):
        return WebDriverWait(self.driver, 10).until(
            EC.url_to_be('https://example.com/dashboard')
        )

@pytest.fixture(scope='module')
def driver():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()

@pytest.mark.parametrize('email,password', [
    ('user1@example.com', 'Password1!'),
    ('user2@example.com', 'Password2!'),
    ('user3@example.com', 'Password3!')
])
def test_login(driver, email, password):
    login_page = LoginPage(driver)
    dashboard_page = DashboardPage(driver)

    login_page.load()
    login_page.login(email, password)

    assert dashboard_page.is_loaded(), f"Login failed for {email}"

    # Log out to prepare for next test iteration
    driver.get('https://example.com/logout')

This test script uses pytest with Selenium WebDriver to automate login tests with multiple user credentials.

The LoginPage class encapsulates the login page elements and actions, following the Page Object Model pattern for cleaner code.

The DashboardPage class checks if the dashboard page is loaded by waiting for the URL to be the expected dashboard URL.

The driver fixture initializes the Chrome browser once per test module and closes it after all tests finish.

The @pytest.mark.parametrize decorator runs the test_login function three times with different email and password pairs.

Explicit waits ensure the test waits for elements to be visible or URL to change before proceeding, avoiding flaky tests.

After each login test, the script navigates to the logout URL to reset the session for the next test.

Common Mistakes - 4 Pitfalls
{'mistake': 'Hardcoding test data inside the test function instead of using parameterization', 'why_bad': 'This makes tests less flexible and requires code changes to add more test data.', 'correct_approach': "Use pytest's @pytest.mark.parametrize decorator to supply multiple data sets cleanly."}
Using implicit waits or time.sleep() instead of explicit waits
Not closing the browser after tests
Mixing locators or using brittle XPath selectors
Bonus Challenge

Now add data-driven testing with 3 different invalid login inputs and verify error messages appear.

Show Hint