An annotation is a tag or meta-data that provides additional information about the class, interface, or method in TestNG.
Annotations are used to keep the structure of the test classes; these annotations invoke the methods according to the invocation time.
We have several annotations present in testNG, but we might use very few annotations as the remaining are intended to develop the TestNG Framework.
Let's discuss important TestNG annotations used along with selenium webdriver, please be aware this list doesn't follow the execution sequence:
@Test annotation makes a method to run as the base for the TestNG annotations, a method annotated with @test will hold the test logic of the business and assertions.
Every @Test annotated method should correspond to a manual test case; sometimes, some teams may map every @test with a requirement.
You might think this is the start point of the TestNG execution, well you are almost there, but the starting point of TestNG is the main method present in the TestNG.class file
The main method present in the TestNG class redirects the control to the @Test method based on the test class and the @Test annotated method gets the second call.
The below code runs the @Test first, and it opens the chercher tech website.
package test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class TestJenkins {
@Test
public void openCherCherTech() {
System.setProperty("webdriver.chrome.driver", "D:PATHchromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.println("open cherCher Tech");
driver.get("https://chercher.tech");
}
}
@Before annotated method gets executed just before executing the @Test annotated method.
TestNG checks if there is any @Before Method, which is related to @Test method if preset, then TestNG executes it.
@BeforeMethod will be executed for every @Test method present in the class, @BeforeMethod declared in one class will not affect @Test present in the Other classes
In the below code, the browser is opened by @Before Test Method; @BeforeMethod will be used when the user wants to perform some operation before every @test Method.
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestJenkins {
WebDriver driver;
@Test
public void openCherCherTech() {
System.out.println("open cherCher Tech");
driver.get("https://chercher.tech");
}
@BeforeMethod
public void openBrowser() {
System.setProperty("webdriver.chrome.driver", "D:PATHchromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
}
If there is no @Test, then @BeforeMethod will be ignored, and you may face : [TestNG] No tests found. Nothing was run
public class TestJenkins {
WebDriver driver;
@BeforeMethod
public void openBrowser() {
System.setProperty("webdriver.chrome.driver", "D:PATHchromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
}
@AfterMethod will be executed once the @Test block completes it's execution.
@AfterMethod will run for every @Test method in the TestNG test class. If there is no @Test method, then @AfterMethod will be ignored by TestNG.
@AfterMethod will have cleaning code for the method like deletion of details and logging out, or refreshing pages
public class TestJenkins {
WebDriver driver;
@Test
public void openCherCherTech() {
System.out.println("@Test method execution");
driver.get("https://chercher.tech");
}
@BeforeMethod
public void openBrowser() {
System.out.println("Opening browser in @BeforeMethod ");
System.setProperty("webdriver.chrome.driver", "D:PATHchromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@AfterMethod
public void closeBrowser() {
System.out.println("Closing Browser in @AfterTest");
driver.quit();
}
}
@BeforeClass method id executed before executing anything in the test class.
@BeforeClass will be executed only once per test class if there is no @Test present in the TestClass, then @BeforeClass will be ignored.
public class TestJenkins {
WebDriver driver;
@BeforeClass
public void setDriverPath() {
System.out.println("setting driver path in @BeforeClass");
System.setProperty("webdriver.chrome.driver", "D:PATHchromedriver.exe");
}
@BeforeMethod
public void openBrowser() {
System.out.println("Opening browser in @BeforeMethod ");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void openCherCherTech() {
System.out.println("@Test method execution");
driver.get("https://chercher.tech");
}
@AfterMethod
public void closeBrowser() {
System.out.println("Closing Browser in @AfterMethod");
driver.quit();
}
}
@AfterClass annotated method will get executed after the completion of the execution of the test class
@AfterClass will be executed only once per class, and if there is no @Test, then this method will not be executed. As the program is becoming bigger, please adjust with code, which doesn't have selenium any more.
public class TestJenkins {
WebDriver driver;
@BeforeClass
public void setDriverPath() {
System.out.println("@BeforeClass");
}
@BeforeMethod
public void openBrowser() {
System.out.println("@BeforeMethod ");
}
@Test
public void openCherCherTech() {
System.out.println("@Test");
}
@AfterMethod
public void closeBrowser() {
System.out.println("@AfterMethod");
}
@AfterClass
public void infomExecutionOver() {
System.out.println("@AfterClass");
}
}
Method annotated with @BeforeGroups gets executed only once for a group before any of the test of that group executes.
Unlike other annotations, we can have different @BeforeGroups annotated methods in test class but only difference, we will provide the group name to @BeforeMethod
The only @BeforeMethod is dependent on the test, so @BeforeGroups got executed before executing @BeforeGroups (so many before )
public class TestJenkins {
@BeforeGroups(value="real")
public void beforeGroupsExampleReal() {
System.out.println("@BeforeGroups for real");
}
@BeforeGroups(value="dummy")
public void beforeGroupsExampleDummy() {
System.out.println("@BeforeGroups for Dummy");
}
/*
after method and after class blocks
*/
@Test(groups = "dummy")
public void openCherCherTechDummy() {
System.out.println("@Test dummy");
}
@Test(groups = "real")
public void openCherCherTechReal() {
System.out.println("@Test Real");
}
/*
after method and after class blocks
*/
}
Method annotated with @AfterGroups gets executed only once for a group only after all the tests of that group finished execution.
Unlike other annotations, we can have multiple @AfterGroups annotated methods in test class but the only difference is, we will provide the group name to @AfterGroups
public class TestJenkins {
@BeforeGroups(value="real")
public void beforeGroupsExampleReal() {
System.out.println("@BeforeGroups for real");
}
@BeforeGroups(value="dummy")
public void beforeGroupsExampleDummy() {
System.out.println("@BeforeGroups for Dummy");
}
/*
before class and after method blocks
*/
@Test(groups = "dummy")
public void openCherCherTechDummy() {
System.out.println("@Test dummy");
}
@Test(groups = "real")
public void openCherCherTechReal() {
System.out.println("@Test Real");
}
/*
after method and after class blocks
*/
@AfterGroups(value="real")
public void afterGroupsExampleReal() {
System.out.println("@AfterGroups for real");
}
@AfterGroups(value="dummy")
public void afterGroupsExampleDummy() {
System.out.println("@AfterGroups for Dummy");
}
}
@BeforeTest annotated method will get executed before executing any element inside the <test> tag in testng.xml
@beforeTest solely depends on <test> tags present in testng.xml.
public class TestJenkins {
@BeforeTest
public void beforeTestExample() {
System.out.println("@BeforeTest");
}
/*
before class and after method blocks
*/
@Test
public void openCherCherTech() {
System.out.println("@Test");
}
@AfterMethod
public void closeBrowser() {
System.out.println("@AfterMethod");
}
@AfterClass
public void infomExecutionOver() {
System.out.println("@AfterClass");
}
}
Even there is no @Test method present in the class, the @BeforeTest method will be executed; also, you might see a message that there are not @Test methods.
public class TestJenkins {
@BeforeTest
public void beforeTestExample() {
System.out.println("@BeforeTest");
}
}
You would get the same above result as above even if you don't have testng.xml, @BeforeMethod will serve its purpose whether testng.xml is present or not.
@AfterTest is executed just once the all the element in the <test> tag is completed
All the properties of @BeforeTest are applicable for @AfterTest as well
public class TestJenkins {
@BeforeTest
public void beforeTestExample() {
System.out.println("@BeforeTest");
}
/*
before class and after method blocks
*/
@Test
public void openCherCherTech() {
System.out.println("@Test");
}
/*
after method and after class blocks
*/
@AfterTest
public void completeTestExecution() {
System.out.println("@AfterTest");
}
}
@BeforeSuite will executed before executing <suite> tag in testng.xml, Similarly @AfterSuite will be executed after executing the <suite> tag in testng.xml
public class TestJenkins {
@BeforeSuite
public void suiteStart() {
System.out.println("@BeforeSuite");
}
@BeforeTest
public void beforeTestExample() {
System.out.println("@BeforeTest");
}
/*
before class and after method blocks
*/
@Test
public void openCherCherTech() {
System.out.println("@Test");
}
/*
after method and after class blocks
*/
@AfterTest
public void completeTestExecution() {
System.out.println("@AfterTest");
}
@AfterSuite
public void suiteCompletion() {
System.out.println("@AfterSuite");
}
}
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.