Bird
Raised Fist0
Selenium Javatesting~20 mins

JSON test data in Selenium Java - 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
What is the output of parsing this JSON string in Java?
Given the following JSON string and Java code snippet using org.json library, what will be the output printed?
Selenium Java
String jsonString = "{\"name\": \"Alice\", \"age\": 30, \"active\": true}";
JSONObject obj = new JSONObject(jsonString);
System.out.println(obj.getString("name") + ", " + obj.getInt("age") + ", " + obj.getBoolean("active"));
AAlice, 30, true
BAlice, 30, false
C30, Alice, true
DThrows JSONException
Attempts:
2 left
💡 Hint
Remember to match the JSON keys with the correct getter methods.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies the 'status' field in JSON response?
You receive a JSON response: {"status":"success","code":200}. Which JUnit assertion correctly checks that the 'status' field equals 'success'?
AassertFalse(jsonObject.getBoolean("status"));
BassertTrue(jsonObject.getInt("status") == 200);
CassertEquals("success", jsonObject.getString("status"));
DassertNull(jsonObject.getString("status"));
Attempts:
2 left
💡 Hint
Check the data type of the 'status' field in JSON.
❓ locator
advanced
2:00remaining
Identify the best locator to find a JSON data-driven test element
You have a web page that dynamically loads test data from JSON into elements with data-test-id attributes. Which Selenium locator is best to find the element with data-test-id='user-123'?
ABy.id("user-123")
BBy.xpath("//div[@data-test-id='user-123']")
CBy.className("user-123")
DBy.cssSelector("[data-test-id='user-123']")
Attempts:
2 left
💡 Hint
Use attribute selectors for custom data attributes.
🔧 Debug
advanced
2:00remaining
Why does this JSON parsing code throw an exception?
Consider this Java code snippet: String jsonString = "{\"id\": 101, \"name\": null}"; JSONObject obj = new JSONObject(jsonString); String name = obj.getString("name"); Why does obj.getString("name") throw an exception?
Selenium Java
String jsonString = "{\"id\": 101, \"name\": null}";
JSONObject obj = new JSONObject(jsonString);
String name = obj.getString("name");
ABecause the 'name' key has a null value, getString throws JSONException
BBecause getString cannot be used with integers
CBecause jsonString is not valid JSON
DBecause 'name' key is missing in JSON
Attempts:
2 left
💡 Hint
Check how org.json handles null values in JSON objects.
❓ framework
expert
3:00remaining
How to integrate JSON test data with Selenium tests in Java?
Which approach best integrates external JSON test data into Selenium Java tests for data-driven testing?
AHardcode JSON strings inside Selenium test methods and parse inline
BRead JSON file using Jackson or Gson, parse into POJOs, then use TestNG DataProvider to supply test data
CUse Selenium WebDriver to fetch JSON from web page and parse it during test execution
DConvert JSON to XML manually and use XML parsers for test data
Attempts:
2 left
💡 Hint
Consider maintainability and separation of test data from test code.

Practice

(1/5)
1. What is the main advantage of using JSON test data in Selenium Java tests?
easy
A. It separates test data from test code for easier maintenance.
B. It makes the test run faster by compiling JSON.
C. It automatically fixes bugs in the test code.
D. It converts Java code into JSON format.

Solution

  1. Step 1: Understand JSON role in testing

    JSON stores test data separately from the test code, so data changes don't require code changes.
  2. Step 2: Identify benefits of separation

    This separation makes tests easier to maintain and reuse with different data sets.
  3. Final Answer:

    It separates test data from test code for easier maintenance. -> Option A
  4. Quick Check:

    JSON separates data and code = B [OK]
Hint: Remember: JSON holds data, not code [OK]
Common Mistakes:
  • Thinking JSON speeds up test execution
  • Believing JSON fixes code bugs automatically
  • Confusing JSON with code format
2. Which of the following is the correct way to read a JSON file in Selenium Java using the org.json library?
easy
A. JSONObject obj = new JSONObject(new FileReader("data.json"));
B. JSONObject obj = new JSONObject("data.json");
C. JSONObject obj = new JSONObject(new FileInputStream("data.json"));
D. JSONObject obj = new JSONObject(new BufferedReader("data.json"));

