Data-Driven framework
In this type of framework Data is not hardcoded with script ,but data is provided from external source.
This external source can be Excel sheet, TXT file, Database etc.
Requirements:
- Test data has to be prepared.
- Install Testng
Pro's: As data is provided from external source same script can be run for different datasets.
Con's: Good efforts and technical expertise is required to create functions which connects to Database
Following code is an example of Data-driven Framework
package com.project1;
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.support.ui.Select;
import jxl.*;
import jxl.read.biff.BiffException;
public class DataDriven_Framework {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
//Login
driver.navigate().to("http://newtours.demoaut.com/");
try {
Workbook workbook = Workbook.getWorkbook(new File("D:\\seleniumworkspace\\SeleniumWebapplications\\src\\TestNG\\Excel1.xls"));
Sheet sheet = workbook.getSheet(0);
driver.findElement(By.name("userName")).sendKeys(sheet.getCell(0, 1).getContents());
driver.findElement(By.name("password")).sendKeys(sheet.getCell(1, 1).getContents());
driver.findElement(By.name("login")).click();
Select Passangers= new Select(driver.findElement(By.cssSelector("select[name='passCount']")));
Passangers.selectByVisibleText(sheet.getCell(2, 1).getContents());
Select Departingfrom = new Select(driver.findElement(By.cssSelector("select[name='fromPort']")));
Departingfrom.selectByVisibleText(sheet.getCell(3, 1).getContents());
Select FromMonth = new Select(driver.findElement(By.cssSelector("select[name='fromMonth']")));
FromMonth.selectByVisibleText(sheet.getCell(4, 1).getContents());
Select ArrivingIn = new Select(driver.findElement(By.cssSelector("select[name='toPort']")));
ArrivingIn.selectByVisibleText(sheet.getCell(6,1).getContents());
Select ToMonth = new Select(driver.findElement(By.cssSelector("select[name='toMonth']")));
ToMonth.selectByVisibleText(sheet.getCell(5,1).getContents());
driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[9]/td[2]/font/font/input")).click();
driver.findElement(By.name("findFlights")).click();
driver.findElement(By.name("reserveFlights")).click();
driver.findElement(By.name("passFirst0")).sendKeys(sheet.getCell(7, 1).getContents());
driver.findElement(By.name("passLast0")).sendKeys(sheet.getCell(8, 1).getContents());
driver.findElement(By.name("creditnumber")).sendKeys(sheet.getCell(9, 1).getContents());
driver.findElement(By.name("buyFlights")).click();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
hi ,
ReplyDeleteis jxl comes in java by default or an external library ?
import jxl.*;
import jxl.read.biff.BiffException;
Hi Sandamal,
ReplyDeleteYou need to download external jars for it. Thanks for your comment on this post.