Bird
Raised Fist0
Selenium Pythontesting~15 mins

Why data-driven tests increase coverage in Selenium Python - Automation Benefits in Action

Choose your learning style10 modes available
Verify login functionality with multiple user credentials
Preconditions (1)
Step 1: Enter username 'user1@example.com' in the username field
Step 2: Enter password 'Password1!' in the password field
Step 3: Click the login button
Step 4: Verify that the user is redirected to the dashboard page
Step 5: Log out and return to the login page
Step 6: Repeat steps 1-4 with username 'user2@example.com' and password 'Password2!'
Step 7: Repeat steps 1-4 with username 'user3@example.com' and password 'Password3!'
✅ Expected Result: Each set of valid credentials logs in successfully and redirects to the dashboard page
Automation Requirements - Selenium with Python unittest
Assertions Needed:
Verify current URL is dashboard URL after login
Verify login button is clickable before clicking
Verify username and password fields accept input
Best Practices:
Use explicit waits to wait for elements to be clickable or visible
Use Page Object Model to separate page locators and actions
Use data-driven approach to run the same test with multiple inputs
Use assertions to validate expected outcomes clearly
Automated Solution
Selenium Python
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.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using time.sleep() instead of explicit waits', 'why_bad': 'It makes tests slower and flaky because fixed waits may be too short or too long depending on page load.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected_conditions to wait only as long as needed."}
Hardcoding credentials inside the test steps repeatedly
Not verifying that elements are interactable before clicking or typing
Bonus Challenge

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

Show Hint

Practice

(1/5)
1. Why do data-driven tests increase test coverage in Selenium Python?
easy
A. Because they skip validation steps
B. Because they reduce the number of test cases to one
C. Because they only test the UI layout
D. Because they run the same test steps with many different inputs

Solution

  1. Step 1: Understand data-driven testing concept

    Data-driven tests repeat the same test steps but use different input data sets.
  2. Step 2: Connect data variation to coverage

    Using many inputs helps test more scenarios, increasing coverage and finding more bugs.
  3. Final Answer:

    Because they run the same test steps with many different inputs -> Option D
  4. Quick Check:

    Data-driven tests = many inputs = more coverage [OK]
Hint: More inputs mean more scenarios tested [OK]
Common Mistakes:
  • Thinking data-driven tests reduce test cases
  • Confusing UI layout testing with data-driven testing
  • Believing data-driven tests skip validations
2. Which of the following is the correct way to pass multiple data sets to a Selenium Python test using pytest parameterization?
easy
A. @pytest.parametrization('input', [1, 2, 3])
B. @pytest.parametrize('input', (1, 2, 3))
C. @pytest.mark.parametrize('input', [1, 2, 3])
D. @pytest.mark.parametrized('input', [1, 2, 3])

Solution

  1. Step 1: Recall pytest parameterization syntax

    The correct decorator is @pytest.mark.parametrize with 'param' and list of values.
  2. Step 2: Check each option syntax

    Only @pytest.mark.parametrize('input', [1, 2, 3]) uses correct spelling and syntax: @pytest.mark.parametrize('input', [1, 2, 3]).
  3. Final Answer:

    @pytest.mark.parametrize('input', [1, 2, 3]) -> Option C
  4. Quick Check:

    Correct pytest decorator = @pytest.mark.parametrize [OK]
Hint: Remember exact decorator spelling: parametrize with 'z' [OK]
Common Mistakes:
  • Misspelling 'parametrize' as 'parametrize' or 'parametrized'
  • Using parentheses instead of brackets for data list
  • Confusing 'param' and 'parametrize'
3. Given this pytest data-driven test snippet, what will be the output if the test fails for input 3?
import pytest

@pytest.mark.parametrize('num', [1, 2, 3])
def test_is_even(num):
    assert num % 2 == 0
medium
A. Test fails only for input 3
B. Test fails for inputs 1 and 3
C. Test passes for all inputs
D. Test fails for input 2 only

Solution

  1. Step 1: Understand the test logic

    The test asserts num % 2 == 0, meaning num must be even.
  2. Step 2: Check each input

    Input 1: 1 % 2 = 1 (fail), Input 2: 2 % 2 = 0 (pass), Input 3: 3 % 2 = 1 (fail).
  3. Final Answer:

    Test fails for inputs 1 and 3 -> Option B
  4. Quick Check:

    Odd numbers fail assertion [OK]
Hint: Even numbers pass; odd numbers fail [OK]
Common Mistakes:
  • Assuming only input 3 fails
  • Thinking all inputs pass
  • Confusing modulo operation results
4. Identify the error in this data-driven Selenium Python test code snippet:
@pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')])
def test_login(username, password):
    driver.get('http://example.com/login')
    driver.find_element(By.ID, 'username').send_keys(username)
    driver.find_element(By.ID, 'password').send_keys(password)
    driver.find_element(By.ID, 'submit').click()
    assert driver.find_element(By.ID, 'welcome').is_displayed()
medium
A. Missing import for By locator
B. Incorrect parameter names in parametrize decorator
C. Using send_keys on a button element
D. No assertion to check login success

Solution

  1. Step 1: Review code for missing imports

    The code uses By.ID but does not show import of By from selenium.webdriver.common.by.
  2. Step 2: Check other code parts

    Parameter names match, send_keys used correctly on input fields, assertion checks welcome element display.
  3. Final Answer:

    Missing import for By locator -> Option A
  4. Quick Check:

    Using By requires importing it [OK]
Hint: Check all used classes are imported [OK]
Common Mistakes:
  • Assuming parameter names are wrong
  • Thinking send_keys is used on button
  • Missing assertion is incorrect here
5. How does using data-driven tests in Selenium Python help maintain and expand test suites efficiently?
hard
A. By allowing reuse of test logic with new data sets without rewriting tests
B. By requiring a new test function for every input variation
C. By limiting tests to only one data set to reduce complexity
D. By removing the need for assertions in tests

Solution

  1. Step 1: Understand test maintenance challenges

    Writing separate tests for each input is time-consuming and hard to maintain.
  2. Step 2: Explain data-driven test advantage

    Data-driven tests reuse the same test code with different inputs, making it easy to add or change data without rewriting code.
  3. Final Answer:

    By allowing reuse of test logic with new data sets without rewriting tests -> Option A
  4. Quick Check:

    Reuse test logic + new data = easier maintenance [OK]
Hint: Reuse code, just add data sets [OK]
Common Mistakes:
  • Thinking each input needs a new test function
  • Believing data-driven tests reduce assertions
  • Assuming data-driven tests limit data sets
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard