TestNG annotations

Below is the list of annotations in TestNG.

  • @BeforeTest
  • @AfterTest
  • @BeforeMethod
  • @AfterMethod
  • @BeforeSuite
  • @AfterSuite
  • @BeforeGroups
  • @AfterGroups
  • @BeforeClass
  • @AfterClass

@BeforeTest: Executes before any method in a class which is having @Test annotation.
@AfterTest: Executes after any method in a class which is having @Test annotation.

@BeforeMethod: Executes every time before any test method in a class.
@AfterMethod: Executes every time after any test method in a class.

@BeforeSuite: Executes before all tests in a test suite.
@AfterSuite: Executes after all tests have run in a test suite.

@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.

@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.

@DataProvider: Supplies data to the test method.  The method using this annotation has to return Object[][] object. It has two attributes i.e., name and parallel.

name: It is the name of the data provider. If not set, by default it will take method name.

parallel: Default value is false. If it is set to true, tests generated using this annotation will run in parallel.

@Parameters: Used to pass parameters to the @Test method.

Source: http://testng.org/doc/documentation-main.html#annotations

 

TestNG example:

package com;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;

public class TestNG {

static WebDriver driver;

@BeforeTest
public void beforeTest() {

// chrome standalone server
System.setProperty(“webdriver.chrome.driver”,
“C:\\Users\\username\\Downloads\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();

}

@Test
public void selenium99_TestNG() {

// this opens the url
driver.get(“https://selenium99.com”);
// Implicit wait used for each step to wait for specified period of time.
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Prints the applications title
System.out.println(driver.getTitle());

}

@AfterTest
public void tearDown() {

// closes the browser.
driver.close();
}

}

Output on the console:

Home – Selenium Tutorials
PASSED: selenium99_TestNG