Concept of Map and HashMap

Map is an interface in Java that works on a Key and Value pair concept, which cannot contain duplicate keys and each Key maps to, at the most, one value. HashMap is the implementation class of Map, which we will be using to store the data from the Excel sheet, which will be related to test steps of a particular testcase. First of all, we will just require a Java HashMap to extract the test steps. One more testcase has been added to show what happens when only a HashMap is used to extract multiple test case steps.

The following code shows how to extract data in HashMap from the Framework.xls:

  public static void main(String[] args) {
Fillo fillo = new Fillo();
Map<String, String> testcaseData = new HashMap<String, String>();
Connection conn = null;
Recordset rcrdset = null;
try {
conn = fillo
.getConnection("C:\SeleniumWD\src\main\resources\Framework.xlsx");
String query = "Select * from TestCases where TestCaseID in (select TestCaseID from TestConfig where Execute_Flag='Y')";
rcrdset = conn.executeQuery(query);
while (rcrdset.next()) {
testcaseData.put("TestCaseID", rcrdset.getField("TestCaseID"));
testcaseData.put("Keyword", rcrdset.getField("Keyword"));
testcaseData.put("Object", rcrdset.getField("Object"));
testcaseData.put("Data", rcrdset.getField("Data"));
}
System.out.println(testcaseData);
} catch (FilloException e) {
throw new FilloException("Error in query processing");
} finally {
rcrdset.close();
conn.close();
}
}
}


The output of this program is
{Keyword=click, TestCaseID=TC002, Object=LOGOUT, Data=NULL}

As seen from the preceding output, the last test step of the test case TC002 is fetched into the HashMap. This happens because a HashMap does not allow duplicate values in the Key and it overwrites the last value every time a duplicate Key appears. Hence, we get only the last test step, since all initial steps were overwritten.

To overcome this issue, we require an ArrayList of HashMaps, which can be done using the following code:

public class ExtractAllData {

public static void main(String[] args) throws FilloException {
Fillo fillo = new Fillo();
Map<String, String> testcaseData = new HashMap<String, String>();
List<Map> teststepList = new ArrayList<Map>();
Connection conn = null;
try {
conn = fillo
.getConnection("C:\SeleniumWD\src\main\resources\Framework.xlsx");
String query = "Select * from TestCases where TestCaseID in (select TestCaseID from TestConfig where Execute_Flag='Y')";
Recordset rcrdset = null;
rcrdset = conn.executeQuery(query);
while (rcrdset.next()) {
testcaseData = new HashMap<String, String>();
testcaseData.put("TestCaseID", rcrdset.getField("TestCaseID"));
testcaseData.put("Keyword", rcrdset.getField("Keyword"));
testcaseData.put("Object", rcrdset.getField("Object"));
testcaseData.put("Data", rcrdset.getField("Data"));
teststepList.add(testcaseData);
}
System.out.println("List Content size: " + teststepList.size());
System.out.println("List Contents: " + teststepList);
rcrdset.close();
conn.close();
} catch (FilloException e) {
throw new FilloException("Error in query");
}
}
}
Do not forget to put this line of code in the recordSet iteration:                               testcaseData = new HashMap<String, String>(); 
Without this line, the teststepList will contain 12 duplicated entries of the last test step, which is not correct.

When the preceding code executes, we will have all the test steps that have to be executed. This completes the extraction process.

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

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