Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a xls file with 2 columns of test data. For each column I want to have a test method.

Now I know how to use dataprovider to read the xls file and provide the test data to a test method. But how can I use the same dataprovider for multiple tests?

I have below code and once i run this code getting error my Code is:

@Test(dataProvider="TestData")
public void test1(String Username ) throws InterruptedException
driver.findElement(By.xpath("//*[@id=\"UserName\"]")).sendKeys(Username);               
@Test(dataProvider="TestData")
public void test2(String Password)
driver.findElement(By.xpath("//*[@id=\"Password\"]")).sendKeys(Password);
driver.findElement(By.xpath("//[@id=\"columns\"]/div/div[1]/form/div[1]/div[2]/div[4]/input")).click();
@DataProvider (name="TestData")
public  Object [][] readexcel(Method method) throws BiffException, IOException 
File f=new File("E:\\Selenium\\Eclips\\NCPDP\\src\\TestData\\TestData.xls");
            Workbook w = Workbook.getWorkbook(f);
            Sheet s = w.getSheet("TestData");
            int rows = s.getRows();
            int columns =s.getColumns();
            String inputData[][]=new String[rows-1][columns];
            try {
                for(int i=1;i<rows;i++)
                    for(int j=0;j<columns;j++)
            Cell c=s.getCell(j,i);
            inputData[i-1][j]=c.getContents();
        catch(Exception e){
            e.getMessage();
        return inputData;

Output:

[RemoteTestNG] detected TestNG version 6.14.2 Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) on port 47795 Only local connections are allowed. Feb 12, 2019 1:15:28 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS FAILED: test1 org.testng.internal.reflect.MethodMatcherException: Data provider mismatch Method: test1([Parameter{index=0, type=java.lang.String, declaredAnnotations=[]}]) Arguments: [(java.lang.String) TESTQA41,(java.lang.String) Ncpdp@1234] at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45) org.testng.internal.Parameters.injectParameters(Parameters.java:796) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:982) org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

FAILED: test2 org.testng.internal.reflect.MethodMatcherException: Data provider mismatch Method: test2([Parameter{index=0, type=java.lang.String, declaredAnnotations=[]}]) Arguments: [(java.lang.String) TESTQA41,(java.lang.String) Ncpdp@1234] at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45) org.testng.internal.Parameters.injectParameters(Parameters.java:796) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:982) org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

=============================================== Default test

Tests run: 2, Failures: 2, Skips: 0

=============================================== Default suite Total tests run: 2, Failures: 2, Skips: 0

Hello Ishita, Thanks for your reply, please check i have edited the post with full excption details – satish trimukhe Feb 12, 2019 at 8:00

If your DataProvider is returning two parameter you need to pass those two parameters to your @Test annotation.

Your solution

@Test(dataProvider="TestData")
public void test1(String Username,String Password) throws Exception
driver.findElement(By.xpath("//*[@id=\"UserName\"]")).sendKeys(Username);          
driver.findElement(By.xpath("//*[@id=\"Password\"]")).sendKeys(Password);
driver.findElement(By.xpath("//[@id=\"columns\"]/div/div[1]/form/div[1]/div[2]/div[4]/input")).click();
                Yes you are right, but my concern is i want to use separate Test annotation for every column available in xls file. can we use multiple Test annotation for single data provider. if yes, then how we can implement in my code?
– satish trimukhe
                Feb 12, 2019 at 9:54
                @satishtrimukhe You might be in wrong direction of using DataProvider scope, Excel Reader and many more. What you want to suppose to do is not correct way,
– Ishita Shah
                Feb 12, 2019 at 11:00
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.