Saturday, December 6, 2025

thumbnail

Using JSON for Test Data in Java Selenium

 Why Use JSON for Test Data?


Using JSON files for test data in Selenium has several advantages:


Separation of data and code → easier to maintain


Reusable test data → different test scenarios without changing code


Supports complex data structures → arrays, nested objects


Easier to integrate with CI/CD pipelines


๐Ÿงฐ 1. Add JSON Parsing Library


Java doesn’t natively parse JSON, so you need a library:


Jackson (popular and widely used)


Gson (from Google)


Maven dependency for Jackson:


<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

    <version>2.15.2</version>

</dependency>


๐Ÿ“‚ 2. Sample JSON Test Data


Example testdata.json:


{

  "login": {

    "username": "testuser",

    "password": "Password123"

  },

  "search": {

    "query": "Selenium WebDriver",

    "category": "Testing"

  }

}


๐Ÿง‘‍๐Ÿ’ป 3. Create Java POJO Classes (Optional)


Define classes matching the JSON structure (helps with type-safe mapping):


public class LoginData {

    private String username;

    private String password;


    // getters and setters

}


public class SearchData {

    private String query;

    private String category;


    // getters and setters

}


public class TestData {

    private LoginData login;

    private SearchData search;


    // getters and setters

}


๐Ÿ“ 4. Read JSON Data in Java


Using Jackson ObjectMapper:


import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;

import java.io.IOException;


public class JsonReader {


    public static TestData readTestData(String filePath) {

        ObjectMapper mapper = new ObjectMapper();

        try {

            return mapper.readValue(new File(filePath), TestData.class);

        } catch (IOException e) {

            e.printStackTrace();

            return null;

        }

    }

}


๐Ÿ”— 5. Use JSON Data in Selenium Tests

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


public class LoginTest {


    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();

        TestData testData = JsonReader.readTestData("src/test/resources/testdata.json");


        driver.get("https://example.com/login");


        // Use JSON data for test inputs

        driver.findElement(By.id("username")).sendKeys(testData.getLogin().getUsername());

        driver.findElement(By.id("password")).sendKeys(testData.getLogin().getPassword());

        driver.findElement(By.id("loginButton")).click();


        driver.quit();

    }

}


⚡ 6. Best Practices


Keep JSON in src/test/resources → easier to access in tests.


Use POJOs → type safety and clarity.


Validate JSON structure → catch errors before tests run.


Use JSON arrays for multiple test cases:


{

  "users": [

    { "username": "user1", "password": "pass1" },

    { "username": "user2", "password": "pass2" }

  ]

}



Combine with TestNG/JUnit → data-driven testing using @DataProvider.


๐Ÿ“Š 7. Example with TestNG DataProvider

@DataProvider(name = "loginData")

public Object[][] getLoginData() throws IOException {

    ObjectMapper mapper = new ObjectMapper();

    LoginData[] users = mapper.readValue(

        new File("src/test/resources/users.json"), LoginData[].class);

    

    Object[][] data = new Object[users.length][1];

    for (int i = 0; i < users.length; i++) {

        data[i][0] = users[i];

    }

    return data;

}


@Test(dataProvider = "loginData")

public void loginTest(LoginData user) {

    driver.get("https://example.com/login");

    driver.findElement(By.id("username")).sendKeys(user.getUsername());

    driver.findElement(By.id("password")).sendKeys(user.getPassword());

    driver.findElement(By.id("loginButton")).click();

}



This makes your Selenium tests fully data-driven.


๐ŸŽฏ Summary


Keep test data in JSON files.


Use Jackson or Gson to parse JSON.


Map JSON to POJOs for type safety.


Access JSON fields in Selenium tests instead of hardcoding data.


Integrate with TestNG/JUnit for data-driven tests.

Learn Selenium with JAVA Training in Hyderabad

Read More

Writing Test Results into Excel Files in Selenium

Reading Test Data from Excel using Apache POI

๐Ÿ”Œ Data Handling and Advanced Concepts

How to Create a Base Test Class in Java

Visit Our Quality Thought Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive