2

I created a testdata in Excel with multiple test cases. Ex:

#UserName#      #Password#     #PhoneNumber#
invalidUsername validPassword    validPhoneNumber
vaidUsername    invalidPassword  vaildPhoneNumber
vaildUsername   vaildPassword    invailidPhonenumber

When I perform the tests, Each test case will show an error message according the error. Ex:

"Please enter valid username" 
"Please enter correct password" 
"please enter correct phone number"

I'm using TestNG framework. I want to use the @dataprovider method and @test to execute my tests. Is it possible to use one dataprovider for multiple test cases?

Example test I wanted to perform is follow

        @Test(priority=1, dataProvider="getFormTestData")
        public void invalidUsername(String firstName, String lastName, String phoneNumber) {
        System.out.println("Please enter valid username");
        }     

       @Test(priority=2, dataProvider="getFormTestData")
        public void invalidPassword(String firstName, String lastName, String phoneNumber) {
        System.out.println("Please enter valid Password");
        }    

       @Test(priority=3, dataProvider="getFormTestData")
        public void invalidPhoneNumber(String firstName, String lastName, String phoneNumber) {
        System.out.println("Please enter valid Phone Number");
        }    

I'm reading the excel file by each row and column.

        public Object[][] getFormTestData(){
        Object data[][] = UtilPage.getTestData(sName);
        return data;
    }
  • Yes you can. you have to use only one test method. it will execute that test method as many rows available in our sheet (in your case you have 3 combination ). so it will execute same test method 3 time – NarendraR Feb 12 '18 at 7:25
  • @NarendraR I want to execute each row for each method. Is it possible? I was trying Factory annotation. But I'm getting Can't invoke public java.lang.Object[][] com.form.test.testcases.getFormTestData(). either make it static or add a no-args constructor to your class error. – user3174886 Feb 12 '18 at 7:32
  • It's possible, but not without a fair amount of extra work. You would need to use the test class initialization to load your data provider. The data provider would need to be static, and you would need to have each test look up the specific row required. – Kate Paulk Feb 15 '18 at 12:46
1

You might find this material really useful.

You need to define a mechanism to read your excel file (which is described in the link above)

Try to use project path rather than absolute paths like in the example in case you are looking for CI integration.

  • 1
    The OP already has that part working - they want to know how to use the same data provider and spreadsheet for different test cases. – Kate Paulk Feb 15 '18 at 12:35
  • @KatePaulk Hey, I would suggest you write a method to your excel reader class to read another string(column) called filter and then you can specify which test uses which row of data in that method. – Jithu Paul Feb 15 '18 at 16:05
0

You need to pass the below arguments:

(String firstName, String lastName, String phoneNumber) inside your script to make it to read the data from excel.

Ex: driver.findElementByXPath("//input[@title='Search']").sendKeys(firstName);

0

You can add the expected error message in another column in your excel file like this: #UserName# #Password# #PhoneNumber# #expectedMessage#. Then in your test compare the actual error message with the expected message from your excel file. In that way your test will be executed n times with different data where n= number of rows in the excel file.

@Test(dataProvider="getFormTestData")
public void formTest(String firstName, String lastName, String phoneNumber, 
                     String expectedMessage) {
    populateFormData(firstName,lastName,phoneNumber);
    String actualErrorMesage=getErrorMessage();
    assertEquals(actualErrorMesage,expectedMessage);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.