Test Overview
This test reads login credentials from a CSV file and verifies successful login on a web page using Selenium.
This test reads login credentials from a CSV file and verifies successful login on a web page using Selenium.
import csv 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 TestLoginWithCSV(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/login') def test_login_from_csv(self): with open('testdata.csv', newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: username = row['username'] password = row['password'] username_field = WebDriverWait(self.driver, 10).until( EC.presence_of_element_located((By.ID, 'username')) ) username_field.clear() username_field.send_keys(username) password_field = self.driver.find_element(By.ID, 'password') password_field.clear() password_field.send_keys(password) login_button = self.driver.find_element(By.ID, 'login-btn') login_button.click() welcome_message = WebDriverWait(self.driver, 10).until( EC.presence_of_element_located((By.ID, 'welcome-msg')) ) self.assertIn('Welcome', welcome_message.text) 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 opened at https://example.com/login page | - | PASS |
| 2 | Reads first row from CSV file with username and password | CSV file opened and first credentials loaded | - | PASS |
| 3 | Finds username input field by ID 'username' and enters username | Username field visible and filled with username | - | PASS |
| 4 | Finds password input field by ID 'password' and enters password | Password field visible and filled with password | - | PASS |
| 5 | Finds login button by ID 'login-btn' and clicks it | Login button clicked, page loading | - | PASS |
| 6 | Waits for welcome message element with ID 'welcome-msg' to appear | Welcome message visible on page | Check that welcome message text contains 'Welcome' | PASS |
| 7 | Navigates back to login page to test next CSV row | Browser at login page ready for next input | - | PASS |
| 8 | Repeats steps 2-7 for each row in CSV file | All CSV rows processed with login attempts | All welcome messages verified for each login | PASS |
| 9 | Test ends and browser closes | Browser closed, test finished | - | PASS |