When our tests are mostly driven by different varieties of test data, so in that case, we need to develop a convenient technique in which data parsing and throwing parsed data to the application become efficient, fast and time-saving. You must have heard about maintaining test data in excel sheet and using POI or JXL jar to read the excel sheet. We will discuss the excel sheet technique too, let me begin with the JSON data parsing technique to maintain the test data in Selenium WebDriver.
Suggested Readings: Capture Screenshots in Selenium using Robot Class
What is JSON?
JSON stands for JavaScript Object Notation.
JSON is a light-weight, language independent, English text-based data exchange technique which is convenient to read and write the data by both, humans and machines.
What is the Structure of the JSON file?
JSON file has extension “.json”. It is represented in two structures one is Object and another one is the Array. JSON Object has the unordered collection of name-value pairs, whereas, the JSON Array has the unordered sequence of values. These values could be anything like String, Integer, Boolean, Null, etc.
Here is the sample example of the JSON data:
[ { "first name":"Avinash", "last name":"Mishra", "blog URL":"http://www.theavinashmishra.com" }, { "phone":[ { "type":"home", "number":"89749000000000" }, { "type":"office", "number":"89749000000000" } ] } ]
Significance of maintaining test data in JSON format
- Ease to maintain the test data
- Sometimes the excel sheet gets corrupted. We don’t face such issue with JSON
- Easy to understand
- Lightweight
- JSON data parsing is faster
- It is maintained in notepad or any other text file
How to implement JSON data parsing in the Selenium project?
There is a JAVA API which sets the environment for JSON data parsing, data generation, data transformation, and data query. Read complete architecture of JSON Simple here.
Either you can download the JSON simple jar file or just put this dependency in your Maven project (Assuming you are using Maven as your build framework).
<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20090211</version> </dependency> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency>
Explanation of the JSON Simple API
JSON Simple API has the object and array implementation as JSONObject and JSONArray respectively for JSON data parsing. JSONObject has the Map view implementation of zero or more data in key-value pairs, whereas, JSONArray has the List implementation of the data.
JSON Data Parsing command to read test data
Below is the basic code which gives a command to read the data from the JSON file.
JSONParser parser = new JSONParser();
JSONArray jsonArr = (JSONArray)parser.parse(JSONArray jsonArr = (JSONArray)parser.parse(reader));
Object obj = jsonArr;
JSONObject jo = (JSONObject)obj;
String n = (String)jo.get(keyword);
Code Explanation JSON Data Parsing command
Above code first, create the instance of JSONParser then this JSONParser type casted to JSONArray. Further, we typecast JSONArray Object to JSONObject which further it reads each JSONElement. Ultimately, we can replicate test data in the console by type casting JSONObject to String.
Sample Program
We have one JSON file below (testdata.json) and we have to make a method which is responsible for JSON data parsing. We call that method wherever required to get the desired test data.
testdata.json file
[ { "first name":"Avinash", "last name":"Mishra", "Blog URL":"http://www.theavinashmishra.com" } ]
Java Program
package com.inviul.selenium.project; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.util.LinkedHashMap; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class JSONDataExample { String n; public String readJSONData(String keyword) throws IOException, ParseException{ Reader reader = new FileReader("data/testdata.json"); JSONParser parser = new JSONParser(); JSONArray jsonArr = (JSONArray)parser.parse(reader); for(Object obj : jsonArr){ JSONObject jo = (JSONObject)obj; n = (String)jo.get(keyword); } return n; } public static void main(String[] args) throws IOException, ParseException{ JSONDataExample e = new JSONDataExample(); String a = e.readJSONData("Blog URL"); System.out.println(a); } }
Console Output
Hey Thanks a lot Avinash, for this informative article. Keep Writing more knowledgeable Articles.
Thanks Babit.
nice article useful to me.
nice article useful to me and keep posting new articles.