Thursday, 20 February 2014

Datadriven + JXL API | TestNG | Method 3

JXL API
Download Jxl.jar

This is the continuation of one of the previous posts from me [ Link ].  In this method, we are using @dataProvider annotation on TestNG to fetch keywords from Excel and passing the arguments to Test class. I would always prefer this Method. Please follow the below example test class..

Method 3 


TYPE 1 - With Excel

@DataProvider | TestNG



package packagename;

import java.io.File;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class classname{
WebDriver driver;
private String baseUrl;

@DataProvider(name = "Test")
public Object[][] createPayId() throws Exception {
Object[][] retObjArr = getTableArray("C:\\data.xls", "Sheet1", "test1");
return (retObjArr);
}

@BeforeClass
public void BeforeClass() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
baseUrl = "http://www.google.co.in";

}

@Test(dataProvider = "Test", description = "Testing ")
public void Test01(String column1, String column2, String column3,
String column4) throws Exception {

driver.get(baseUrl + "/");
driver.findElement(By.id("gbqfq")).sendKeys(column1);
driver.findElement(By.id("gbqfq")).sendKeys(Keys.RETURN);
}

public String[][] getTableArray(String xlFilePath, String sheetName,
String tableName) throws Exception {
String[][] tabArray = null;

Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
Sheet sheet = workbook.getSheet(sheetName);
int startRow, startCol, endRow, endCol, ci, cj;
Cell tableStart = sheet.findCell(tableName);
startRow = tableStart.getRow();
startCol = tableStart.getColumn();

Cell tableEnd = sheet.findCell(tableName, startCol + 1, startRow + 1,
100, 64000, false);

endRow = tableEnd.getRow();
endCol = tableEnd.getColumn();
System.out.println("startRow=" + startRow + ", endRow=" + endRow + ", "
+ "startCol=" + startCol + ", endCol=" + endCol);
tabArray = new String[endRow - startRow - 1][endCol - startCol - 1];
ci = 0;

for (int i = startRow + 1; i < endRow; i++, ci++) {
cj = 0;
for (int j = startCol + 1; j < endCol; j++, cj++) {
tabArray[ci][cj] = sheet.getCell(j, i).getContents();
}
}
return (tabArray);
}

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

}


Create an Excel sheet that resembles this:



Test Console looks the same as below after Execution





TYPE 2 - Without Excel

@DataProvider | TestNG



package packagename;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class classname {
WebDriver driver;
private String baseUrl;

@DataProvider(name = "Test")
public String[][] y() {

return new String[][] { { "prashanth sams", "prashanth sams" },
{ "yeah", "prashanth sams" },
{ "seleniumworks", "prashanth sams" } };
}

@BeforeClass
public void BeforeClass() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
baseUrl = "http://www.google.co.in";

}

@Test(dataProvider = "Test")
public void Google(String actual, String expected) throws Exception {

driver.get(baseUrl + "/");
driver.findElement(By.id("gbqfq")).sendKeys(actual);
driver.findElement(By.id("gbqfq")).sendKeys(Keys.RETURN);

boolean b = driver.getPageSource().contains(expected);
Assert.assertTrue(b);

}

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

}

Test Console looks the same as below after Execution




1 comment: