This Java Selenium test reads login data from a JSON file and uses it to fill a login form. It then checks if login succeeded by looking at the page title.
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.json.JSONObject;
import java.nio.file.Files;
import java.nio.file.Paths;
public class JsonTestDataExample {
public static void main(String[] args) throws Exception {
// Read JSON file content
String content = new String(Files.readAllBytes(Paths.get("testdata.json")));
JSONObject json = new JSONObject(content);
// Extract test data
String username = json.getString("username");
String password = json.getString("password");
// Setup WebDriver (assumes chromedriver isin system PATH)
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");
// Fill login form using JSON data
driver.findElement(By.id("usernameInput")).sendKeys(username);
driver.findElement(By.id("passwordInput")).sendKeys(password);
driver.findElement(By.id("loginButton")).click();
// Simple check: page title contains 'Dashboard' after login
String title = driver.getTitle();
if (title.contains("Dashboard")) {
System.out.println("Test Passed: Login successful.");
} else {
System.out.println("Test Failed: Login unsuccessful.");
}
driver.quit();
}
}
OutputSuccess
Important Notes
Keep JSON files well formatted for easy reading and editing.
Use meaningful keys in JSON to avoid confusion.
Always validate JSON data before using it in tests to avoid errors.
Summary
JSON test data helps separate test data from test code.
It makes tests easier to maintain and reuse with different data.
Reading JSON in Selenium Java allows dynamic and flexible testing.
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
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 A
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
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 A
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
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 C
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?
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
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 B
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 unknownstrongwhile earning +1 Grip toward your certificate.