0
0
Selenium Pythontesting~10 mins

Parameterize with test data in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a login page and tries multiple username and password combinations. It checks if the login success message appears for each set of data.

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

    def test_login_with_multiple_users(self):
        test_data = [
            ("user1", "pass1"),
            ("user2", "pass2"),
            ("user3", "pass3")
        ]
        for username, password in test_data:
            username_field = WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located((By.ID, "username"))
            )
            password_field = self.driver.find_element(By.ID, "password")
            login_button = self.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()

            success_message = WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located((By.ID, "success-msg"))
            )
            self.assertTrue(success_message.is_displayed(), f"Login failed for {username}")

            # Navigate back to login page for next iteration
            self.driver.get('https://example.com/login')

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window opened at https://example.com/login-PASS
2Find username, password fields and login buttonLogin page loaded with input fields and button visibleCheck presence of username, password fields and login buttonPASS
3Enter username 'user1' and password 'pass1', then click loginLogin form filled and submitted-PASS
4Wait for success message to appearSuccess message displayed on pageVerify success message is displayedPASS
5Navigate back to login pageLogin page reloaded for next test data-PASS
6Repeat steps 2-5 for username 'user2' and password 'pass2'Login page loaded, form filled, submitted, success message shownVerify success message is displayed for user2PASS
7Repeat steps 2-5 for username 'user3' and password 'pass3'Login page loaded, form filled, submitted, success message shownVerify success message is displayed for user3PASS
8Test ends and browser closesBrowser closed-PASS
Failure Scenario
Failing Condition: Success message does not appear after login attempt
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test do after each login attempt?
ACloses the browser immediately
BSkips to the next test without checking success
CNavigates back to the login page for the next data set
DRefreshes the current page without navigation
Key Result
Parameterizing tests with multiple data sets helps verify the same functionality with different inputs efficiently, reducing code duplication and improving coverage.