0
0
Selenium Pythontesting~10 mins

Data providers pattern in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses the data providers pattern to run the same login test with multiple username and password pairs. It verifies that the login succeeds for each set of credentials.

Test Code - unittest
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 LoginTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(5)

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    def login_test(self, username, password):
        driver = self.driver
        driver.get('https://example.com/login')
        username_field = driver.find_element(By.ID, 'username')
        password_field = driver.find_element(By.ID, 'password')
        login_button = driver.find_element(By.ID, 'login-btn')

        username_field.clear()
        username_field.send_keys(username)
        password_field.clear()
        password_field.send_keys(password)
        login_button.click()

        WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, 'welcome-msg'))
        )
        welcome_text = driver.find_element(By.ID, 'welcome-msg').text
        self.assertIn('Welcome', welcome_text)

    def test_login_with_multiple_users(self):
        test_data = [
            ('user1', 'pass1'),
            ('user2', 'pass2'),
            ('user3', 'pass3')
        ]
        for username, password in test_data:
            with self.subTest(username=username):
                self.login_test(username, password)

if __name__ == '__main__':
    unittest.main()
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open-PASS
2Navigate to login page for user1Login page loaded with username and password fields-PASS
3Find username, password fields and login buttonElements located by IDs: username, password, login-btn-PASS
4Enter username 'user1' and password 'pass1', then click loginLogin form submitted-PASS
5Wait until element with ID 'welcome-msg' is presentWelcome message element appearsCheck that welcome message text contains 'Welcome'PASS
6Repeat steps 2-5 for user2 with password 'pass2'Login page loaded and login successful for user2Welcome message contains 'Welcome'PASS
7Repeat steps 2-5 for user3 with password 'pass3'Login page loaded and login successful for user3Welcome message contains 'Welcome'PASS
8Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: If the welcome message element does not appear after login or text does not contain 'Welcome'
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after clicking the login button?
AThat the welcome message with 'Welcome' text appears
BThat the login button is disabled
CThat the username field is cleared
DThat the password field shows the password
Key Result
Using data providers pattern with subTest allows running the same test logic for multiple data sets cleanly and reports each case separately.