Software Development and QA Tips By QASource Experts

How To Automate Dynamic UI From JSON in Selenium?

Written by Timothy Joseph | Apr 4, 2022 4:00:00 PM

JSON is a format of keeping your data in a customized way that allows you to fetch data from JSON files, dynamically, at run time as per requirement. For automating dynamic UI from JSON in Selenium, a synchronized mapping between Json Objects & UI objects is required. Data in JSON is stored as Key-Value pairs and through the index of a particular attribute in JSON, we can fetch the corresponding value. These fetched values from Json then can be used dynamically in your automation selenium test script.

Additionally, please refer to the below code to fetch data from JSON File(DummyJsonFile.json) and use it in your automation script:

{
"organization" : "QASource",
var employeeDetails = [{
"Name" : "Mike",
"Role" : "QA Manager",
"Gender" : "Male"
},
{
"Name" : "Paul",
"Designation" : "Team Lead",
"Gender" : "Male"
}];
}

High Level Code:

JSONParser jsonParser = new JSONParser();
FileReader reader = new FileReader("DummyJsonFile.json");
Object parsedObject = jsonParser.parse(reader);
JSONObject employeeObject = (JSONObject) parsedObject;
String getOrganizationName = (String) employeeObject.get("organization");

Note: Alternative way is to use Pseudo WebDriver elements and use in automation script.