Introduction
The priority function is used to set the priorities for different test cases in Selenium. We can assign only integer values as a priority, and the value can be either positive, negative, or zero. In the priority, function lower the value of the priority number higher is the priority of the test case. The default priority for all the test cases is always zero. The developer can assign only one priority value to a test case. The syntax for the priority function looks like this,
@Test (priority = 1)
public void Test1(){
//Code to be executed
}
In the above example, we assign the priority of Test1 as 1.
Example
Let us look at an example of the priority function to understand it better.
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.chrome.ChromeDriver;
public class priorityTest {
WebDriver driver = new ChromeDriver();
@Test (priority = 1)
public void FirstTestCase() {
driver.close();
System.out.println("This is the first test case");
}
@Test (priority = 0)
public void SecondTestCase() {
System.out.println("This is the second test case");
}
}
In the above example, we create a class named priorityTest. We assign the priority of FirstTestCase as one and the priority of SecondTestCase as zero. Since zero is the smaller number, SecondTestCase will get executed before FirstTestCase. In case no priority was declared, the test cases would get executed in alphabetical order, i.e., FirstTestCase would be executed before SecondTestCase.