Independent Software QA Testing Services

Data Driven Testing using JSON

What is Data Driven Testing?

An automated testing framework called “data-driven testing” keeps test data for apps in table or spreadsheet form. A single test script that can run tests against all the test data in the external file is helpful for automation testers.

The testing input data can be kept in one or more data sources, such as XLS, XLSX, XML, or JSON files.

Testing with various test data sets is necessary to confirm that a specific application capability is functioning as intended. For instance, in order to test whether the application’s login capability is functioning properly, we must supply both valid and invalid credentials. This ensures that the program functions as intended for both valid and invalid test data sets.

What is JSON?

JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data and is often used when data is sent from the server to web pages. JSON is a text format that is completely language-independent but uses conventions that are familiar to programmers of the C family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many other languages. These specific properties make JSON an ideal data-interchange language.

The two most important things to learn from JSON are its syntax rules and the data types. JSON syntax is derived from JavaScript object notation syntax:

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays

In JSON, values must be one of the following data types:

  • a string
  • a number
  • an object
  • an array
  • a boolean
  • null

JSON is one of the most common formats for exchanging data over the internet. And even though most APIs work with JSON, there’s no guarantee that the consumer will be able to interact with data in this format. A schema aims to fix all those pain points.

Why use JSON for Data-Driven Testing?

For most data-driven testing frameworks we have used Excel – Apache POI. Although Excel is effective at managing data and is easy to use, it has several drawbacks. The system where the tests are being run has to have MS Office installed. Small, well-organized spreadsheet files load quickly and easily in Excel. It performs poorly when loading files that have 10,000 rows, and 100 or more columns, and some of these columns are filled with unstructured material, such as reviews or descriptions. And excel sheets become so heavy and turn into large files, so scaling your test data with excel is not ideal. On the other hand, when working with data at scale, JSON is the best standard.

Since the majority of contemporary APIs are RESTful, JSON input and output are supported natively. A number of database technologies, including most NoSQL variants, support it. Additionally, working with it in most computer languages is substantially simpler.

One of the best things is that the JSON format supports nested structures. The value in JSON can be of several sorts, such as an object or an array. Where log records have been written and preserved as repeated portions, JSON is primarily used. The foundation of CSV is not nested structures. The format is strictly two-dimensional and tabular. The approach is utilized as the vertical separation in CSV to handle the recurring log portions.

How to use JSON for Data Driven Testing?

There are two ways we can fetch data from the JSON file:
– using jsonobject
– using jsonpath

Let’s discuss both approaches.

How to fetch test data from JSON file using Jsonobject

Let’s go through the steps to fetch data using JsonObject
1. Create a testData.json file
Create a folder with the name “Resource” and add a subfolder as “TestData”, inside this TestData folder we can add our testData.json file. JSON should look like below:
				
					{
"Username" : "sid@thoughtframeworks.com",
"Password" : "test123"
}
				
			
2. Create a method as getJsonData() with the return type as JsonObject
Create a class as JsonReader and add a method “getJsonData” as mentioned below.
				
					public static JSONObject getJsonData() throws IOException, ParseException  {
	   
		//pass the path of the testdata.json file
		File filename = new File("TestData//testData.json");
		//convert json file into string
		String json = FileUtils.readFileToString(filename, "UTF-8");
		//parse the string into object
		Object obj = new JSONParser().parse(json);
    	//give jsonobject o that I can return it to the function everytime it get called
		JSONObject jsonObject = (JSONObject) obj;
		return jsonObject;
		
	}
				
			
3. Create a method that will fetch a value from JSON object using key
Now we need to create another method inside the class “JsonReader”, which will basically use the method “getJsonData” to fetch the required test data.
				
					public static String getTestData(String input) throws IOException, ParseException {
		String testdata;
		return testdata = (String) getJsonData().get(input); //input is the key
	}
				
			
To learn more about JSON please refer below link: https://www.json.org/
4. Use getTestData to fetch value
Usually the data specific to test cases should be kept inside the JSON, if you have any form of data which is required at the project level then it is always better to manage it using a properties file. Given a real-time scenario, let’s assume that you are automating Gmail Login, and we need to fetch the Username and password from the JSON file, and we will fetch data using the getTestData() method.
				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;

public class gmailLogin{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
      "C:\Users\xxxxxx\Desktop\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://accounts.google.com/signin");
      //identify email
      WebElement username = driver.findElement(By.name("identifier"));
      //fetch data from json for Username
      username.sendKeys(getTestData(“Username”));
      //identify password
      WebElement password = driver.findElement(By.name("password"));
      //fetch data from json for Password
      password.sendKeys(getTestData(“Password”));
      WebElement clickSubmit = driver.findElement(By.name("Submit"));
      clickSubmit.click();
      //close browser
      driver.close();
   }
}


				
			

How to fetch test data from a JSON file using JSON path?

Note: Use https://jsonpath.com/ to write your own jsonpath
1. Create a testdata.json file
Follow the steps mentioned in the above scenario on adding test data json file.
2. Create a method that will fetch a value from JSON object using key
				
					public static String getdatafromjson(String path) throws IOException {
	   
		File f = new File("TestData//testdata.json");
		String json = FileUtils.readFileToString(f, "UTF-8");
		String result;
		return result = JsonPath.read(json, path);
		
	}
				
			

Conclusion

Data-driven testing (DDT), also known as table-driven testing or parameterized testing. Usually, when we execute our test cases with multiple sets of data, it’s always preferable to implement data-driven testing. As JSON is easier to scale and lightweight as compared to excel or CSV, it is always advisable to use JSON to manage test data.

If your team is looking for support in data Driven testing setup then reach out to our team. We at ThoughtFrameworks make sure that the Data-Driven Testing Solutions we provide should be scalable and can be consumed across the enterprise.

Recommended Blogs