Data Driven Framework
Data Driven Framework
Using data driven framework we can easily retrieve the data from excel sheet.
If we maintaining all our input data are in Excel sheet, it will be very easy to reuse.
In excel sheet have 2 different extensions like,
1. Xls
2. Xlsx
Xls:
Xlsx:
Jars:
1. Jxl
2. Apache POI
Jxl is an old one. So it will support only xls format.
Apache POI is a recent one. It will support both xls and xlsx.
Now a days we mostly used Apache POI jar
Steps:
switch (currentCell.getCellType()) {
case Cell.CELL_TYPE_STRING:
mapDatas.put(headerRow.getCell(j).getStringCellValue(),
currentCell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
mapDatas.put(headerRow.getCell(j).getStringCellValue(),
String.valueOf(currentCell
.getNumericCellValue()));
break;
}
}
mapDatasList.add(mapDatas);
}
// System.out.println(mapDatasList);
String s = mapDatasList.get(1).get("Username");
String s1 = mapDatasList.get(1).get("Password");
System.out.println(s);
System.out.println(s1);
} catch (Throwable e) {
e.printStackTrace();
}
Input data:
Output:
vengat16
Karthick
Here,
Excel is a workbook
Workbook class
Xlsx in this format we use XSSFworkbook
If the input data is number, then we have to convert into String because in sendkeys we
should pass only string.
Using valueof() method we can convert integer into string
For ex
Int num=12;
String s=String.valueof(num);
Cell data type, if it is 0 means it is a numeric and if it is 1 means it will be String
getType() is a method is used to print the data type of the input data.
Easy to maintain
User friendly
Reusable purpose
If we use excel sheet have some restrictions like limited rows and columns only we can
able read
Exercise:
1. Base class:
} else if (browser.equals("firefox")) {
System.setProperty("webdriver.gecko.driver", f.getAbsolutePath()
+ "/geckodriver.exe");
driver = new FirefoxDriver();
} else if (browser.equals("ie")) {
System.setProperty("webdriver.ie.driver", f.getAbsolutePath()
+ "/IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get((String) jsonObject.get("url"));
return driver;
}
}
return name;
}
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
switch (currentCell.getCellType()) {
case Cell.CELL_TYPE_STRING:
mapDatas.put(headerRow.getCell(j).getStringCellValue(),
currentCell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
mapDatas.put(headerRow.getCell(j).getStringCellValue(),
String.valueOf(currentCell
.getNumericCellValue()));
break;
}
}
mapDatasList.add(mapDatas);
}
} catch (Throwable e) {
e.printStackTrace();
}
return mapDatasList;
}
}
2. JUnit class:
}
@Test
public void verifyLogin() {
loginPage=new LoginPage(driver);
next=new NextPage(driver);
getScreenShot("facebookPage");
setText(loginPage.getTxtFirstName(), readValueFromExcelSheet().get(1)
.get("Firstname"));
getScreenShot("firstname");
setText(loginPage.getTxtSurName(), readValueFromExcelSheet().get(1)
.get("Surname"));
getScreenShot("lastname");
setText(loginPage.getTxtMobileNum(), readValueFromExcelSheet().get(1)
.get("Mobile"));
getScreenShot("mobile");
setText(loginPage.getTxtPassword(), readValueFromExcelSheet().get(1)
.get("Password"));
getScreenShot("password");
dropDownSelectVText(loginPage.getDrpDwnDay(),"16");
dropDownSelectVText(loginPage.getDrpDwnMonth(),"Nov");
dropDownSelectVText(loginPage.getDrpDwnYear(), "1993");
clickBtn(loginPage.getBtnMale());
clickBtn(loginPage.getBtnSignup());
}
@After
public void closeBrowser() {
driver.quit();
3. Main class:
@FindBy(id="u_0_e")
private WebElement txtPassword;
@FindBy(id="day")
private WebElement drpDwnDay;
@FindBy(id="month")
private WebElement drpDwnMonth ;
@FindBy(id="year")
private WebElement drpDwnYear;
@FindBy(id="u_0_i")
private WebElement btnMale;
@FindBy(id="u_0_m")
private WebElement btnSignup;
Input data:
Screenshot output: