Annotation

What is annotations in TestNG ?
An annotation is a tag or meta-data that provides additional information about 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 number of annotations present in testNG, but we might use very few annotations as remaining are intended to develop the TestNG Framework.

Let's discuss about important TestNG annotations used along with selenium webdriver, please be aware this list doesn't follow execution sequence:

    test class based annotations

  1. @Test : Marks a class or a method as a part of the test. Or in simple words, makes a method as the test method.
  2. @BeforeMethod : The BeforeMethod runs before each test method.
  3. @AfterMethod : The AfterMethod Annotation runs after each test method.
  4. @BeforeClass : BeforeClass Method runs only once before the first test method. The Current Class method will be one from which it is invoked.
  5. @AfterClass : AfterClass annotation method runs only once when all the test methods mentioned in the current class are executed.
  6. @BeforeGroups : BeforeGroups method is run before the first test method, and the condition is that the group should contain the method which is invoked.
  7. @AfterGroups : AfterGroups method is run after the first test method, and the condition is that the group should contain the method which is invoked.
  8. @Parameters : Parameters annotation is used to pass the parameter’s value in the test.
  9. @DataProvider : DataProvider annotation method returns an Object[ ][ ] type value, where each Object[ ] can be assigned as the parameter of the test method that wants to receive the data from this DataProvider annotation method.
  10. @Listeners : Defines listeners on a test class.
  11. testng.xml based annotations

  12. @BeforeTest : BeforeTest method runs before any test method inside the tag is run. (in testNG.xml file). It runs before the execution of classes takes place.
  13. @AfterTest : AfterTest method runs after any test method inside the tag is run. (in testNG.xml file). It runs after the execution of classes takes place.
  14. @BeforeSuite : BeforeSuite annotation method runs only once before all tests from the suite.
  15. @AfterSuite : AfterSuite annotation method runs only once after all tests from the suite.

@Test annotation

@Test annotation makes a method to run as the base for the TestNG annotations, method annotated with @test will hold the test logic of the business and assertions.

Every @Test annotated methods 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 a 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 second call.

Below code runs the @Test first and it open 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:\\PATH\\chromedriver.exe");
								
								WebDriver driver = new ChromeDriver();
								driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
								
								System.out.println("open cherCher Tech");
								driver.get("https://chercher.tech");
							}
						}
						


If there is no @Test then no other annotation will get executed

@BeforeMethod

@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 below code, the browser is opened by @Before Test Method, @BeforeMethod will be used when 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:\\PATH\\chromedriver.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:\\PATH\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}
}			

@AfterMethod

@AfterMethod will be executed once the @Test block completes it's execution.

@AfterMethod will run for every @Test method in 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:\\PATH\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}
	@AfterMethod
	public void closeBrowser() {
		System.out.println("Closing Browser in @AfterTest");
		driver.quit();
	}
}
			


aftertest-before-test-test-in-testng

@BeforeClass

@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:\\PATH\\chromedriver.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();
	}
}			


beforeclass-testng-selenium

@AfterClass

@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");
	}
}			


afterclass-testng-selenium

@BeforeGroups and @AfterGroups

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 the different @BeforeGroups annotated methods in test class but only difference, we will provide the group name to @BeforeMethod

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
	*/
}
			


beforegroups-testng

@BeforeGroups and @AfterGroups

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 the multiple @AfterGroups annotated methods in test class but 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");
	}
}			


aftergroups-testng

@BeforeTest

@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. testng-xml-testng-test-annotation-selenium


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");
	}
}
			


beforetest-testng-selenium Even there is no @Test method present in the class, the @BeforeTest method will be executed, also you might see message that there is not @test methods.


public class TestJenkins {
	@BeforeTest
	public void beforeTestExample() {
		System.out.println("@BeforeTest");
	}
}	


beforetest-testng-no-testmethod You would get same above result as above even if you don't have testng.xml, @BeforeMethod will serve it's purpose whether testng.xml is present or not.

@AfterTest

@AfterTest is executed just once the all the element in the <test> tag is completed

All the properties of @BeforeTest is 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");
	}	
}
			

suite-testng-xml

@BeforeSuite and @AfterSuite

@BeforeSuite will executed before executing <suite> tag in testng.xml, Similarly @AfterSuite will be executed after executing the <suite> tag in testng.xml
suite-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");
	}
}			


beforesuite-aftersuite-testng

Headless browser in Selenium webdriver

Execution Sequence in TestNG

  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeGroups
  • BeforeMethod
  • Test
  • AfterMethod
  • AfterGroups
  • AfterClass
  • AfterTest
  • AfterSuite
 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech