LambdaTest Community

Find answers, support, and inspiration from other users

LambdaTest Community

How do I execute a sequence of annotations in TestNG for Selenium?

I wanted to know how do I execute a sequence of annotations in TestNG for Selenium, Can anyone please provide me some valueable information.

Up Vote Down Vote 0 Votes
Posted 4 months ago


Execution Sequence Of Annotations In TestNG For Selenium

All annotations described above are executed on runtime in the following order:

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

Here is an image of the basic workflow of these annotations:

Attributes Used With Annotations In TestNG

These test annotations in TestNG have multiple attributes that can be used for our test method. The attributes further help in defining our tests and help in providing clarity in terms of execution flow of the different test’s method used in the TestNG class. Listing them below:

  • Description: It defines the test method. One can define what a method does via the description. For example, @Test(description=”this test validates the login functionality”).
  • alwaysRun: this attribute when used with a test method ensures it always run irrespective of the fact even if the parameters on which the method depends fails. When the value is set to true this method will always execute. For example, @Test(alwaysRun= true).
  • dataProvider: This attribute is set to provide data from the dataProvider annotated test to the test provided with this attribute. For example, let’s say you intent to run your tests on multiple cross browsers, where a dataProvider annotated test is written which contains multiple inputs of browsers and their corresponding versions. In this case the test containing this attribute will use those inputs of data to run you tests on multiple browsers. Syntax for the same is, @Test(dataProvider=”cross browser testing”).
  • dependsOnMethods: This attribute provides details to the execution flow, wherein the test is executed only if its dependent method mentioned in the attribute is executed. In case the test on which the method depends on is failed or not executed, the test is skipped from the execution. For example, @Test(dependsOnmethod=”Login”).
  • groups: This attribute helps to groups your test methods focusing onto a single functionality into one group. For example, @Test(groups=”Payment_Module”). This attribute also helps in the longer run when one can choose to ignore few groups during the execution cycle and chose over the other groups. All one need to do is mention the included groups in the TestNG.xml file within the include tag whereas the excluded groups can be defined using the exclude tag in the xml file.
  • dependsOnGroups: this attribute performs the above two attributes functions in collation i.e. it defines the test method with the attribute ‘dependsOn’ the defined groups. Once that group of tests are run, only post that this annotated method would execute. For example, @Test(dependsOnMethods = "Payment_Module" ).
  • priority: This attribute helps us to defined priority of the test’s methods. When TestNG executes the @Test annotated method, it may do so in random order. In a scenario where you wish that your @Test annotated method runs in a desired sequence you can use the priority attribute. The default priority of all test methods is 0. Priorities in ascending order are scheduled first for execution for example, @Test(priority=1), @Test(priority=2), in this case test with priority equal to one will be executed first then the test with priority as 2.
  • enabled: This attribute comes into picture, when you have an intent to ignore a particular test method and don’t want to execute it. All you need to do is set this attribute to false. For example, @Test(enabled= false).
  • timeout: This attribute helps to define the time a particular test should take to execute, in case it exceeds the time defined by the attribute, the test method would terminate and will fail with an exception marked as org.testng.internal.thread.ThreadTimeoutException. For example, @Test(timeOut= 500). Please note the time specified is in milliseconds.
  • InvocationCount: This attribute works exactly like the loop. Based on the attribute set across the test method, it would execute that method those number of times. For example, @Test(invocationCount = 5), this would execute the test 5 times.
  • InvocationTimeOut: this attribute is used in unison with the above invocationCount attribute. Based on the set value of this attribute along with the invocationCount, this ensures the test runs the number of times specified as per the invocationCount in the defined time set by the invocationTimeOut attribute. For example, @Test(invocationCount =5,invocationTimeOut = 20 ).
  • expectedExceptions: this attribute helps to handle the exception the test method is expected to throw. In case the one defined in the attribute is set and thrown by the test method it is passed else any other exception not stated in the attribute and thrown by the test method, would make the test method fail. For example, @Test(expectedExceptions = {ArithmeticException.class }).

The above defined are the attributes used with the annotations in TestNG for Selenium. Below is the code snippet showcasing the use of the above attributes:

import static org.testng.Assert.assertEquals;
import Welcome to nginx!.File;
import Welcome to nginx!.IOException;
import java.util.concurrent.TimeUnit;
import http://org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
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 AnnotationsTest {
	public WebDriver driver;
	public String url="Free Cross Browser Testing Tool on Cloud | Selenium Automation Testing";
	public void setUp()
		System.setProperty("", "C:\\Users\\navyug\\workspace\\QAPractise\\src\\ChromeDriver\\chromedriver.exe");
		driver=new ChromeDriver();
		System.out.println("The setup process is completed");
	public void profileSetup()
		System.out.println("The profile setup process is completed");
	public void appSetup()
		System.out.println("The app setup process is completed");
	public void checkLogin()
		 driver.get("Free Cross Browser Testing Tool on Cloud - LambdaTest");
		 System.out.println("The login process on lamdatest is completed");
	@Test(priority=0 ,description= "this test validates the sign-up test")
	public void signUp() throws InterruptedException
		WebElement link= driver.findElement(By.xpath("//a[text()='Free Sign Up']"));;
		WebElement organization=driver.findElement(By.xpath("//input[@name='organization_name']"));
		WebElement firstName=driver.findElement(By.xpath("//input[@name='name']"));
		WebElement email=driver.findElement(By.xpath("//input[@name='email']"));
		WebElement password=driver.findElement(By.xpath("//input[@name='password']"));
		WebElement phoneNumber=driver.findElement(By.xpath("//input[@name='phone']"));
		WebElement termsOfService=driver.findElement(By.xpath("//input[@name='terms_of_service']"));;
		WebElement button=driver.findElement(By.xpath("//button[text()='Signup']"));;
	@Test(priority=3, alwaysRun= true, dependsOnMethods="check_login", description="this test validates the URL post logging in" , groups="url_validation")
	public void testCurrentUrl() throws InterruptedException
		String currentUrl= driver.getCurrentUrl();
		assertEquals(current_url, "", "url did not matched");
		System.out.println("The url validation test is completed");
	@Test(priority=1, description = "this test validates the logout functionality" ,timeOut= 25000)
	public void logout() throws InterruptedException
	public void skipMethod()
		System.out.println("this method will be skipped from the test run using the attribute enabled=false");
	@Test(priority=6,invocationCount =5,invocationTimeOut = 20)
	public void invocationcountShowCaseMethod()
		System.out.println("this method will be executed by 5 times");
	public void screenshot() throws IOException
		TakesScreenshot scr= ((TakesScreenshot)driver);
 File file1= scr.getScreenshotAs(OutputType.FILE);
  FileUtils.copyFile(file1, new File("C:\\Users\\navyug\\workspace\\QAPractise\\test-output\\test1.PNG"));
  System.out.println("Screenshot of the test is taken");
	public void closeUp()
		System.out.println("The close_up process is completed");
	public void reportReady()
		System.out.println("Report is ready to be shared, with screenshots of tests");
	public void cleanUp()
		System.out.println("All close up activities completed");
 public void setUpSecurity() {
 System.out.println("url validation test starting");
 public void tearDownSecurity() {
 System.out.println("url validation test finished");

Console Output:

Up Vote Down Vote 0 Votes
Posted 3 months ago
1 Answer
4 months ago