DataProviders in TestNG

DataProviders in TestNG are used when we need to iterate over objects. These can be objects created from Java that include complex objects, objects from a property file or database, or simple string objects. Whenever a dataProvider attribute is specified in a @Test annotation, and a method with the annotation @DataProvider exists, the data from this method is passed into the @Test annotated method as a two-dimensional object array.

Let's look at a small example, which is enhanced from our earlier example, where we extracted data into a list of HashMaps. Here we go a step further and read the list of HashMaps in a two-dimensional object array. The following code demonstrates this concept. The code has been split into different sections for readability.

The following code fragment shows all the declarations for this program:

static Object[][] objectArray = {};
static Object[][] noRecords = {};
private static String tempParam;
static Map<String, String> tempMap = new HashMap<String, String>();
static List<HashMap> tempList = new ArrayList<HashMap>();
private static List<HashMap> input_data;
private static String browserName;
static IActionKeyword actKeyword;
private static Object actionKeyword;
private static Object inputData;
private static Object testcaseID;
static ICommand command;
static BrowserInvoker invoker;
private static WebDriver driver;

The following code shows the @Test method with the @Parameters annotation for supplying the browser name and the @AfterTest method:

@Test()
@Parameters("browserName")
public static void fetchValues(String param) {
tempParam = param;
}

@AfterTest
public static void quitBrowser() {
driver.quit();
}

The following code fragment shows the @Test method with the dependsOnMethods annotation and the data provider, Data1. This method has been made to depend on the fetchValues method using the dependsOnMethods attribute:

@Test(dataProvider = "Data1", dependsOnMethods = "fetchValues")
public void getValues(String testCaseID, String teststepID,
String action, String data, String keyword) {
System.out.println("First Extract: " + testCaseID + ": " +
teststepID + ": " + action + ": " + data + ": " + keyword);
List<String> browsers = new ArrayList<String>();

browsers.add(tempParam);
String timestamp = new SimpleDateFormat("yyyyMMMddHHmmss")
.format(new java.util.Date());

System.out.println("Starting " + browserName + timestamp);
actionKeyword = keyword;
inputData = data;
testcaseID = testCaseID;
System.out.println("Action Keyword: " + testCaseID);
System.out.println("Action Data: " + data);

switch (keyword) {
case "openbrowser":
try {
actKeyword = new OpenBrowser();
} catch (IOException e) {
throw new IOException();
} catch (FilloException e) {
throw new FilloException("Error in query");
}
command = new NewBrowser(actKeyword);
invoker = new BrowserInvoker(command);
driver = invoker.open(browsers);
break;
case "navigate":
try {
actKeyword = new OpenURL();
} catch (IOException e) {
throw new IOException();
} catch (FilloException e) {
throw new IOException();
}
command = new OpenURLCommand(actKeyword);
NavigatorInvoker invoker1 = new NavigatorInvoker(command);
driver = invoker1.navigate(driver, data);
break;
}
}

The following code shows the actual DataProvider method annotated with the @DataProvider annotation. This annotation is identified with the name Data, which can be referenced in the @Test annotation:

@DataProvider(name = "Data1")
public static Object[][] exec() {
String timestamp = new SimpleDateFormat("yyyyMMMddHHmmss")
.format(new java.util.Date());
System.out.println("Starting " + tempParam + timestamp);
Connection conn = null;
Fillo fillo = new Fillo();
Recordset rs = null, rs1 = null;
try {
conn = fillo.getConnection(System.getProperty("user.dir")
+ "\src\main\resources\Framework.xlsx");
String testConfig = "select * from TestConfig where Browser='"
+ tempParam + "'";
String allTestcases = "select * from TestCases";
rs = conn.executeQuery(allTestcases);
String strQuery2 = "select * from TestCases where TestCaseID='";
rs = conn.executeQuery(testConfig);
for (int i = 0; i < rs.getCount(); i++) {
rs.next();
System.out.println(rs.getField("Browser"));
System.out.println(rs.getField("Execute_Flag"));
if (rs.getField("Execute_Flag").equalsIgnoreCase("y")) {
rs1 = conn.executeQuery(testcases
+ rs.getField("TestCaseID") + "'");

System.out.println("Rs1 count: " + rs1.getCount());
for (int j = 0; j < rs1.getCount(); j++) {
rs1.next();
tempMap = new HashMap<String, String>();
tempMap.put("TestCaseID", rs1.getField("TestCaseID"));
tempMap.put("TestStepID", rs1.getField("TestStepID"));
tempMap.put("Object", rs1.getField("Object"));
tempMap.put("Data", rs1.getField("Data"));
tempMap.put("Keyword", rs1.getField("Keyword"));
tempList.add((HashMap) tempMap);
}
}
}
objectArray = noRecords;
objectArray = new Object[tempList.size()][5];
for (int i = 0; i < tempList.size(); i++) {
objectArray[i][0] = tempList.get(i).get("TestCaseID");
objectArray[i][1] = tempList.get(i).get("TestStepID");
objectArray[i][2] = tempList.get(i).get("Object");
objectArray[i][3] = tempList.get(i).get("Data");
objectArray[i][4] = tempList.get(i).get("Keyword");
}
} catch (FilloException e) {
return noRecords;
}
String timestamp1 = new SimpleDateFormat("yyyyMMMddHHmmss")
.format(new java.util.Date());
System.out.println("Ending " + tempParam + timestamp1);
return objectArray;
}

..................Content has been hidden....................

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