Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » How To Handle Sequence Error with @ Test Annotation in TestNG

While using TestNG framework, we provide @Test annotation for original test method.
Example:
public class Test1 {
  @Test
  public void testMethod1() {
  }
}


So the code says that testMethod1() is a function and since it is annotated with @Test so it will execute.

Problem starts when we have multiple functions to be covered as a part of the test.
@Test
  public void testMethodForRefresh()
  {
         System.out.println("Refresh the page");
  }
 @Test
  public void testMethofForClick()
  {
         System.out.println("Click on a link");
  }

our objective is  to Refresh a page and click on a link. Accordingly we have coded. But when you will execute the tetstcases, in the output, it is seen instead of Refresh method, Click method got executed first then Refresh method got executed.
Below are the ways how we can resolve this issue:

Process One:
TestNG follows the alphabetic order to pickup the methods when all the methods have same annotation as @Test. So name all your method accordingly..
Like - append Test00X in front of the methods. The first method what we need to call and execute will have name Test001_MethodForRefresh and next method will be Test002_MethofForClick


@Test
  public void Test001_MethodForRefresh()
  {
         System.out.println("Refresh the page");
  }
 @Test
  public void Test002_MethofForClick()
  {
         System.out.println("Click on a link");
  }


Process two:
A little more advanced upgrade is to set priority of the method. like: @Test(Priority=1). In this way, we don't have to change the method name.

@Test (Priority=1)
  public void testMethodForRefresh()
  {
         System.out.println("Refresh the page");
  }
 @Test(Priority=2)
  public void testMethofForClick()
  {
         System.out.println("Click on a link");
  }

Process three:
This is another approach weher we need to mention the sequence in testng.xml or pom.xml.
documentation for @Test is available here: http://testng.org/doc/documentation-main.html




«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply