Hi All
I am working to build a Test Framework using TestNG.
I am building it in such a way that I will have one Test Case script per Test Case.
Now when I am using the dataproviders in the @Test annotation at the class level it is just executing the first method with the pair of values that the data provider provides and the rest of the methods of the Test Class are executing simply without getting the data from the dataprovider.
What I exactly want is something like this::
This is the testclass that I have
@Test(dataProvider="DP")
public class GmailLogin extends CommTestClass {
public void enterUserName(String username, String password) {
// User name input field identification and data entered
WebElement usernametext = driver.findElement(By.name("Email"));
usernametext.sendKeys(username);
System.out.println("Inside logingmail i is :: " + i);
i++;
}
public void enterPassword(String username, String password) {
// Password input field identification and data entered
WebElement passwordtext = driver.findElement(By.name("Passwd"));
passwordtext.sendKeys(password);
System.out.println("Inside enterPassword i is :: " + i);
i++;
}
public void clickSignin(String username, String password) {
// Sign in button identification and click it
WebElement signinbutton = driver.findElement(By.name("signIn"));
signinbutton.click();
System.out.println("Inside clickSiginin i is :: " + i);
i++;
try {
System.out.println("Sleeping");
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
In the above code CommTestClass contains all the configuration methods and DataProvider method.
and this is how I want this test to be executed::
1. Execute enterUserName with DataProvider set1
2. Execute enterPassword with DataProvider set1
3. Execute clickSignin with DataProvider set1
4. Execute enterUserName with DataProvider set2
5. Execute enterPassword with DataProvider set2
6. Execute clickSignin with DataProvider set2
My DataProvider and its supporting method is as follows:
//Data provider definition
@DataProvider(name = "DP")
public static Object[][] createData() {
System.out.println("Inside DP");
Object[][] retObjArr = getExcelData(
"C:\\Users\\mundraa\\Desktop\\gmailLoginTest.xls", "Sheet1",
"Login");
System.out.println("Returning DP");
return (retObjArr);
}
//This is only for test.
//Excel API to read test data from excel workbook
public static String[][] getExcelData(String xlPath, String shtName, String tbName) throws Exception{
String[][] tabArray=null;
Workbook workbk = Workbook.getWorkbook(new File(xlPath));
Sheet sht = workbk.getSheet(shtName);
int sRow,sCol, eRow, eCol,ci,cj;
Cell tableStart=sht.findCell(tbName);
sRow=tableStart.getRow();
sCol=tableStart.getColumn();
Cell tableEnd= sht.findCell(tbName, sCol+1,sRow+1, 100, 64000, false);
eRow=tableEnd.getRow();
eCol=tableEnd.getColumn();
System.out.println("startRow="+sRow+", endRow="+eRow+", " + "startCol="+sCol+", endCol="+eCol);
tabArray=new String[eRow-sRow-1][eCol-sCol-1];
ci=0;
for (int i=sRow+1;i<eRow;i++,ci++){
cj=0;
for (int j=sCol+1;j<eCol;j++,cj++){
tabArray[ci][cj]=sht.getCell(j,i).getContents();
}
}
System.out.println(tabArray);
return(tabArray);
}
The Excel table is like :
I also tried using the @Factory annotation to the constructor but could not do it the way I needed probably I'm doing something wrong.
Please Help
Sorry for such a big post.I'm a newbie for TestNG ;-)
Thanks & Regards
Ankit Mundra