Search code examples
javaseleniumtestngtestng-dataprovider

Using Test-ng priority when using data providers


I'm using a data provider with test-ng and I want the particular test to follow a series of steps for each of the elements within the data collection object.

The test:

For each element in the object, validate the form can input the values

The process therefore has the following:

  1. Open a web page (from the data)
  2. Check if element exists on the page
  3. Input the values

I have tried to use the following below, however, for each of the elements in the object it runs step 1 and then moves onto step 2 after, rather than following the process. I'm therefore asking whether or not it's possible to do a 'test step' approach using test-ng?

If there are 2 values in the Data it will execute Open twice and then move on to CheckElementExists

@Test (priority = 1, dataProvider = "Data")
public void Open(Data data) throws InterruptedException
{ 
    System.out.println("Step 1");
    this.module.open(data);
}

@Test (priority = 2, dataProvider = "Data")
public void CheckElementExists(Data data)
{
   System.out.println("TWO");
}

Solution

  • In this case, you can use Factory class.

    public class TestCase {
        Data data;
    
        @Factory(dataProvider = "Data")
        public TestCase(Data data){
            this.data=data;
        }
    
        @Test(priority = 1)
        public void Open() throws InterruptedException {
            System.out.println("Step 1");
            this.module.open(data);
        }
    
        @Test(priority = 2)
        public void CheckElementExists(Data data) {
            System.out.println("TWO");
        }
    }
    

    You need to mention group-by-instance = true in your testng suite xml file and run using the xml suite

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Test Suite New"  group-by-instances="true" configfailurepolicy="continue" preserve-order="true">
       <test name="Test Case">
          <classes>
    
             <class name="com.package.TestCase"></class>
    
          </classes>
       </test>
    </suite>