You are on page 1of 2

package read.

excel;
import org.junit.AfterClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
//import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;
import java.io.File;
import jxl.*;
public class DataProviderExample {
public WebDriver driver;
@BeforeClass
public void setUp() throws Exception {
//System.setProperty("webdriver.chrome.driver", "D:/Pradeep/Selenium/chr
omedriver_win32/chromedriver.exe");
//driver=new ChromeDriver();
driver=new FirefoxDriver();
driver.get("http://www.imdb.com/");
driver.manage().window().maximize();
}
@DataProvider(name = "DP1")
public Object[][] createData1() throws Exception{
Object[][] retObjArr=getTableArray("C:/Users/Mani/workspace/Data driven/
src1/data.xls",
"DataPool", "imdbTestData1");
return(retObjArr);
}
@Test (dataProvider = "DP1")
public void testDataProviderExample(String movieTitle,
String directorName, String moviePlot, String actorName) throws Exce
ption {
//enter the movie title
driver.findElement(By.name("q")).sendKeys(movieTitle);;
driver.findElement(By.id("navbar-submit-button")).click();
Thread.sleep(3000);
//click on the movie title in the search result page
driver.findElement(By.xpath("/descendant::a[text()='"+movieTitle+"']")).
click();
Thread.sleep(3000);
//verify director name is present in the movie details page
//verifyTrue(selenium.isTextPresent(directorName));
Assert.assertTrue(driver.getPageSource().contains(directorName));
//verify movie plot is present in the movie details page
// verifyTrue(selenium.isTextPresent(moviePlot));
//verify movie actor name is present in the movie details page

// verifyTrue(selenium.isTextPresent(actorName));
System.out.println("Movie plot"+moviePlot);
System.out.println("Actor name"+actorName);
}
@AfterClass
public void tearDown(){
driver.quit();
}
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);
}
}//end of class