Order of TestNG Annotations!!!!!

7:58 PM 0 Comments













The Order of the below Testng Annotations is important. Lets look into them below:
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeGroups
@BeforeMethod
@AfterMethod
@AfterGroups
@AfterClass
@AfterTest
@AfterSuite

Example for @BeforeMethod & @AfterMethod:


Scenario1:

Suppose for every test-case to execute the pre-condition as login and post-condition as log-out then we have to use as follows:

LoginApp.java

public class LoginApp 
{
        @BeforeMethod
         public void Login()
        {
              System.out.println("In Login Method");
         }
          @AfterMethod
          public void Logout()
         {
               System.out.println("In Logout Method"); 
         }
        @Test
         public void testcase1()
        {
               System.out.printl("testcase1");
         }
         @Test
         public void testcase2()
        {
               System.out.printl("testcase2");
         }
}

Order of Execution:

Login()
testcase1()
Logout()
Login()
testcase2()
Logout()


Example for @BeforeClass & @AfterClass:

Scenario2:

Suppose for all the test-cases the pre-condition say creating a driver instance should be invoked only and the post-condition say quitting the browser once after the execution of all the @Test methods is done.

LoginApp.java

public class LoginApp 
{
         WebDriver driver;
        @BeforeClass
         public void invokeBrowser()
        {
             driver = new FirefoxDriver();
             driver.get("http://www.Facebook.com");
         }
          @AfterClass
          public void closeBrowser()
         {
              driver.quit();
         }
        @Test
         public void testcase1()
        {
               System.out.printl("testcase1");
         }
         @Test
         public void testcase2()
        {
               System.out.printl("testcase2");
         }
}

Order of Execution:

invokeBrowser()
testcase1()
testcase2()
closeBrowser()

Example for @BeforeTest & @AfterTest:

Scenario 3:
Suppose for example if we want to execute the same program on multiple browsers say IE and FF. In those cases we should have configuration file should be written as follows:


Unknown

This site creates a platform to become an expert by gaining knowledge of unknown real-time issues as well.

0 comments: