Extracting JSON data into Java objects

Now that the basic syntax has been covered, we will start building the JSON DataProvider method. First, we need a file I/O method to read the JSON data from a file. The parameter to the method will be the filename, including the path and string type. The method will be static and return JSONObject. Here is the code sample:

/**
* extractData_JSON - method to extract JSON data from a file
*
* @param file (including path)
* @return JSONObject
* @throws Exception
*/
public static JSONObject extractData_JSON(String file) throws Exception {
FileReader reader = new FileReader(file);
JSONParser jsonParser = new JSONParser();

return (JSONObject) jsonParser.parse(reader);
}

In cases where users might want to extract only specific sets of JSON data, as when filtering for specific test cases, they could create a wrapper method around the extractData_JSON method that would allow a parameter to be used as a filter. This method would also be static and return a JSONArray. Here is the code sample:

/**
* fetchData - method to get only the data that matches the filter

* @param file (including path)
* @param filter
* @return JSONArray
* @throws Exception
*/
public static JSONArray fetchData(String file,
String filter)
throws Exception {

JSONArray testData = (JSONArray) extractData_JSON(file).get(filter);

return testData;
}

The fetchData method to be used as the DataProvider will be constructed to support the data-driven test model. What that means is the parameter to the fetchData method, java.lang.Reflect.Method, will pass the test method name to the fetchData method and return only the sets of JSON data for that specific test case. In other words, each test method will include the DataProvider name as an attribute and it will automatically pull only the sets of data by the same name.

In essence, TestNG does the filtering for each test case so that only the correct sets of data are sequentially passed into the test cases that apply. Additional filtering can be added in the DataProvider.

This method will use the Java class JSON.simple, which provides methods for processing, reading, and writing JSON data using JSONArray and JSONObject types.

Now, let's look at the method structure of this DataProvider:

// global variables to be "set" later outside the DataProvider Class
public static
String dataFile = "";

/**
* fetchData - generic DataProvider method that extracts data
* by JSON key:value pairs
*
* @param method
* @return Object[][]
* @throws Exception
*/
@DataProvider(name = "myData_JSON")
public static Object[][] fetchData(Method method) throws Exception {
Object rowID, description;
Object result [][];
testCaseName = method.getName();
JSONArray testData = (JSONArray) extractData_JSON(dataFile)
.get(method.getName());

List<JSONObject> testDataList = new ArrayList<JSONObject>();

for
( int i = 0; i < testData.size(); i++ ) {
testDataList.add((JSONObject) testData.get(i));
}

// include Filter Placeholder

// exclude Filter Placeholder

// create object for dataprovider to return
Object[][] result = new Object[testDataList.size()]
[testDataList.get(0).size()];

for ( int i = 0; i < testDataList.size(); i++ ) {
result[i] = new Object[] { testDataList.get(i) };
}

return result;
}

The next code example is for later use in this framework, but we'll cover it now. There are third-party test reports that allow users to customize the report content, and having a row ID and description of the test allows users to filter within the report, name the screenshots with the test method rowID, add conditions to method setup and teardown routines, and so on. The following code example shows users how to "stuff" the rowID and description into the object be created in the DataProvider:

// add in rowID and description for later use  

try {
result = new Object[testDataList.size()]
[testDataList.get(0).size()];

for ( int i = 0; i < testDataList.size(); i++ ) {
rowID = testDataList.get(i).get("rowID");
description = testDataList.get(i).get("description");
result[i] = new Object[] { rowID, description,
testDataList.get(i) };
}
}

catch(IndexOutOfBoundsException ie) {
result = new Object[0][0];
}

return
result;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.254.122