0
0
Selenium Pythontesting~20 mins

Reading test data from CSV in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSV Test Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of reading CSV test data with Python csv.reader
What will be the output of this code snippet that reads a CSV file and prints each row as a list?
Selenium Python
import csv
with open('testdata.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print(row)
AError: FileNotFoundError
Busername,password\nuser1,pass1\nuser2,pass2
C['username,password', 'user1,pass1', 'user2,pass2']
D[['username', 'password'], ['user1', 'pass1'], ['user2', 'pass2']]
Attempts:
2 left
💡 Hint
csv.reader returns each row as a list of strings.
assertion
intermediate
1:30remaining
Correct assertion for CSV test data length
Given a list 'data' read from a CSV file with 3 rows including header, which assertion correctly checks the number of data rows excluding the header?
Selenium Python
data = [['username', 'password'], ['user1', 'pass1'], ['user2', 'pass2']]
Aassert len(data) == 2
Bassert len(data[1:]) == 3
Cassert len(data) - 1 == 2
Dassert len(data[1:]) == 1
Attempts:
2 left
💡 Hint
Remember to exclude the header row when counting data rows.
locator
advanced
1:30remaining
Best locator to find username input for CSV-driven test
In a Selenium test reading usernames from CSV, which locator is best to find the username input field for entering data?
Adriver.find_element(By.ID, 'username')
Bdriver.find_element(By.XPATH, '//input[@name="user"]')
Cdriver.find_element(By.CLASS_NAME, 'input-field')
Ddriver.find_element(By.CSS_SELECTOR, 'input[type="text"]')
Attempts:
2 left
💡 Hint
ID locators are unique and fast.
🔧 Debug
advanced
2:00remaining
Identify error reading CSV with DictReader
What error will this code raise when reading a CSV file missing the header row?
Selenium Python
import csv
with open('testdata_noheader.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row['username'])
AAttributeError: 'NoneType' object has no attribute 'get'
BKeyError: 'username'
CNo error, prints usernames
DFileNotFoundError
Attempts:
2 left
💡 Hint
DictReader uses the first row as keys by default.
framework
expert
3:00remaining
Best practice for integrating CSV test data in Selenium Python tests
Which approach best integrates CSV test data into Selenium tests for multiple login scenarios?
ARead CSV in test setup, use pytest parametrize to run test with each row
BHardcode test data in test function, ignore CSV
CRead CSV inside each test method without reuse
DManually copy CSV data into test assertions
Attempts:
2 left
💡 Hint
Use test frameworks features to run tests with multiple data sets.