What if you could change test data without touching your test code at all?
Why Reading test data from JSON in Selenium Python? - Purpose & Use Cases
Imagine you have a web form to test with many different input values. You write each test case by hand, typing the data directly into your test script every time.
This manual way is slow and tiring. If you want to change a test value, you must edit the code again and again. It's easy to make mistakes and hard to keep track of all test data.
Reading test data from JSON files lets you keep all your test inputs in one place. Your test script reads the data automatically, so you can add or change test cases without touching the code.
username = 'user1' password = 'pass1' # test steps here
import json with open('data.json') as f: data = json.load(f) username = data['username'] password = data['password'] # test steps here
This makes your tests easier to maintain and lets you run many test cases quickly by just changing the JSON file.
Think of testing a login page with dozens of usernames and passwords. Instead of rewriting tests, you store all credentials in a JSON file and run tests automatically for each pair.
Manual test data entry is slow and error-prone.
JSON files separate data from test code for easy updates.
Automated reading of JSON data speeds up testing and improves reliability.
Practice
Solution
Step 1: Understand test data separation
Reading test data from JSON separates data from test logic, making code cleaner.Step 2: Recognize flexibility advantage
Updating test data in JSON does not require changing test scripts, improving flexibility.Final Answer:
It keeps test code clean and makes data easy to update without changing code. -> Option DQuick Check:
Data separation = Clean code + Easy updates [OK]
- Thinking JSON speeds up test execution
- Believing JSON auto-generates tests
- Confusing data storage with encryption
data.json into a variable test_data?Solution
Step 1: Identify correct json module usage
The json.load() function reads JSON from a file object, so open() must be passed to json.load().Step 2: Check syntax correctness
import json test_data = json.load(open('data.json')) correctly imports json and uses json.load(open('data.json')) to read the file.Final Answer:
import json\ntest_data = json.load(open('data.json')) -> Option AQuick Check:
json.load + open(file) = correct JSON read [OK]
- Using json.loads() on filename string
- Forgetting to import json module
- Calling non-existent json.read() function
user.json with content:{"username": "testuser", "password": "pass123"}and the code:
import json
with open('user.json') as f:
data = json.load(f)
print(data['username'])What will be the output?
Solution
Step 1: Load JSON data into dictionary
The json.load(f) reads the JSON file and converts it into a Python dictionary with keys 'username' and 'password'.Step 2: Access dictionary value by key
data['username'] accesses the value 'testuser' from the dictionary.Final Answer:
testuser -> Option CQuick Check:
data['username'] = 'testuser' [OK]
- Printing the whole dictionary instead of key value
- Using wrong key causing KeyError
- Confusing json.load() with json.loads()
import json
file = open('data.json')
data = json.load(file)
file.close()
print(data['email'])Assuming
data.json does not contain the key email.Solution
Step 1: Understand json.load() behavior
json.load() correctly reads JSON from an open file object, so no error here.Step 2: Identify missing key issue
Accessing data['email'] when 'email' key is missing causes a KeyError at runtime.Final Answer:
The code will raise a KeyError because 'email' key is missing. -> Option AQuick Check:
Missing key access = KeyError [OK]
- Thinking json.load() fails on open files
- Ignoring possible missing keys in JSON
- Assuming file.close() is missing or wrong
login.json contains:{"username": "user1", "password": "pass1"}Which code snippet correctly reads the JSON and uses Selenium to fill the form fields with ids
user and pass?Solution
Step 1: Correctly read JSON file
json.load(f) reads JSON from file object correctly; json.loads() expects a string, so import json with open('login.json') as f: data = json.loads(f) driver.find_element_by_id('user').send_keys(data['username']) driver.find_element_by_id('pass').send_keys(data['password']) is wrong.Step 2: Use correct Selenium locator method
driver.find_element('id', 'user') is the modern syntax to find element by id; find_element_by_id() is deprecated.Step 3: Match locators with element ids
The form fields have ids 'user' and 'pass', so locating by id is correct, not by name.Final Answer:
import json with open('login.json') as f: data = json.load(f) driver.find_element('id', 'user').send_keys(data['username']) driver.find_element('id', 'pass').send_keys(data['password']) -> Option BQuick Check:
json.load + find_element('id', ...) = correct usage [OK]
- Using json.loads() on file object
- Using deprecated find_element_by_id()
- Using wrong locator type (name instead of id)
Ready to prove it?
Pass the test and this pattern moves unknown
