Bird
Raised Fist0
Selenium Pythontesting~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JSON 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 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'])
Aadmin_user
BKeyError
CTypeError
DFileNotFoundError
Attempts:
2 left
💡 Hint
Check the JSON structure and the key used to access the username.
❓ assertion
intermediate
2: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']
Aassert password = 'pass123'
BassertEqual(password, 'pass123')
Cassert password == 'pass123'
Dassert password != 'pass123'
Attempts:
2 left
💡 Hint
Remember the correct syntax for assertions in Python.
❓ locator
advanced
2: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']
Adriver.find_element(By.NAME, login_button_id)
Bdriver.find_element(By.CLASS_NAME, login_button_id)
Cdriver.find_element(By.XPATH, login_button_id)
Ddriver.find_element(By.ID, login_button_id)
Attempts:
2 left
💡 Hint
Consider the type of locator that matches an element's id attribute.
🔧 Debug
advanced
2: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)
AFileNotFoundError
Bjson.decoder.JSONDecodeError
CKeyError
DTypeError
Attempts:
2 left
💡 Hint
Think about what happens when json.load tries to parse invalid JSON.
❓ framework
expert
3: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?
ALoad JSON data once in a fixture or setup method and pass it to test functions
BRead the JSON file inside every test function separately
CUse global variables to store JSON data and access them anywhere
DHardcode test data inside test functions instead of JSON
Attempts:
2 left
💡 Hint
Think about avoiding repeated file reads and keeping tests clean.

Practice

(1/5)
1. What is the main benefit of reading test data from a JSON file in Selenium Python tests?
easy
A. It encrypts test data for security.
B. It makes tests run faster by avoiding web interactions.
C. It automatically generates test cases without writing code.
D. It keeps test code clean and makes data easy to update without changing code.

Solution

  1. Step 1: Understand test data separation

    Reading test data from JSON separates data from test logic, making code cleaner.
  2. Step 2: Recognize flexibility advantage

    Updating test data in JSON does not require changing test scripts, improving flexibility.
  3. Final Answer:

    It keeps test code clean and makes data easy to update without changing code. -> Option D
  4. Quick Check:

    Data separation = Clean code + Easy updates [OK]
Hint: Separate data from code for easy updates [OK]
Common Mistakes:
  • Thinking JSON speeds up test execution
  • Believing JSON auto-generates tests
  • Confusing data storage with encryption
2. Which Python code snippet correctly reads a JSON file named data.json into a variable test_data?
easy
A. import json test_data = json.load(open('data.json'))
B. test_data = open('data.json').read()
C. import json test_data = json.loads('data.json')
D. test_data = json.read('data.json')

Solution

  1. 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().
  2. 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.
  3. Final Answer:

    import json\ntest_data = json.load(open('data.json')) -> Option A
  4. Quick Check:

    json.load + open(file) = correct JSON read [OK]
Hint: Use json.load() with open() for files [OK]
Common Mistakes:
  • Using json.loads() on filename string
  • Forgetting to import json module
  • Calling non-existent json.read() function
3. Given the JSON file 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?
medium
A. {'username': 'testuser', 'password': 'pass123'}
B. pass123
C. testuser
D. KeyError

Solution

  1. 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'.
  2. Step 2: Access dictionary value by key

    data['username'] accesses the value 'testuser' from the dictionary.
  3. Final Answer:

    testuser -> Option C
  4. Quick Check:

    data['username'] = 'testuser' [OK]
Hint: Access JSON keys like dictionary keys [OK]
Common Mistakes:
  • Printing the whole dictionary instead of key value
  • Using wrong key causing KeyError
  • Confusing json.load() with json.loads()
4. What is wrong with this code snippet that reads JSON test data?
import json
file = open('data.json')
data = json.load(file)
file.close()
print(data['email'])

Assuming data.json does not contain the key email.
medium
A. The code will raise a KeyError because 'email' key is missing.
B. The print statement syntax is incorrect.
C. The file is not closed properly.
D. json.load() cannot read from open file objects.

Solution

  1. Step 1: Understand json.load() behavior

    json.load() correctly reads JSON from an open file object, so no error here.
  2. Step 2: Identify missing key issue

    Accessing data['email'] when 'email' key is missing causes a KeyError at runtime.
  3. Final Answer:

    The code will raise a KeyError because 'email' key is missing. -> Option A
  4. Quick Check:

    Missing key access = KeyError [OK]
Hint: Check keys exist before accessing JSON data [OK]
Common Mistakes:
  • Thinking json.load() fails on open files
  • Ignoring possible missing keys in JSON
  • Assuming file.close() is missing or wrong
5. You want to use JSON test data to fill a login form in Selenium Python. The JSON file 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?
hard
A. import json with open('login.json') as f: data = json.load(f) driver.find_element_by_name('user').send_keys(data['username']) driver.find_element_by_name('pass').send_keys(data['password'])
B. 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'])
C. 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'])
D. import json with open('login.json') as f: data = json.load(f) driver.find_element('name', 'user').send_keys(data['username']) driver.find_element('name', 'pass').send_keys(data['password'])

Solution

  1. 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.
  2. 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.
  3. Step 3: Match locators with element ids

    The form fields have ids 'user' and 'pass', so locating by id is correct, not by name.
  4. 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 B
  5. Quick Check:

    json.load + find_element('id', ...) = correct usage [OK]
Hint: Use json.load() and find_element('id', ...) for form filling [OK]
Common Mistakes:
  • Using json.loads() on file object
  • Using deprecated find_element_by_id()
  • Using wrong locator type (name instead of id)
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