What if you could run hundreds of tests automatically without typing a single input twice?
Why JSON test data in Selenium Java? - Purpose & Use Cases
Imagine you have to test a web form that accepts user details. You write down each test case on paper or in a spreadsheet, then manually enter data into the form every time you test.
This manual method is slow and tiring. You might mistype data, forget test cases, or miss edge cases. Repeating tests for many data sets becomes a huge chore, and errors sneak in unnoticed.
Using JSON test data lets you store all test inputs in a neat, structured file. Your Selenium tests can read this file automatically, running the same test many times with different data without extra typing.
driver.findElement(By.id("name")).sendKeys("John"); driver.findElement(By.id("email")).sendKeys("john@example.com");
JSONObject user = jsonData.getJSONObject("user1"); driver.findElement(By.id("name")).sendKeys(user.getString("name")); driver.findElement(By.id("email")).sendKeys(user.getString("email"));
It enables fast, reliable, and repeatable tests that cover many scenarios without extra manual work.
QA teams testing an e-commerce site use JSON files to supply thousands of product details and user profiles, ensuring the site works perfectly for all cases.
Manual data entry is slow and error-prone.
JSON test data automates input and improves accuracy.
This approach scales testing efficiently across many scenarios.
Practice
JSON test data in Selenium Java tests?Solution
Step 1: Understand JSON role in testing
JSON stores test data separately from the test code, so data changes don't require code changes.Step 2: Identify benefits of separation
This separation makes tests easier to maintain and reuse with different data sets.Final Answer:
It separates test data from test code for easier maintenance. -> Option AQuick Check:
JSON separates data and code = B [OK]
- Thinking JSON speeds up test execution
- Believing JSON fixes code bugs automatically
- Confusing JSON with code format
Solution
Step 1: Check correct constructor usage
The org.json library's JSONObject constructor accepts a Reader object to parse JSON text.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.Final Answer:
JSONObject obj = new JSONObject(new FileReader("data.json")); -> Option AQuick Check:
JSONObject needs Reader input = A [OK]
- Passing filename string directly to JSONObject
- Using InputStream instead of Reader
- Passing filename string to BufferedReader constructor
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"));Solution
Step 1: Parse JSON content
The JSON has key "username" with value "user1".Step 2: Call getString("username")
This returns the string value for key "username", which is "user1".Final Answer:
user1 -> Option CQuick Check:
obj.getString("username") = "user1" [OK]
- Confusing username and password keys
- Expecting null if key exists
- Not handling JSONException properly
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.Solution
Step 1: Understand getString behavior
Calling getString on a missing key throws JSONException in org.json library.Step 2: Check code effect
Since "email" key is missing, the code throws JSONException at runtime.Final Answer:
It throws a JSONException at runtime. -> Option DQuick Check:
Missing key getString throws exception = D [OK]
- Expecting null or empty string instead of exception
- Ignoring exception handling
- Assuming code won't compile
{"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?
Solution
Step 1: Identify JSON array access
The key "users" holds a JSON array, so getJSONArray("users") is correct.Step 2: Loop correctly over array
Loop from 0 to users.length() - 1, get each JSONObject, then extract username and password.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.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 BQuick Check:
JSONArray + for loop with i < length = A [OK]
- Using getJSONObject instead of getJSONArray for array
- Looping with i <= length causing IndexOutOfBounds
- Treating JSON array as string
Ready to prove it?
Pass the test and this pattern moves unknown
