0
0
Selenium Javatesting~20 mins

JSON test data in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 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.