Why data-driven tests increase coverage in Selenium Python - Automation Benefits in Action
import unittest 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.username_input = (By.ID, 'username') self.password_input = (By.ID, 'password') self.login_button = (By.ID, 'loginBtn') def enter_username(self, username): WebDriverWait(self.driver, 10).until( EC.visibility_of_element_located(self.username_input) ).clear() self.driver.find_element(*self.username_input).send_keys(username) def enter_password(self, password): WebDriverWait(self.driver, 10).until( EC.visibility_of_element_located(self.password_input) ).clear() self.driver.find_element(*self.password_input).send_keys(password) def click_login(self): WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable(self.login_button) ).click() class DashboardPage: def __init__(self, driver): self.driver = driver self.logout_button = (By.ID, 'logoutBtn') def is_loaded(self): return WebDriverWait(self.driver, 10).until( EC.url_contains('/dashboard') ) def logout(self): WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable(self.logout_button) ).click() class TestLoginDataDriven(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/login') self.login_page = LoginPage(self.driver) self.dashboard_page = DashboardPage(self.driver) def test_login_multiple_users(self): test_data = [ {'username': 'user1@example.com', 'password': 'Password1!'}, {'username': 'user2@example.com', 'password': 'Password2!'}, {'username': 'user3@example.com', 'password': 'Password3!'} ] for data in test_data: with self.subTest(data=data): self.login_page.enter_username(data['username']) self.login_page.enter_password(data['password']) self.login_page.click_login() self.assertTrue(self.dashboard_page.is_loaded(), f"Dashboard did not load for {data['username']}") self.dashboard_page.logout() # After logout, verify we are back on login page self.assertIn('/login', self.driver.current_url) def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test script uses Selenium with Python's unittest framework to automate login tests with multiple user credentials.
The LoginPage class encapsulates the login page elements and actions, using explicit waits to ensure elements are ready before interacting.
The DashboardPage class checks if the dashboard page loaded by waiting for the URL to contain '/dashboard' and provides a logout method.
The test test_login_multiple_users runs the login steps for three different users using a data-driven approach with a list of dictionaries. It uses subTest to isolate each data set.
Assertions verify that after login the dashboard page loads and after logout the user returns to the login page.
Explicit waits improve reliability by waiting for elements to be ready.
Using Page Object Model keeps locators and actions organized and reusable.
This approach increases test coverage by verifying the login works correctly for multiple users without duplicating code.
Now add data-driven testing with 3 different invalid login inputs and verify error messages appear