Search code examples
javaseleniumselenium-webdrivertestngtestng-dataprovider

Run all Test for one iteration of dataprovider and so on till the end of data in data provider


actually data provider have date and brand name so my first @test can only select date and brand name then in second @test it will fetch data against the same date and brand name and on second iteration first @test take second date and second brand name from @dataprovider and then second @test will do their own work and so on please help me thanks in advance. example is given below :

`@DataProvider
  public static Object[][] dp() {
    return new Object[][] {
      new Object[] { "23", "Online" },
      new Object[] { "24", "Online" },
    };
  }
@Test(dataProvider = "dp")
  public void DateAndBrand(String date,String game) throws InterruptedException{
System.out.println(date"  "+game)
}
@Test
  public void CheckDifference(){
System.out.println("in second Test");
} 

I want output something like :

23  Online 
in second Test
24  Online
in second Test

My testng.xml file is given below :

<suite name="Automation" parallel="false">
<test name="Data Difference">
<classes>
 <class name="monitoring.DataCompareAuto">
  <methods>
        <include name="DateAndBrand"/>
        <include name="CheckDifference" />
    </methods>
  </class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

Solution

  • Whenever we use the dataProvider attribute with @Test annotation that method is called the number of times the number of object in array returned by @DataProvider.

    To achieve the output that you are referring, you can use the ITestListener interface or TestListenerAdapter class.

    Using these listeners you can intercept each and every test method on start, success, faliure etc. You can perform any operation there.

    import org.testng.ITestContext;
    import org.testng.ITestListener;
    import org.testng.ITestResult;
    
    public class TestInterceptor implements ITestListener {
        @Override
        public void onFinish(ITestContext arg0) {
            // TODO Auto-generated method stub
    
        }
        @Override
        public void onStart(ITestContext arg0) {
            // TODO Auto-generated method stub
    
        }
        @Override
        public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
            // TODO Auto-generated method stub
    
        }
        @Override
        public void onTestFailure(ITestResult arg0) {
            // TODO Auto-generated method stub
    
        }
        @Override
        public void onTestSkipped(ITestResult arg0) {
            // TODO Auto-generated method stub
    
        }
        @Override
        public void onTestStart(ITestResult arg0) {
            // TODO Auto-generated method stub
    
        }
        @Override
        public void onTestSuccess(ITestResult arg0) {
            System.out.println(" Can perform CheckDifference operation here.");
    
        }
    
    }
    

    So your output in this case will be

    23  Online
     Can perform CheckDifference operation here.
    24  Online
     Can perform CheckDifference operation here.
    in second Test
     Can perform CheckDifference operation here.
    

    Do remeber to add the listener in your testng.xml file