Solution

  1. Step 1: Check correct constructor usage

    The org.json library's JSONObject constructor accepts a Reader object to parse JSON text.
  2. Step 2: Match with options

    JSONObject obj = new JSONObject(new FileReader("data.json")); uses new FileReader("data.json") which is a Reader, so it is correct.
  3. Final Answer:

    JSONObject obj = new JSONObject(new FileReader("data.json")); -> Option A
  4. Quick Check:

    JSONObject needs Reader input = A [OK]
Hint: Use FileReader with JSONObject constructor [OK]
Common Mistakes:
  • Passing filename string directly to JSONObject
  • Using InputStream instead of Reader
  • Passing filename string to BufferedReader constructor
3. Given the JSON file data.json with content:
{"username": "user1", "password": "pass123"}

What will the following Java code print?
JSONObject obj = new JSONObject(new FileReader("data.json"));
System.out.println(obj.getString("username"));
medium
A. null
B. pass123
C. user1
D. Throws JSONException

Solution

  1. Step 1: Parse JSON content

    The JSON has key "username" with value "user1".
  2. Step 2: Call getString("username")

    This returns the string value for key "username", which is "user1".
  3. Final Answer:

    user1 -> Option C
  4. Quick Check:

    obj.getString("username") = "user1" [OK]
Hint: Match key names exactly to get values [OK]
Common Mistakes:
  • Confusing username and password keys
  • Expecting null if key exists
  • Not handling JSONException properly
4. What is wrong with this Selenium Java code snippet that reads JSON test data?
JSONObject obj = new JSONObject(new FileReader("testdata.json"));
String email = obj.getString("email");
System.out.println(email);

Assuming the JSON file testdata.json does NOT contain the key email.
medium
A. It compiles but does not run.
B. It prints an empty string.
C. It returns null without error.
D. It throws a JSONException at runtime.

Solution

  1. Step 1: Understand getString behavior

    Calling getString on a missing key throws JSONException in org.json library.
  2. Step 2: Check code effect

    Since "email" key is missing, the code throws JSONException at runtime.
  3. Final Answer:

    It throws a JSONException at runtime. -> Option D
  4. Quick Check:

    Missing key getString throws exception = D [OK]
Hint: Missing JSON keys cause exceptions, not null [OK]
Common Mistakes:
  • Expecting null or empty string instead of exception
  • Ignoring exception handling
  • Assuming code won't compile
5. You want to run a Selenium test multiple times with different user credentials stored in a JSON array like:
{"users": [{"username": "user1", "password": "pass1"}, {"username": "user2", "password": "pass2"}]}

Which approach correctly reads and uses this JSON data in Java to loop through each user?
hard
A. JSONObject users = obj.getJSONObject("users"); for (int i = 0; i < users.length(); i++) { String username = users.getString("username"); String password = users.getString("password"); }
B. JSONArray users = obj.getJSONArray("users"); for (int i = 0; i < users.length(); i++) { JSONObject user = users.getJSONObject(i); String username = user.getString("username"); String password = user.getString("password"); // use username and password }
C. String users = obj.getString("users"); for (String user : users) { // use user }
D. JSONArray users = obj.getJSONArray("users"); for (int i = 0; i <= users.length(); i++) { JSONObject user = users.getJSONObject(i); // use user }

Solution

  1. Step 1: Identify JSON array access

    The key "users" holds a JSON array, so getJSONArray("users") is correct.
  2. Step 2: Loop correctly over array

    Loop from 0 to users.length() - 1, get each JSONObject, then extract username and password.
  3. Step 3: Check options

    JSONArray users = obj.getJSONArray("users"); for (int i = 0; i < users.length(); i++) { JSONObject user = users.getJSONObject(i); String username = user.getString("username"); String password = user.getString("password"); // use username and password } correctly uses JSONArray, loops with i < length, and extracts data properly.
  4. Final Answer:

    JSONArray users = obj.getJSONArray("users"); for (int i = 0; i < users.length(); i++) { JSONObject user = users.getJSONObject(i); String username = user.getString("username"); String password = user.getString("password"); // use username and password } -> Option B
  5. Quick Check:

    JSONArray + for loop with i < length = A [OK]
Hint: Use JSONArray and loop i < length, not <= [OK]
Common Mistakes:
  • Using getJSONObject instead of getJSONArray for array
  • Looping with i <= length causing IndexOutOfBounds
  • Treating JSON array as string
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