0% found this document useful (0 votes)
4 views

Data Driven Framework

Uploaded by

manuamu1405
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data Driven Framework

Uploaded by

manuamu1405
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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:

 Xls is a old one (i.e.) it we used before 2007


 In Xls have some limitations like,
1. It will support only 512(2^9) rows and 65536 (2^16) columns
2. It will not support macros(xml format)
3. Colors also limited (i.e.) up to 56 colors
4. It will support only xls format

Xlsx:

 In this extensions only we have using recently


 Xlsx support up to 2^14 rows and 2^20 columns
 It will support macros
 Colors are unlimited
 It will support both xls and xlsx format

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:

1. Download Apache POI jar


2. Set path (Excel sheet location)
3. Sheet name
4. Rows detail
5. Cell detail
6. Cell type(number, string, Boolean, blank or formula)
7. String conversion( if other than string)
Program:

To read data from Excel:

public class HotelSearchTest {


public static void main(String[] args) {

List<HashMap<String, String>> mapDatasList = new ArrayList();


try {
File excelLocaltion = new File("./Excel/Adactin.xlsx");

String sheetName = "Adact";

FileInputStream f = new FileInputStream(


excelLocaltion.getAbsolutePath());
Workbook w = new XSSFWorkbook(f);
Sheet sheet = w.getSheet(sheetName);
Row headerRow = sheet.getRow(0);
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
Row currentRow = sheet.getRow(i);
HashMap<String, String> mapDatas = new HashMap<String, String>();
for (int j = 0; j < headerRow.getPhysicalNumberOfCells(); j++) {
Cell currentCell = currentRow.getCell(j);

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.

Advantages of Data driven:

 Easy to maintain
 User friendly
 Reusable purpose

Disadvantages of Data driven:

 If we use excel sheet have some restrictions like limited rows and columns only we can
able read

Exercise:

Facebook registration using data driven framework

1. Base class:

public class Base {


static WebDriver driver;
WebDriverWait wait;

static File f1 = new File("./JSON/Configuration.json");

public static WebDriver getDriver() {


JSONObject jsonObject = JSONReadFromFile();
String browser = (String) jsonObject.get("browser");

File f = new File("./driver");


if (browser.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", f.getAbsolutePath()
+ "/chromedriver.exe");
driver = new ChromeDriver();

} 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;
}

public boolean elementToBeVisible(WebDriver driver, int time,


WebElement element) {
boolean flag = false;
try {
wait = new WebDriverWait(driver, time);
wait.until(ExpectedConditions.visibilityOf(element));
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}

public boolean elementFound(WebDriver driver, int time, WebElement element) {


boolean res = false;
driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);
try {
res = element.isDisplayed();
} catch (Exception e) {
e.printStackTrace();
}
return res;

public boolean elementFound(WebElement element) {


boolean b = false;
try {
b = element.isDisplayed();
} catch (Exception e) {
e.printStackTrace();
}
return b;

public void setText(WebElement element, String name) {


if (name != null && elementFound(element)) {
element.clear();
element.sendKeys(name);
}

public String getText(WebElement element) {


String name = null;
if (elementFound(element)) {
name = element.getAttribute("value");

}
return name;
}

public void clickBtn(WebElement element) {


if (elementFound(element)) {
element.click();
}

public static JSONObject JSONReadFromFile() {


JSONParser parser = new JSONParser();
JSONObject jsonObject = null;
try {

Object obj = parser.parse(new FileReader(f1.getAbsoluteFile()));

jsonObject = (JSONObject) obj;

} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}

public void dropDownSelect(WebElement element, String name) {


Select s = new Select(element);
s.selectByValue(name);
}

public void dropDownSelectVText(WebElement element, String name) {


Select s = new Select(element);
s.selectByVisibleText(name);
}

public void getScreenShot(String screenShotFileName) {


File screenShotLocation = new File("./screenshot/" + screenShotFileName
+ ".png");
TakesScreenshot screenshot = (TakesScreenshot) driver;
File file = screenshot.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(file, screenShotLocation);
} catch (IOException e) {
e.printStackTrace();
}
}

public static List<HashMap<String, String>> readValueFromExcelSheet() {


List<HashMap<String, String>> mapDatasList = new ArrayList();
try {
File excelLocaltion = new File("./Excel/Facebook.xlsx");

String sheetName = "Sheet1";

FileInputStream f = new FileInputStream(


excelLocaltion.getAbsolutePath());
Workbook w = new XSSFWorkbook(f);
Sheet sheet = w.getSheet(sheetName);
Row headerRow = sheet.getRow(0);
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
Row currentRow = sheet.getRow(i);
HashMap<String, String> mapDatas = new HashMap<String,
String>();
for (int j = 0; j < headerRow.getPhysicalNumberOfCells(); j++) {
Cell currentCell = currentRow.getCell(j);

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:

public class LoginPageTest extends Base {


static WebDriver driver;
LoginPage loginPage;
static Base base;
NextPage next;
@Before
public void launchBrowser() {
base = new Base();
driver=base.getDriver();

}
@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:

public class LoginPage {


static WebDriver driver;
@FindBy(id="u_0_2")
private WebElement txtFirstName;
@FindBy(id="u_0_4")
private WebElement txtSurName;
@FindBy(id="u_0_7")
private WebElement txtMobileNum;

@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;

public LoginPage(WebDriver ldriver) {


this.driver=ldriver;
PageFactory.initElements(driver, this);

public static WebDriver getDriver() {


return driver;
}

public WebElement getTxtFirstName() {


return txtFirstName;
}

public WebElement getTxtSurName() {


return txtSurName;
}

public WebElement getTxtMobileNum() {


return txtMobileNum;
}

public WebElement getTxtPassword() {


return txtPassword;
}

public WebElement getDrpDwnDay() {


return drpDwnDay;
}

public WebElement getDrpDwnMonth() {


return drpDwnMonth;
}

public WebElement getDrpDwnYear() {


return drpDwnYear;
}

public WebElement getBtnMale() {


return btnMale;
}

public WebElement getBtnSignup() {


return btnSignup;
}

Input data:
Screenshot output:

You might also like