0
0
Selenium Javatesting~5 mins

JSON test data in Selenium Java

Choose your learning style9 modes available
Introduction

We use JSON test data to keep test information organized and easy to change without touching the test code.

When you want to test a web form with many input values.
When you need to run the same test with different data sets.
When you want to separate test data from test logic for easier maintenance.
When multiple testers or developers share test data in a clear format.
When you want to load test data dynamically during test execution.
Syntax
Selenium Java
{
  "key": "value",
  "number": 123,
  "array": ["item1", "item2"],
  "object": {
    "subkey": "subvalue"
  }
}

JSON uses key-value pairs inside curly braces.

Keys and string values must be in double quotes.

Examples
Simple JSON with username and password for login test.
Selenium Java
{
  "username": "testuser",
  "password": "pass123"
}
JSON with an array of user objects for testing multiple users.
Selenium Java
{
  "users": [
    {"name": "Alice", "age": 30},
    {"name": "Bob", "age": 25}
  ]
}
Sample Program

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 is in 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.