0

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");
}
Phorce
  • 4,424
  • 13
  • 57
  • 107

2 Answers2

1

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>   
Navarasu
  • 8,209
  • 2
  • 21
  • 32
0

As per your test, it is working fine because tests are designed in that way. As per your scenario, each step is a step and you set the priority as well. so it executed first for all data and second step for all data. it looks like a BDD style. You can try with any BDD framework like cucumber, jbehave, etc.

If you want to repeat all steps for each data using testng. Then combine all the steps in a single test then iterate over the data using data provider as given below.

@Test (priority = 1, dataProvider = "Data")
public void OpenAndCheck(Data data) throws InterruptedException
{ 
    System.out.println("Step 1");
    this.module.open(data);
    System.out.println("TWO");
}
Murthi
  • 5,299
  • 1
  • 10
  • 15