0
0
Selenium Pythontesting~10 mins

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

Choose your learning style9 modes available
Test Overview

This test reads login credentials from an Excel file and uses them to perform a login on a web page. It verifies that the login is successful by checking the presence of a logout button.

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
import openpyxl

class TestLoginWithExcel(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/login')

    def test_login_from_excel(self):
        # Load Excel file
        workbook = openpyxl.load_workbook('testdata.xlsx')
        sheet = workbook.active

        # Read username and password from first row
        username = sheet['A2'].value
        password = sheet['B2'].value

        driver = self.driver

        # Find username input and enter value
        username_input = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, 'username'))
        )
        username_input.clear()
        username_input.send_keys(username)

        # Find password input and enter value
        password_input = driver.find_element(By.ID, 'password')
        password_input.clear()
        password_input.send_keys(password)

        # Click login button
        login_button = driver.find_element(By.ID, 'loginBtn')
        login_button.click()

        # Wait for logout button to appear to confirm login success
        logout_button = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, 'logoutBtn'))
        )

        # Assert logout button is displayed
        self.assertTrue(logout_button.is_displayed())

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window opened at https://example.com/login page-PASS
2Load Excel file 'testdata.xlsx' and read username and password from cells A2 and B2Excel file loaded with test data: username and password read-PASS
3Find username input field by ID 'username' and enter usernameUsername input field located and filled with username from Excel-PASS
4Find password input field by ID 'password' and enter passwordPassword input field located and filled with password from Excel-PASS
5Find login button by ID 'loginBtn' and click itLogin button clicked, page processing login-PASS
6Wait for logout button with ID 'logoutBtn' to appear to confirm login successLogout button visible on page after successful loginAssert logout button is displayedPASS
7Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Excel file missing or cells A2/B2 empty, or login fails due to incorrect credentials
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after clicking the login button?
AThat the username input is cleared
BThat the logout button appears on the page
CThat the Excel file is saved
DThat the browser navigates to the homepage URL
Key Result
Reading test data from external files like Excel helps separate test logic from data, making tests easier to maintain and reuse with different inputs.