Input your search keywords and press Enter.

JSON data parsing technique for Parameterization in Selenium

Selenium JSON data parsing

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.

Selenium JSON data parsing

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:

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).

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.

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 

Java Program 

Console Output

 JSON data parsing sample

 

Join Inviul fb group

Avinash Mishra is the author of Inviul blog. He is a software engineer and blogger by choice. He loves to write blogs, and apart from blogging, he is interested in documentary film making, listening to music, traveling around the world and philanthropic activities.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *