Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 14, 2016

Before And After Annotations





Before and After Annotations are very useful while writing the selenium programs. These will control the execution flow of your program. What should execute what time and which statement or block of code should execute first and which should be last.

One of the important thing is the order of writing the annotations is not predefined. It is your wish where you want to write those in the program. TestNG will take care of the execution flow according to the annotations which we used in the program.

The below is the sample program for the execution flow of the different types of annotations and how they will execute:

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class BeforeAndAfterClass
{
    /**
     * This is the first method will be executed in this program.
     * As Before Suite will be executed first
    */
    @BeforeSuite
    public void beforeSuite()
    {
        System.out.println("In Before Suite Method");
    }
    
    /**
     * This is the last method will be executed in this program.
     * As After Suite will be executed last
     * And the order is not important that this is the second method in the Program.
     * Though it will execute at last
    */
    @AfterSuite
    public void afterSuite()
    {
        System.out.println("In After Suite Method");
    }
    
    /**
     * Before Test method will be executed before the first
     * test-method mentioned in each test inside the 'test'
     * tag in testng.xml file.
    */
    @BeforeTest
    public void beforeTest()
    {
        System.out.println("In Before Test Method");
    }
    
    /**
     * After Test method will be executed after the last
     * test-method mentioned in each test inside the 'test'
     * tag in testng.xml file.
    */
    @AfterTest
    public void afterTest()
    {
        System.out.println("In After Test Method");
    }
    
    /**
     * Before Class method will be executed before
     * any of the test-methods inside a class.
    */
    @BeforeClass
    public void beforeClass()
    {
        System.out.println("In Before Class Method");
    }
    
    /**
     * After Class method will be executed After
     * any of the test-methods inside a class.
    */
    @AfterClass
    public void afterClass()
    {
        System.out.println("In AfterClass Method");
    }
    
    /**
     * Before group method will be executed before executing any of
     * the tests belonging to the group as mentioned in the 'groups'
     * attribute.
     * The following method gets executed before execution of the
     * test-method belonging to group "firstGroup".
    */
    @BeforeGroups(groups={"firstGroup"})
    public void beforeFirstGroup()
    {
        System.out.println("In Before First Group Method");
    }
    
    /**
     * After group method will be executed after executing any of
     * the tests belonging to the group as mentioned in the 'groups'
     * attribute.
     * The following method gets executed after execution of the
     * test-method belonging to group "firstGroup".
    */
    @AfterGroups(groups={"firstGroup"})
    public void afterFirstGroup()
    {
        System.out.println("In After First Group Method");
    }
    
    /**
     * Before group method will be executed before executing any of
     * the tests belonging to the group as mentioned in the 'groups'
     * attribute.
     * The following method gets executed before execution of the
     * test-method belonging to group "secondGroup".
    */
    @BeforeGroups(groups={"secondGroup"})
    public void beforeSecondGroup()
    {
        System.out.println("In Before Second Group Method");
    }
    
    /**
     * After group method will be executed after executing any of
     * the tests belonging to the group as mentioned in the 'groups'
     * attribute.
     * The following method gets executed after execution of the
     * test-method belonging to group "secondGroup".
    */
    @AfterGroups(groups={"secondGroup"})
    public void afterSecondGroup()
    {
        System.out.println("In After Second Group Method");
    }
    
    /**
    * Before method will be executed before each test-method.
    */
    @BeforeMethod
    public void beforeMethod()
    {
        System.out.println("In BeforeMethod Method");
    }
    
    /**
    * After method will be executed after each test-method.
    */
    @AfterMethod
    public void afterMethod()
    {
        System.out.println("In AfterMethod Method");
    }
    
    /**
    * Test-method which belongs to group "firstGroup".
    */
    @Test(groups={"firstGroup"})
    public void firstGroupMethodOne()
    {
        System.out.println("In Test method one of FirstGroup");
    }
    
    /**
    * Test-method which belongs to group "firstGroup".
    */
    @Test(groups={"firstGroup"})
    public void firstGroupMethodTwo()
    {
        System.out.println("In Test method two of FirstGroup");
    }
    
    /**
    * Test-method which belongs to group "secondGroup".
    */
    @Test(groups={"secondGroup"})
    public void secondGroupMethodOne()
    {
        System.out.println("In Test method one of SecondGroup");
    }
    
    /**
    * Test-method which belongs to group "secondGroup".
    */
    @Test(groups={"secondGroup"})
    public void secondGroupMethodTwo()
    {
        System.out.println("In Test method two of SecondGroup");
    }
}

Now execute the above script using the testng.xml configuration file. Below is the testng.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="TestOne">
    <classes>
        <class name="BeforeAndAfterClass">
            <methods>
            <include name="firstGroupMethodOne"/>
            <include name="firstGroupMethodTwo"/>
            </methods>
        </class>           
    </classes>
  </test>
  <test name="TesTwo">
    <classes>
        <class name="BeforeAndAfterClass">
            <methods>
            <include name="secondGroupMethodOne"/>
            <include name="secondGroupMethodTwo"/>
            </methods>
        </class>           
    </classes>
  </test>
</suite>

Output for the above program is :

In Before Suite Method
In Before Test Method
In Before Class Method
In Before First Group Method
In BeforeMethod Method
In Test method one of FirstGroup
In AfterMethod Method
In BeforeMethod Method
In Test method two of FirstGroup
In AfterMethod Method
In After First Group Method
In AfterClass Method
In After Test Method
In Before Test Method
In Before Class Method
In Before Second Group Method
In BeforeMethod Method
In Test method one of SecondGroup
In AfterMethod Method
In BeforeMethod Method
In Test method two of SecondGroup
In AfterMethod Method
In After Second Group Method
In AfterClass Method
In After Test Method
In After Suite Method

Now, we will analyze the output in detail:

1.beforeSuite() and afterSuite() executed only once per the suite.
2.beforeTest() and afterTest() methods executed 2 times as the testng.xml suite contains 2 tests as tags.
3.beforeClass() and afterClass() methods executed only once as we have only one test class to execute.
4.beforeFirstGroup() and afterFirstGroup() methods executed 2 times as this group is used by two test methods(i.e. firstGroupMethodOne() and firstGroupMethodTwo()).
5.beforeSecondGroup() and afterSecondGroup() methods executed 2 times as this group is used by two test methods (i.e. secondGroupMethodOne() and secondGroupMethodTwo()).
6.beforeMethod() and afterMethod() methods executed 4 times as we have 4 test methods in the program (i.e.firstGroupMethodOne(), firstGroupMethodTwo(),secondGroupMethodOne() and secondGroupMethodTwo()).
7.firstGroupMethodOne(), firstGroupMethodTwo(),secondGroupMethodOne() and secondGroupMethodTwo() methods exeucted only one time as these are the actual test methods to execute in the program.



Share this post: on Twitter on Facebook

TestNG Annotations Enabling and Disabling TestNG Test

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025