dataprovider at class level

491 views
Skip to first unread message

Ankit Mundra

unread,
Aug 13, 2013, 10:01:01 AM8/13/13
to testng...@googlegroups.com
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

Krishnan Mahadevan

unread,
Aug 14, 2013, 12:21:57 AM8/14/13
to testng...@googlegroups.com
Ankit,

You are better off using the @Factory annotation in conjunction with the @DataProvider to get this done.


If you need a sample to get you started, please take a look at this blog post of mine : http://rationaleemotions.wordpress.com/2013/07/31/pretty-printing-with-testng/

It shows you how to use @Factory and data provider as well.




Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ankit Mundra

unread,
Aug 14, 2013, 1:25:39 PM8/14/13
to testng...@googlegroups.com
Hi Krishnan

Thanks for your reply.

But just as I had mentioned, I already tried using the Factory annotation on the constructor of the test script class. The output by it was something like this:

1. Enterusername data provider value 1
2. Enterusername data provider value 2
3. Enterpassword data provider value 1
4. Enterpassword data provider value 2

Which is not what I want. As I mentioned the test steps are like this

1. Enter username with data provider values 1
2. Enter password with data provider values 1
3. Click on sign in button
4. Enter username with data provider values 2
5. Enter password with data provider values 2
6. Click on sign in button

Need to achieve this.

Any other ideas if you have.

Thanks & regards
Ankit Mundra

Reply all
Reply to author
Forward
0 new messages