• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Jira Selenium Protractor Testing SDLC Agile Methodology

Java Selenium Tutorial & Testing Interview Questions

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Test Cases
  • Selenium
  • Manual Testing
  • Difference
  • Search
SoftwareTestingo » Selenium » Selenium Tutorial » What Are TestNG Annotations in Selenium & Sequence Order

What Are TestNG Annotations in Selenium & Sequence Order

Last Updated on: June 6, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • What is an Annotation?
  • TestNG Annotations
  • TestNG Annotation With Description
  • TestNG Annotations Order
  • TestNG Annotation With Example

TestNG Annotation: In our previous articles, we have discussed various basic things about TestNG framework as we have mentioned earlier that TestNg support various annotation to handle multiple operations. TestNG makes use of these annotations to provide several features to the user so that they can able to build a robust testing framework. So in this post, we are going to discuss different annotations that are present in TestNG in detail.

What is an Annotation?

Annotations are nothing but a piece of instruction for the compiler that you apply to classes, methods, or variables in your Java code. It is a predominant feature of the TestNG framework.

In TestNG, there are multiple annotations available which can be used for different tasks. So before starting, you should be aware of those annotations and their use so that you can use those in your Selenium webDriver project.

TestNG Annotations

As per the TestNG official website, in TestNG we have 15 different annotations are present, those are:

  • @BeforeSuite
  • @AfterSuite
  • @BeforeTest
  • @AfterTest
  • @BeforeGroups
  • @AfterGroups
  • @BeforeClass
  • @AfterClass
  • @BeforeMethod
  • @AfterMethod
  • @DataProvider
  • @Factory
  • @Listeners
  • @Parameters
  • @Test

Let us try to understand all the above annotations one by one.

TestNG Annotation With Description

@Test: You can use this annotation in class or method level for the test method. If you declare this annotation at class level, then all the methods present inside the class will be considered as a test method. This is one of the important annotations in TestNG because inside that we need to write the business logic.

@BeforeMethod: If you declare a test case or method with this annotation, then the annotated method will be executed before the execution of each @test annotated method.

@AfterMethod: Those methods are annotated by the @AfterMethod method executed after the execution of each @test annotated method.

@BeforeClass: A class can have multiple classes, and each class can have various methods in it. So @BeforeClass annotated methods are executed before the execution of any of the methods of that class.

@AfterClass: These annotated methods are executed after the execution of all test methods of a test class are executed.

Note: @BeforeClass and @AfterClass methods are unique to a particular class.

@BeforeTest: A Suite can have several tests. The @BeforeTest annotated methods will be executed before <test> tag is run. We are using this type of annotation to set up any specific configuration for the tests.

@AfterTest: The annotated methods will execute after each <test> tag, which is declared inside a TestNG suite.

Note: @BeforeTest and @AfterTest are unique to test levels. If a suite contains multiple tests, these annotated methods will not be applicable for all tests.

@BeforeSuite: The annotated method will be executed before any tests declared inside a TestNG suite. We can use this tag for print what tests are going to run, browser name and project name, which is common for all the tests.

@AfterSuite: The annotated method will be executed after any tests declared inside a TestNG suite.

@BeforeGroups: BeforeGroups annotated method will run before any of the test methods of the specified group is executed.

@AfterGroups: AfterGroups annotated method will run after any of the test methods of the specified group gets executed.

@Parameters: This annotation is used to pass parameters to a test method. These parameter values are provided using the testng.xml configuration file at runtime.

@DataProvider: If any of your methods required some input data, then we can pass the data to that method. So we can use @DataProvider annotation to make that method as a data supplier. The annotated method must return an object[][]. The method which wants input data from data provider that needs to use the data provider name, which you mentioned at the time of creating the data provider.

@Factory: Marks an annotated method as a factory that returns an array of class objects (Object[ ]). These class objects will then be used as test classes by TestNG. This is used to run a set of test cases with different values.

