Bird
Raised Fist0
Selenium Pythontesting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main reason to read test data from an Excel file in Selenium Python tests?
easy
A. To create Excel files automatically
B. To make the test code run faster
C. To run tests with many different inputs easily
D. To avoid writing any test code

Solution

  1. Step 1: Understand test data usage

    Test data from Excel allows running the same test with many inputs without changing code.
  2. Step 2: Identify the benefit

    This makes tests flexible and easier to maintain.
  3. Final Answer:

    To run tests with many different inputs easily -> Option C
  4. Quick Check:

    Reading Excel data = flexible inputs [OK]
Hint: Excel stores many inputs for tests in one place [OK]
Common Mistakes:
  • Thinking Excel makes tests faster
  • Believing Excel replaces test code
  • Confusing data reading with file creation
2. Which Python library is commonly used to read Excel files for Selenium tests?
easy
A. selenium
B. pandas
C. requests
D. openpyxl

Solution

  1. Step 1: Recall Excel reading libraries

    openpyxl is a popular library to read/write Excel files in Python.
  2. Step 2: Differentiate from other libraries

    pandas can read Excel but is heavier; requests is for web calls; selenium is for browser automation.
  3. Final Answer:

    openpyxl -> Option D
  4. Quick Check:

    Excel reading = openpyxl [OK]
Hint: openpyxl is made for Excel files in Python [OK]
Common Mistakes:
  • Choosing selenium which automates browsers
  • Picking requests which handles HTTP
  • Confusing pandas with openpyxl
3. Given this code snippet, what will be printed?
from openpyxl import load_workbook
wb = load_workbook('data.xlsx')
sheet = wb.active
print(sheet['A2'].value)
Assuming cell A2 contains the text "TestUser".
medium
A. data.xlsx
B. TestUser
C. sheet['A2']
D. None

Solution

  1. Step 1: Understand openpyxl cell access

    sheet['A2'].value reads the content of cell A2 in the active sheet.
  2. Step 2: Check given cell content

    Cell A2 contains "TestUser", so print outputs this string.
  3. Final Answer:

    TestUser -> Option B
  4. Quick Check:

    sheet['A2'].value = "TestUser" [OK]
Hint: sheet['A2'].value reads cell A2 content [OK]
Common Mistakes:
  • Printing the cell object instead of its value
  • Confusing filename with cell content
  • Expecting None if cell has text
4. What is wrong with this code snippet for reading Excel data?
from openpyxl import load_workbook
wb = load_workbook('testdata.xlsx')
sheet = wb['Sheet1']
for row in sheet.iter_rows(min_row=2, values_only=True):
    print(row[0])
medium
A. No error; code reads first column from second row correctly
B. Using sheet['Sheet1'] instead of wb['Sheet1']
C. Not specifying min_row=1 in iter_rows
D. Missing call to wb.save() before reading

Solution

  1. Step 1: Check sheet access

    wb['Sheet1'] correctly accesses the sheet named 'Sheet1'.
  2. Step 2: Analyze iter_rows usage

    iter_rows with min_row=2 and values_only=True returns tuples of cell values from second row onward.
  3. Step 3: Confirm print statement

    Printing row[0] prints the first column value of each row starting from row 2.
  4. Final Answer:

    No error; code reads first column from second row correctly -> Option A
  5. Quick Check:

    Code reads data correctly [OK]
Hint: wb['Sheet1'] accesses sheet; iter_rows returns values [OK]
Common Mistakes:
  • Using sheet['Sheet1'] instead of wb['Sheet1']
  • Forgetting values_only=True to get cell values
  • Thinking wb.save() is needed before reading
5. You want to read username and password pairs from Excel to run login tests. Which approach is best to handle empty cells safely?
hard
A. Use iter_rows with values_only=True and check if each cell is None before using
B. Skip checking cells and assume all data is present
C. Use sheet['A1'].value only and ignore other rows
D. Convert Excel file to CSV and read with open()

Solution

  1. Step 1: Understand data safety

    Empty cells in Excel return None, which can cause errors if not checked.
  2. Step 2: Use iter_rows with values_only=True and check for None

    This approach reads all rows and allows safe validation before using data in tests.
  3. Step 3: Avoid unsafe assumptions

    Skipping checks or reading only one cell risks test failures or incomplete coverage.
  4. Final Answer:

    Use iter_rows with values_only=True and check if each cell is None before using -> Option A
  5. Quick Check:

    Check None in cells to avoid errors [OK]
Hint: Always check for None in Excel cell values [OK]
Common Mistakes:
  • Ignoring empty cells causing test crashes
  • Reading only one cell missing data
  • Converting to CSV losing Excel features
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard