RE: Is it possible using two two excel file for executing a single testcase?

Answered

Respected All,

I have a problem, we can preform a data driven testing using single excel file but I want to perform data driven testing using two excel file for a single test case.

Till now I am not able to do so. please help me . It is very urgent.

biswajit Train Asked on November 13, 2014 in Selenium WebDriver.

Hello Biswajit

that’s really strange requirement i would say , why in any case you require test data from different excel sheet ?

always keep it simple:)

on November 14, 2014.
Add Comment
2 Answers
Best answer

Hello All,

The problem solved with the help of my friends,

Please follow the steps:-

public class LoginGmail {

public WebDriver driver;

@BeforeClass
public void setUp(){
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(“https://www.google.co.in/”);
driver.findElement(By.id(“gb_70”)).click();
}

@Test(dataProvider = “login”)
public void gmailLogin(String UserName, String Password){

driver.findElement(By.id(“Email”)).sendKeys(UserName);
driver.findElement(By.id(“Email”)).clear();
driver.findElement(By.id(“Passwd”)).sendKeys(Password);
driver.findElement(By.id(“Passwd”)).clear();
driver.findElement(By.id(“signIn”)).click();
}

@DataProvider(name = “login”)
publicĀ  Object[][] loginToGmail() throws IOException {
System.out.println(new File(“.”).getAbsolutePath());
Object[][] cellData = ExcelUtils.getInstance().readData(“src/Login.xls”, 0, “src/LoginPass.xls”, 0);
System.out.println(“MMMMMM : ” + cellData);
return cellData;
}

@AfterClass
public void tearDown(){
driver.close();
driver.quit();
}

}

and My Excel Utils is:-

public class ExcelUtils {

private static ExcelUtils excelUtils = new ExcelUtils();

private ExcelUtils() {

}

public static ExcelUtils getInstance() {

return excelUtils;
}

public Object[][] readData(String workbook, int sheet, String workbook1, int sheet1) throws IOException {
Object[][] data = new Object[0][0];

HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(new File(workbook)));
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(sheet);

HSSFWorkbook hssfWorkbook1 = new HSSFWorkbook(new FileInputStream(new File(workbook1)));
HSSFSheet hssfSheet1 = hssfWorkbook1.getSheetAt(sheet1);

Row headerRow = hssfSheet.getRow(0);
StringBuffer headerStringBuffer = new StringBuffer(“WorkBook Header\n”);
for (Iterator<Cell> iterator = headerRow.iterator(); iterator.hasNext();) {
Cell cell = (Cell) iterator.next();
headerStringBuffer.append(cell + “|”);
}

System.out.println(headerStringBuffer);

int maxRowNum = hssfSheet.getLastRowNum();
int maxCellNum = headerRow.getLastCellNum();

System.out.println(“Row Sise “+maxRowNum+” Clo Size “+maxCellNum);

data = new Object[maxRowNum][maxCellNum+1];
StringBuffer dataStringBuffer = new StringBuffer(“WorkBook Data\n”);

for (int row = 1; row <= maxRowNum; row++) {

dataStringBuffer.append(“\n”);
for (int cell = 0; cell < maxCellNum; cell++) {
Object cellValue;
if(hssfSheet.getRow(row).getCell(cell)!=null){
cellValue = hssfSheet.getRow(row).getCell(cell);
}else{
cellValue=””;
}

Object cellValue1;
if(hssfSheet1.getRow(row).getCell(cell)!=null){
cellValue1 = hssfSheet1.getRow(row).getCell(cell);
}else{
cellValue1=””;
}

//Object cellValue = hssfSheet.getRow(row).getCell(cell);

dataStringBuffer.append(cellValue.toString() + “|”);
data[row – 1][cell] = cellValue.toString();
data[row – 1][cell+1] = cellValue1.toString();
System.out.println(“ROW :”+(row-1)+”Col :”+cell +”Data : “+data[row – 1][cell]);
}
}
System.out.println(dataStringBuffer);
return data;

}

}

Train Answered on November 18, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.