Transcripted Summary

In this chapter, we will discuss the default execution order for @Test methods and the priority attribute for the test methods.

# Default Execution Order for Test Methods

As a reminder, a test method is marked by the @Test annotation. In addition to setting our methods as tests, we can also set our entire class as a test.

In this example, I marked the class as part of the TestNG tests by adding a @Test annotation at the class level.


@Test
public class DefaultExecutionOrder_Class
{

}

Let's add setUp and tearDown methods with configuration annotations.


@Test
public class DefaultExecutionOrder_Class
{
	@BeforeClass
	public void setUp() {}

	@AfterClass
	public void tearDown() {}
}

We'll add three more methods with no annotations: signIn, searchTShirt and signOut.


@Test
public class DefaultExecutionOrder_Class
{
	@BeforeClass
	public void setUp() {}

  public void signIn() {}
	
	public void searchTShirt() {}
	
	public void signOut() {}
	
	@AfterClass
	public void tearDown() {}
}

Looking at this layout, it seems the execution order should be:

  1. setUp
  2. signIn
  3. searchTShirt
  4. signOut
  5. tearDown

However, that’s not the case. Execution is going to run searchTShirt before signIn and signOut. This is because the default execution order is ascending alphabetical order from A – Z. Therefore, in ascending order, se in searchTShirt comes before si in signIn and signOut.

We can place the test methods anywhere in the editor and it will run in the same order every time because the Test annotations identify the test methods.

Before running this program, let’s walk through the test application.


test application

The plan is to first sign in by entering an email of TestNG@Framework.com and password of TestNG1234 then click the Sign In button. After clicking the Sign In button, we click T-Shirts, search for a Blue T-shirt, then click the Search button. Finally, we sign out.

Let’s code this and run it.


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.testautomationu.utility.Highlighter;

@Test
public class DefaultExecutionOrder_Class
{
	WebDriver driver;
	
	@BeforeClass
	public void setUp () throws Exception
	{
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\Rex Allen Jones II\\Downloads\\Drivers\\chromedriver.exe");
		driver = new ChromeDriver ();
		driver.manage().window().maximize();
		
		driver.get("http://automationpractice.com/index.php");		
	}
	
  public void signIn ()
  {
      driver.findElement(By.xpath("//div[@class='header_user_info']/a")).click();

      WebElement emailAddress = driver.findElement(By.id("email"));
      Highlighter.highlightElement(driver, emailAddress);
      emailAddress.sendKeys("TestNG@Framework.com");

      WebElement password = driver.findElement(By.id("passwd"));
      Highlighter.highlightElement(driver, password);
      password.sendKeys("TestNG1234");

      WebElement buttonSignIn = driver.findElement(By.id("SubmitLogin"));
      Highlighter.highlightElement(driver, buttonSignIn);
      buttonSignIn.click();

      System.out.println("1. Sign In");
  }

  public void searchTShirt ()
  {
      WebElement menu = driver.findElement(By.xpath("//div[@id='block_top_menu']/ul/li/a[text()='T-shirts']"));
      Highlighter.highlightElement(driver, menu);
      menu.click();

      WebElement searchBox = driver.findElement(By.id("search_query_top"));
      Highlighter.highlightElement(driver, searchBox);
      searchBox.sendKeys("Blue");

      WebElement buttonSearch = driver.findElement(By.xpath("//*[@id='searchbox']/button"));
      Highlighter.highlightElement(driver, buttonSearch);
      buttonSearch.click();

      System.out.println("2. Search For T-Shirt");
  }

  public void signOut ()
  {
      WebElement linkSignOut = driver.findElement(By.className("logout"));
      Highlighter.highlightElement(driver, linkSignOut);
      linkSignOut.click();

      System.out.println("3. Sign Out");
  }
	
	@AfterClass
	public void tearDown () throws Exception
	{
		driver.quit();		
	}
}

Output:

2. Search For T-Shirt
1. Sign In
3. Sign Out

This works the same as if we had a @Test annotation at the method levels. If we removed the class @Test annotation and added @Test above the three test methods, the default execution order would be the same.


Note

As a side note, only the methods with a public access modifier are considered as test methods when marking the class with a @Test annotation. Any other access modifier will not set the method as a test method. If I change one of these methods to private then that method will not be executed.


# Priority Attributes For Test Methods

The @Test annotation has a lot of attributes. We can see those attributes by going to the TestNG Library -> TestNG jar file -> org.testng -> annotations, then scroll down to the Test.class and expand.


test attributes

Let's focus on the priority attribute. The purpose of a priority attribute is to determine the execution order for our test method.

We use a priority attribute by writing priority within a parenthesis after the @Test annotation. The priority attribute uses an Integer data type. The lowest number gets executed first. Some people start at zero but I prefer to use 1 since it’s the first test method.

Let's update our tests to use priority.

public class PriorityExecutionOrder
{
	@BeforeClass
	public void setUp() {}
	
	@Test (priority = 1)
	public void signIn() {}

    @Test (priority = 2)
	public void searchTShirt() {}	

	@Test (priority = 3)
	public void signOut() {}
	
	@AfterClass
	public void tearDown() {}
}

If we now execute this, we see it run in the following order:

1. Sign In
2. Search For T-Shirt
3. Sign Out



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR