Thursday 2 August 2012

TestNg Groups feature in Selenium Automation

In this blog, I would like to share with all of you on the usage of 'groups' feature of testNG in selenium automation.
TestNG allows you to perform sophisticated groupings of test methods. Not only can you declare that methods belong to groups, but you can also specify groups that contain other groups. Then TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while excluding another set.  This gives you maximum flexibility in how you partition your tests and doesn't require you to recompile anything if you want to run two different sets of tests back to back.

Declaring a Group in your test:

public class Test {
  @Test
  public void testMethod1() {
  }
  @Test
  public void testMethod2() {
  }
  @Test
  public void testMethod3() {
  }
}
 Here you can see there are three methods declared inside the class namely testMethod1, testMethod2, testMethod3. Consider them as three scenarios (@Test in TestNg).
Now one may be only needed to run while a sanity testing is performed, the other only for a regression testing and another for both sanity and regression. In such a situvation Groups concept of testNg is very usefull.
We can define classify these tests on the basis of thier type of testing with the help of groups as follows:

public class Test {
  @Test(groups = { "sanity" })
  public void testMethod1() {
  }
  @Test(groups = {"regression"})
  public void testMethod2() {
  }
  @Test(groups = { "sanity", "regression" })
  public void testMethod3() {
  }
}
Notice the use of 'groups' inside the @Test annotation in the above example. So we have classified the three scenarios into diffrent groups.

Now to execute this scenarios on the basis of their specific groups we have two options availabe.

1) FROM THE SUITE XML [TESTNG XML]
2) FROM THE ANT XML [BUILD XML]

I shall explain in detail on these two options in my upcoming blogs.....
Please follow up with your comments in case of any suggestions or opinions.