Automate login functionality using Page Object pattern
Preconditions (2)
✅ Expected Result: User successfully logs in and dashboard page is displayed
Jump into concepts and practice - no test required
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 import pytest 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, url): self.driver.get(url) def enter_email(self, email): email_field = WebDriverWait(self.driver, 10).until( EC.visibility_of_element_located(self.email_input) ) email_field.clear() email_field.send_keys(email) def enter_password(self, password): password_field = WebDriverWait(self.driver, 10).until( EC.visibility_of_element_located(self.password_input) ) password_field.clear() password_field.send_keys(password) def click_login(self): login_btn = WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable(self.login_button) ) login_btn.click() @pytest.fixture def driver(): driver = webdriver.Chrome() yield driver driver.quit() def test_login_success(driver): login_page = LoginPage(driver) login_page.load("https://example.com/login") login_page.enter_email("user@example.com") login_page.enter_password("Password123") login_page.click_login() WebDriverWait(driver, 10).until( EC.url_contains("/dashboard") ) assert "/dashboard" in driver.current_url, "User should be redirected to dashboard after login"
This test uses the Page Object Model (POM) pattern to improve test quality by separating page details from test logic. The LoginPage class encapsulates all interactions with the login page elements. This makes the test test_login_success easier to read and maintain.
Explicit waits ensure elements are ready before interacting, avoiding flaky tests. Assertions check that the user is redirected to the dashboard URL after login, confirming success.
Using pytest fixtures for the WebDriver setup and teardown keeps the test clean and reusable.
Now add data-driven testing with 3 different sets of login credentials (valid and invalid)
def test_sum():
result = sum([1, 2, 3])
assert result == 6
def test_divide():
result = 10 / 0
assert result == 0