Monday, July 18, 2016
Saturday, July 16, 2016
Data Driven Approach - Using HashMap/Dictionary for Selenium automation
In this article, we will discuss how to create data dictionary or hash map in Java for data driven tests and read the data from the excel/csv file similar to dictionary object. Please go through below code example to read data from the external source using hashmap/dictionary or data driven approach. Please suggest or comment for better approach for data driven testing in java for Selenium.
package testingtest; import java.io.File; import java.io.IOException; import java.util.HashMap; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; class testclass { static String[][] getexceldatainArray; static HashMap<String, String> dictionary; public static void main(String[] args) throws IOException, BiffException { // Create an array based on data in excel file excelarrayclass tc = new excelarrayclass(); getexceldatainArray = tc.CreateArrayfromExcel("D:\\Testing.xls", "testing"); for (int i = 0; i < getexceldatainArray.length; i++) { dictionary = DataDictionaryFromArray(getexceldatainArray, i); // the keys will be value in the header column String username = dictionary.get("UserName"); /* * Perform the required operation using the dictionary values */ System.out.println("UserName " + username); } } public String[][] CreateArrayfromExcel(String WorkbookName, String SheetName) throws BiffException, IOException { String[][] arrTestDta; Workbook workbk = Workbook.getWorkbook(new File(WorkbookName)); Sheet wrkSheet = workbk.getSheet(SheetName); arrTestDta = new String[wrkSheet.getRows()][wrkSheet.getColumns()]; // Loop to get data from excel file into an array for (int i = 0; i < wrkSheet.getRows(); i++) { for (int j = 0; j < wrkSheet.getColumns(); j++) { Cell cell = wrkSheet.getCell(j, i); if (cell.getContents() != null) { arrTestDta[i][j] = cell.getContents(); } } } return arrTestDta; } public static HashMap<String, String> DataDictionaryFromArray( String[][] arrayforDictionary, int iRow) { // Create a hashmap to store the data as dictionary in key value form HashMap<String, String> dictionary = new HashMap<String, String>(); // get the size of the array, assuming being used in data driven test, // get the number of columns in the data file int iColCnt = arrayforDictionary[0].length; for (int i = 0; i < iColCnt; i++) { // mark the header column values as key and current column value as // key value pair dictionary.put(arrayforDictionary[0][i], arrayforDictionary[iRow][i]); } return dictionary; } }
Sunday, July 10, 2016
Code to read data from excel file - Java Selenium Data Driven frameworks
In Data driven framework for automation testing, data is stored in external file and helper/utility classes are created to interact with the data stored in external file. Data is normally stored in excel or csv files. Storing data in excel file is convenient as data for multiple workflows/module can be stored in different sheets of the same workbook. In the last post,code explaining how to read data from a csv file was explained. Click here to understand how to read data from a csv file.
In this post, we will understand how to interact with excel file. To interact with excel file, we need to add external jar files. Two common external file to interact with excel are :
- jxl.jar - Supports only xls format.
- org.apache.poi - Support both xls and xlsx format.
Download the external Jar file and add in the build path for the project. Below code shows how to read data from excel file using jxl.jar
- jxl.jar - Supports only xls format.
- org.apache.poi - Support both xls and xlsx format.
package testingtest; import java.io.File; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; class excelarrayclass { static String[][] getexceldatainArray; public static void main(String[] args) throws IOException, BiffException { // Create an array based on data in excel file excelarrayclass tc = new excelarrayclass(); getexceldatainArray = tc.CreateArrayfromExcel("D:\\Testing.xls", "testing"); } public String[][] CreateArrayfromExcel(String WorkbookName, String SheetName) throws BiffException, IOException { String[][] arrTestDta; Workbook workbk = Workbook.getWorkbook(new File(WorkbookName)); Sheet wrkSheet = workbk.getSheet(SheetName); arrTestDta = new String[wrkSheet.getRows()][wrkSheet.getColumn()] for (int i = 0; i < wrkSheet.getRows(); i++) { for (int j = 0; j < wrkSheet.getColumns(); j++) { Cell cell = wrkSheet.getCell(j, i); if (cell.getContents() != null) { arrTestDta[i][j] = cell.getContents(); System.out.println(cell.getContents()); } } } return arrTestDta; } }
Subscribe to:
Posts (Atom)