top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Data Driven Testing using JSON in Selenium

 


WHY DATA DRIVEN TESTING:

Data-driven testing (DDT), also known as table-driven testing or parameterized testing. Data driven testing is important because there is always a need for testing with multiple sets of valid and invalid data. Data Driven testing helps keeping data separate from the test scripts and can be executed for different combinations of input test data and test results can be generated efficiently. Data Files may be in various formats and the most used formats are:

·         csv files

·         xls or xlsx files

·         xml files

·         json files

 

Let’s take a very simple example of login into an application. It is important to test the functionality with different sets of valid and invalid data to make sure that application can successfully handle all kinds of data.


JSON:

JSON stands for JavaScript Object Notation. JSON is text-based format. It uses plain text to represent data using a combination of key-value pairs, arrays and primitive data types like strings, numbers, Booleans and null. It gives a human readable collection of data that can be accessed in a logical and organized manner. JSON syntax is easy to use. Since its syntax is simple and light weighted, it executes the response in faster way. Also, JSON is best tool for sharing data of any size. This is because JSON stores the data in arrays which makes data transfer easier.


Why JSON for data driven testing:

Most of the times, for data driven testing Excel is used. Other medium can also be used to stored data into files such as csv, xml, text, json file etc. The main advantage of using Excel is that it is effective at managing huge sets of data and is easy to use. But there are some drawbacks as well. To use excel for data driven, the system where the tests are run should have Microsoft Office installed. 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.


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. JSON is best when working with data at scale.

 

 

STEPS TO USE JSON FILE AS DATATABLE IN SELENIUM:

In this blog post, let us take the example of testing the feature of logging into Numpy Ninja DS Algo application. We will be using JSON file for this data driven approach. The login username and password are stored in the JSON file and sent to the application and the result of the test is updated in the JSON file.

 

Let us go through the steps needed to use JSON as datatable in selenium.

1.      First step is identifying the test scenario.

Test Scenario: To test the login functionality using different sets of valid and invalid data.

The login has two input fields, username and password. If the username and password is correct, user will be able to login to application successfully or else the login is unsuccessful.

Correct Username and Correct Password – Login Success

Incorrect Username and Correct Password – Login Failure

Correct Username and Incorrect Password – Login Failure

Incorrect Username and Incorrect Password – Login Failure

 

2.      In Maven Project, add the following dependency in pom.xml

 

 

3.      Create the JSON file for login test data. The login test is verified with one set of correct username and password and three sets of incorrect data in JSON file.

 

 

 

4.      Use any JSON Validator to make sure the JSON created is valid. It is helpful to identify invalid or incorrect JSON syntax. Validation and error messages will be notified if there are any. Copy and paste JSON directly into the editor to have it formatted and validated instantly.

 


 

5.      Next is to read data from the JSON file.

Create a JSONParser instance to parse the JSON file. JsonArray is used to parse the JSON, starting with the array brackets.

 


 

 

6.      The .get method is used to access the values in the JSON by index


 

7.      The login method executes the login function with different sets of input JSON data and returns the execution status (Pass or Fail).

8.      To write into the JSON file, .put method is used. To write the result as a string in JSON file, toJsonString() method is used.

 

 


 

9.      The scenario discussed will run for 4 iterations, validating the inputs and writing the login pass or fail result in the ‘result’ key of the JSON file. The JSON file is updated with the result after execution.

 

Conclusion:

In this blog post, it is mentioned how to use JSON as datatable with login functionality as an example. 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.

 

 

210 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page