How can I pass Json File which contains a json array to TestNG data Provider and use it in my test to parametrize my test cases. this is a Json file which I want to pass to TestNG test cases as Data Provider :
{"TestList":[{
"display_name":"test1",
"type":"testlist",
"author":null,
"body":
{
"description":"test1 tl",
"type":"GENERIC"
},
"perm_read":"all",
"perm_write":"all",
},
{
"display_name":"test2",
"type":"testlist",
"author":null,
"body":
{
"description":"test2 tl",
"type":"GENERIC"
},
"perm_read":"all",
"perm_write":"all",
},
{
"display_name":"test3",
"type":"testlist",
"author":null,
"body":
{
"description":"test3 tl",
"type":"GENERIC"
},
"perm_read":"all",
"perm_write":"all",
}
]}
Here are the test and Data Provider in which I am trying to use Json file :
@Test(dataProvider = "body")
public void AddTestGroup() throws InterruptedException{
System.out.println("Parsed json ===" + TestData);
}
@DataProvider(name = "body")
public Object[][] body() throws IOException{
JSONParser parser = new JSONParser();
JSONObject jsonObject = null;
JSONArray json_array = null;
try {
Object obj = parser.parse(new FileReader("./TestDataFile.json"));
jsonObject = (JSONObject) obj;
json_array = (JSONArray) jsonObject.get("TestList");
}catch (IOException e) {
e.printStackTrace();
}
return json_array;
}
how can I do it so that json_array can have
json_Array[0] = {
"display_name":"test1",
"type":"testlist",
"author":null,
"body":
{
"description":"test1 tl",
"type":"GENERIC"
},
"perm_read":"all",
"perm_write":"all",
}
and
jason_array[1] = {
"display_name":"test2",
"type":"testlist",
"author":null,
"body":
{
"description":"test2 tl",
"type":"GENERIC"
},
"perm_read":"all",
"perm_write":"all",
},
and so on and can be used in my test cases.