Saturday, April 30, 2011

DDT Code

//Data Driven Test Framework using Selenium and TestNG
//This Test performs search for the movie and looks for the attributes on the result page
//Data is read from the Excel SS - imdb_data1.xls
package script;

import com.thoughtworks.selenium.*;

//import org.junit.AfterClass;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;

import java.io.File;
import jxl.*;

public class dataProviderExample extends SeleneseTestCase{

@BeforeClass
public void setUp() throws Exception {
SeleniumServer seleniumserver=new SeleniumServer();
seleniumserver.boot();
seleniumserver.start();
setUp("http://www.imdb.com/", "*firefox");
selenium.open("/");
selenium.windowMaximize();
selenium.windowFocus();
}

@DataProvider(name = "DP1")
public Object[][] createData1() {
Object[][] retObjArr=getTableArray("test\\resources\\data\\imdb_data1.xls",
"DataPool", "imdbTestData1");
return(retObjArr);
}


@Test (dataProvider = "DP1")
public void testDataProviderExample(String movieTitle,
String directorName, String moviePlot, String actorName) throws Exception {
//enter the movie title
selenium.type("q", movieTitle);
//they keep switching the go button to keep the bots away
if (selenium.isElementPresent("nb15go_image"))
selenium.click("nb15go_image");
else
selenium.click("xpath=/descendant::button[@type='submit']");
selenium.waitForPageToLoad("30000");

//click on the movie title in the search result page
selenium.click("xpath=/descendant::a[text()='"+movieTitle+"']");
selenium.waitForPageToLoad("30000");
//verify director name is present in the movie details page
verifyTrue(selenium.isTextPresent(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));
}

@AfterClass
public void tearDown(){
selenium.close();
selenium.stop();
}

public String[][] getTableArray(String xlFilePath, String sheetName, String tableName){
String[][] tabArray=null;
try{
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();
}
}
}
catch (Exception e) {
System.out.println("error in getTableArray()");
}

return(tabArray);
}

}//end of class

1 comment: