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.
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.
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()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window opened at https://example.com/login | — | PASS |
| 2 | Find username, password fields and login button | Login page loaded with input fields and button visible | Check presence of username, password fields and login button | PASS |
| 3 | Enter username 'user1' and password 'pass1', then click login | Login form filled and submitted | — | PASS |
| 4 | Wait for success message to appear | Success message displayed on page | Verify success message is displayed | PASS |
| 5 | Navigate back to login page | Login page reloaded for next test data | — | PASS |
| 6 | Repeat steps 2-5 for username 'user2' and password 'pass2' | Login page loaded, form filled, submitted, success message shown | Verify success message is displayed for user2 | PASS |
| 7 | Repeat steps 2-5 for username 'user3' and password 'pass3' | Login page loaded, form filled, submitted, success message shown | Verify success message is displayed for user3 | PASS |
| 8 | Test ends and browser closes | Browser closed | — | PASS |
@pytest.mark.parametrize in Selenium Python tests?@pytest.mark.parametrize allows running one test function multiple times with different sets of input data.("user1", "pass1") and ("user2", "pass2") in pytest?@pytest.mark.parametrize with a string of parameter names and a list of tuples for values.import pytest
@pytest.mark.parametrize('num, expected', [(1, 2), (3, 4)])
def test_increment(num, expected):
assert num + 1 == expectedimport pytest
@pytest.mark.parametrize('username, password')
def test_login(username, password):
assert username != '' and password != ''@pytest.mark.parametrize requires two arguments: parameter names and a list of values. Here, the list of values is missing.@pytest.mark.parametrize allows running the test multiple times with different data, opening the browser fresh each time ensures test isolation.Pass the test and this pattern moves unknown