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.
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.
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()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open | - | PASS |
| 2 | Navigate to login page for user1 | Login page loaded with username and password fields | - | PASS |
| 3 | Find username, password fields and login button | Elements located by IDs: username, password, login-btn | - | PASS |
| 4 | Enter username 'user1' and password 'pass1', then click login | Login form submitted | - | PASS |
| 5 | Wait until element with ID 'welcome-msg' is present | Welcome message element appears | Check that welcome message text contains 'Welcome' | PASS |
| 6 | Repeat steps 2-5 for user2 with password 'pass2' | Login page loaded and login successful for user2 | Welcome message contains 'Welcome' | PASS |
| 7 | Repeat steps 2-5 for user3 with password 'pass3' | Login page loaded and login successful for user3 | Welcome message contains 'Welcome' | PASS |
| 8 | Test ends and browser closes | Browser window closed | - | PASS |