1

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.

ssharma
  • 935
  • 2
  • 19
  • 43

2 Answers2

3

You can use JsonPath to make your code even easier.

    @DataProvider(name = "body")
    public Object[] body() throws IOException{
        JsonPath path = JsonPath.from(new File("./TestDataFile.json"));
        List<HashMap<String, Object>> maps = path.getList("TestList");
        Object[] hashMaps = maps.toArray();
        return hashMaps;
    }

The above code will parse your JSON Array into List of HashMaps. Each of Map is the set of data you want to use.

Then, in your test, you need to declare that method annotated with @Test accepts HashMap<String, Object> as an argument, like this:

    @Test(dataProvider = "body")
    public void test(HashMap<String, Object> map)
    {
        System.out.println(map.get("display_name"));
    }

In order to get the data from the map, you need to use get() method like in the example above.

However, for element body you will have to do a little bit more. body element will be treated as HashMap<String, String>, so to get element from body like type, you have to do the following:

@Test(dataProvider = "body")
    public void test(HashMap<String, Object> map)
    {
        HashMap<String, String> body = map.get("body");
        String bodyType = body.get("type");
    }

Hope it helps!

Fenio
  • 3,528
  • 1
  • 13
  • 27
1

You can try using the below code. it worked when i used ur json.

    public class NewTest {
        @DataProvider(name = "JsonParser")

        public static Object[] credentials() {

            Object[] data = null;
// I saved the Json in a file.
            String filename = "YourFilePath";
            try {
                JsonObject jObject = new JsonParser().parse(new FileReader(new File(filename))).getAsJsonObject();
                JsonArray jArray = jObject.getAsJsonArray("TestList");
                data = new Object[jArray.size()];
                for (int i = 0; i < jArray.size(); i++) {
                    System.out.println(jArray.get(i));
                    data[i] = jArray.get(i);
                }

            } catch (Exception e) {

            }
            return data;

        }

        // Here we are calling the Data Provider object with its Name

        @Test(dataProvider = "JsonParser")

        public void test(Object arrays) {

            System.out.println("From test class" + arrays);

        }
Arun Nair
  • 425
  • 3
  • 11