Data-Driven Testing using Json

Reading Time: 2 minutes

In this blog, we will learn how to read data from JSON File and use them in our data-driven testing using Json framework.

What is Data-Driven Testing?

  • Data-driven testing is a test automation framework which stores data in a table or Excel sheet
  • The input data for testing, we can stored in data sources like  XLS, XLSX, XML or in JSON files. Here we will see how to manipulate data stored using JSON. 
  • It enables testers to build both positive and negative test cases into a single test.

What is JSON?

  • JSON stands for JavaScript Object Notation and it’s a lightweight format for storing and transporting data.
  • It stores the data in a text format in an organised way which is easy to access.
  • We can also have multiple sets of data in JSON. To do this, we can store multiple objects in square brackets. We call this as a JSON Array.
{
  "firstName": "John",
  "lastName": "Kennedy",
  "address": [
    {
      "street": "abc",
      "city": "Hyderabad",
      "state": "TL"
    },
    {
      "street": "xyz",
      "city": "Chennai",
      "state": "TN"
    }
  ]
}

Why JSON over Excel?

  • Most of data driven testing framework we have used Excel – Apache POI But there is other medium as well to store data into files such as csv, xml, json, text file, etc. 
  • Excel is good to manage data and to use but it comes with its own limitations. Like MS Office needs to be installed on the system where the tests are being executed. 
  • As the test servers has never bound to have such dependencies.
  • If test is run on Mac, then again there is a different problem.

JSON Maven dependency

  • Go to Maven dependency page and select “The latest Version dependency” and Download the latest version dependency.
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

Read Data From JSON File

  • Create a JsonParser instance to parse JSON into a tree structure with any FileInputStream  object (using the path of the source file) as a parameter:
JSONParser jsonParser= new JSONParser();
FileReader fileReader = new FileReader("src/Jsonfiles/employee.json");

//Read Json file
Object obj = jsonParser.parse(fileReader);
  • JsonArray is use to parse JSON which starts with Array brackets.
JSONObject jsonObject = (JSONObject)obj;

//Print JSON file
System.out.println(jsonObject);
  • Using the get method for accessing the values by index.
 for (int i =0;i<array.size();i++){
     JSONObject address = (JSONObject) array.get(i);

     String street = (String) address.get("street");
     String city = (String) address.get("city");
     String state = (String) address.get("state");

     System.out.println("STREET  "+street);
     System.out.println("CITY  "+city);
     System.out.println("STATE  "+state);
 }

Selenium Code to Handle JSON Files

 @Test(dataProvider="dp")
 void login(String data) throws InterruptedException {
     String user[] = data.split(",");
     driver.get("http://thedemosite.co.uk/login.php");
     driver.findElement(By.name("username")).sendKeys(user[0]);
     driver.findElement(By.name("password")).sendKeys(user[1]);
     driver.findElement(By.name("FormsButton2")).click();
     Thread.sleep(1000);

 }

 @DataProvider(name = "dp")
 public String[] readjson() throws IOException, ParseException {
     JSONParser jsonParser = new JSONParser();

     FileReader fileReader = new FileReader("src/Jsonfiles/testdata.json");
     Object obj = jsonParser.parse(fileReader);

     JSONObject jsonObject = (JSONObject) obj;
     JSONArray array = (JSONArray) jsonObject.get("userlogins");

     String arr[] = new String[array.size()];
     for (int i = 0; i < array.size(); i++) {
         JSONObject users = (JSONObject) array.get(i);
         String username = (String) users.get("username");
         String password = (String) users.get("password");

         arr[i] = username+","+password;
     }
     return arr;
 }

References:

https://www.tutorialspoint.com/software_testing_dictionary/data_driven_testing.htm


Knoldus-blog-footer-image

Written by 

Prajjawal is a QA Consultant. He is familiar with core concepts of manual & automation testing using tools like Selenium and Postman Also having knowledge of Python and Data Science. He is always eager to learn new and advanced concepts in order to improve himself. He likes to watch web series and play cricket.

1 thought on “Data-Driven Testing using Json3 min read

Leave a Reply

%d bloggers like this: