0
0
Selenium Pythontesting~10 mins

Reading test data from CSV in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test reads login credentials from a CSV file and verifies successful login on a web page using Selenium.

Test Code - Selenium
Selenium Python
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()
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser opened at https://example.com/login page-PASS
2Reads first row from CSV file with username and passwordCSV file opened and first credentials loaded-PASS
3Finds username input field by ID 'username' and enters usernameUsername field visible and filled with username-PASS
4Finds password input field by ID 'password' and enters passwordPassword field visible and filled with password-PASS
5Finds login button by ID 'login-btn' and clicks itLogin button clicked, page loading-PASS
6Waits for welcome message element with ID 'welcome-msg' to appearWelcome message visible on pageCheck that welcome message text contains 'Welcome'PASS
7Navigates back to login page to test next CSV rowBrowser at login page ready for next input-PASS
8Repeats steps 2-7 for each row in CSV fileAll CSV rows processed with login attemptsAll welcome messages verified for each loginPASS
9Test ends and browser closesBrowser closed, test finished-PASS
Failure Scenario
Failing Condition: Welcome message element not found or text does not contain 'Welcome' after login
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test do after clicking the login button?
ACloses the browser
BImmediately navigates back to the login page
CWaits for the welcome message element to appear
DReads the next CSV row before checking login
Key Result
Using external CSV files for test data helps run multiple test cases easily and keeps test code clean and maintainable.