0
0
Selenium Pythontesting~10 mins

Why data-driven tests increase coverage in Selenium Python - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test runs a login form check with multiple sets of usernames and passwords. It verifies that the login result matches the expected outcome for each data set. This shows how data-driven tests increase coverage by testing many cases in one test.

Test Code - Selenium
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 TestLoginDataDriven(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/login')

    def test_login_multiple_users(self):
        test_data = [
            {'username': 'user1', 'password': 'pass1', 'expected': 'Welcome user1!'},
            {'username': 'user2', 'password': 'wrongpass', 'expected': 'Invalid credentials'},
            {'username': 'user3', 'password': 'pass3', 'expected': 'Welcome user3!'}
        ]

        for data in test_data:
            username_input = WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located((By.ID, 'username'))
            )
            password_input = self.driver.find_element(By.ID, 'password')
            login_button = self.driver.find_element(By.ID, 'login-btn')

            username_input.clear()
            username_input.send_keys(data['username'])
            password_input.clear()
            password_input.send_keys(data['password'])
            login_button.click()

            message = WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.ID, 'message'))
            ).text

            self.assertEqual(message, data['expected'])

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test starts and browser opens the login pageBrowser shows login page with username, password fields and login button-PASS
2Find username, password input fields and login buttonElements are present and interactable-PASS
3Enter username 'user1' and password 'pass1', then click loginLogin form filled and submitted-PASS
4Wait for message element and check textMessage displayed: 'Welcome user1!'Assert message equals 'Welcome user1!'PASS
5Repeat steps 2-4 for username 'user2' and password 'wrongpass'Login form filled and submitted-PASS
6Wait for message element and check textMessage displayed: 'Invalid credentials'Assert message equals 'Invalid credentials'PASS
7Repeat steps 2-4 for username 'user3' and password 'pass3'Login form filled and submitted-PASS
8Wait for message element and check textMessage displayed: 'Welcome user3!'Assert message equals 'Welcome user3!'PASS
9Test ends and browser closesBrowser closed-PASS
Failure Scenario
Failing Condition: If the message text does not match the expected text for any data set
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify for each set of username and password?
AThe displayed message matches the expected result
BThe page URL changes after login
CThe login button becomes disabled
DThe browser title changes
Key Result
Data-driven tests improve coverage by running the same test steps with different inputs and expected results, catching more cases efficiently.