@Listeners: This is applied to a test class. It defines an array of test listeners classes extending org.testng.ITestNGListener. It helps in tracking the execution status and logging purpose.

TestNG Annotations Order

The Sequence order of execution of all the above annotation is like below

  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeMethod
  • Test
  • AfterMethod
  • AfterClass
  • AfterTest
  • AfterSuite

TestNG Annotation With Example

package Testing;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class SampleTest 
{
   @BeforeSuite
   public void createSelenium()
   {
      System.out.println("Creating selenium...");
   }
   @AfterSuite
   public void destroySel() 
   {
      System.out.println("Destroy the sel...");
   }
   @BeforeTest
   public void openConnection() 
   {
      System.out.println("establishing the connection with db..");
   }
   @AfterTest
   public void closeConnection() 
   {
      System.out.println("shutdown the connection with db..");
   }
   @BeforeMethod
   public void openBrowser()
   {
      System.out.println("Opening the browser..");
   }
   @AfterMethod
   public void closeBrowser()
   {
      System.out.println("Closing the browser..");
   }
   @Test
   public void testApp() 
   {
      System.out.println("in testApp...");
   }
   @Test
   public void testLogin() 
   {
      System.out.println("in testLogin...");
   }
}

The annotations, as mentioned earlier of TestNG, are to provide a better structure and readability to code. Also, it has some more rich functionality by using those we can save lots of time. After each execution of the script, it provides us a detailed report so that we can share those reports at the time of status reporting.

But using the above annotations is depends on your business requirement, hence Choose the right ones as per your requirement is important. Therefore choosing the right ones for proper use is important.

TestNG Annotation Execution Flow Interview Question: TestNG Annotation Execution Flow Selenium Example Program?

package com.selenium.TestNG;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class FlowTestClass1 
{
   @BeforeTest
   public void beforetest2()
   {
      System.out.println("Class 1 Before Test");
   }
   @BeforeClass
   public void beforeclass1()
   {
      System.out.println("Class 1 Before Class");
   }
   @BeforeMethod
   public void beforemethod1()
   {
      System.out.println("Class 1 Before Method");
   }
   @Test
   public void class1test1()
   {
      System.out.println("Inside Class 1 Test1");
   }
   @Test
   public void class1test2()
   {
      System.out.println("Inside Class 1 Test2");
   }@AfterClass
   public void Afterclass1()
   {
      System.out.println("Class 1 After Class");
   }
}

 

package com.selenium.TestNG;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
@Test
public class FlowTestClass2 
{
   @BeforeTest
   public void beforeTest0()
   {
      System.out.println("Class 2 Before Test");
   }
   @BeforeClass
   public void beforeClass2()
   {
      System.out.println("Class 2 Before Class");
   }
   @Test
   public void class2test1()
   {
      System.out.println("Class 2 Test1");
   }
   @AfterClass
   public void AfterClass()
   {
      System.out.println("Class 2 After Class");
   }
   @AfterTest
   public void aftertest()
   {
      System.out.println("Inside Class2 After Test");
   }
}

Read Also: TestNG Group Annotation

Source: Link

    Filed Under: Selenium Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Tutorials Important Links

    • Software Testing Jobs
    • Manual Testing Tutorial
    • Selenium Tutorial
    • Core Java Tutorial
    • TestNG Tutorial
    • Java Programs
    • Selenium Programs
    • Manual Test Cases
    • Interview Tips
    • Contact US
    • www.softwaretestingo.com

    Important Links

    • Software Testing Interview Questions
    • Agile Interview Questions
    • Manual Testing Interview Questions
    • Testing Interview Questions
    • Selenium Interview Questions
    • Selenium Real Time Interview Questions
    • Selenium WebDriver Interview Questions
    • Automation Framework Interview Questions
    • Appium Interview Questions
    • TestNG Interview Questions
    • Java Interview Questions
    • Core Java Interview Questions

    Categories

    Copyright © 2021 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy