Challenge - 5 Problems
JSON Test Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of reading JSON test data in Selenium Python
What will be the output of the following code snippet that reads test data from a JSON file and prints the username?
Selenium Python
import json with open('testdata.json', 'r') as file: data = json.load(file) print(data['user']['username'])
Attempts:
2 left
💡 Hint
Check the JSON structure and the key used to access the username.
✗ Incorrect
The code reads the JSON file and accesses the nested key 'user' then 'username'. If the JSON file contains {"user": {"username": "admin_user"}}, it prints 'admin_user'.
❓ assertion
intermediate2:00remaining
Correct assertion for JSON test data in Selenium Python
Which assertion correctly verifies that the password read from JSON test data equals 'pass123'?
Selenium Python
import json with open('testdata.json', 'r') as file: data = json.load(file) password = data['user']['password']
Attempts:
2 left
💡 Hint
Remember the correct syntax for assertions in Python.
✗ Incorrect
Option C uses the correct Python assert statement syntax to check equality. Option C uses assignment instead of comparison, causing a syntax error. Option C is a unittest method but not used standalone. Option C asserts inequality, which is incorrect.
❓ locator
advanced2:00remaining
Best locator to use with JSON test data for Selenium Python
Given JSON test data contains a field 'login_button_id' with value 'btnLogin'. Which locator is best to find the login button in Selenium Python?
Selenium Python
login_button_id = data['login_button_id']Attempts:
2 left
💡 Hint
Consider the type of locator that matches an element's id attribute.
✗ Incorrect
Since the JSON field contains the id of the button, using By.ID with that value is the most direct and reliable locator. Other options use different attributes that may not match the id value.
🔧 Debug
advanced2:00remaining
Identify the error when reading JSON test data
What error will this code raise if the JSON file is empty or malformed?
import json
with open('testdata.json', 'r') as file:
data = json.load(file)
print(data)
Attempts:
2 left
💡 Hint
Think about what happens when json.load tries to parse invalid JSON.
✗ Incorrect
If the JSON file is empty or malformed, json.load raises JSONDecodeError indicating it cannot parse the content. FileNotFoundError occurs if the file is missing, KeyError if accessing missing keys, TypeError if wrong types are used.
❓ framework
expert3:00remaining
Best practice for integrating JSON test data in Selenium Python tests
Which approach best integrates JSON test data into Selenium Python tests for maintainability and reusability?
Attempts:
2 left
💡 Hint
Think about avoiding repeated file reads and keeping tests clean.
✗ Incorrect
Loading JSON data once in a fixture or setup method improves efficiency and keeps tests clean and maintainable. Reading JSON in every test causes redundancy and slows tests. Hardcoding data reduces flexibility. Global variables can cause side effects and are discouraged.