Now if you want to put a number of tests under any test class and want to run everyone in one shot, then such situations will happen and we need to prioritize every test otherwise TestNG will run all tests in alphabetical order.
- With the help of TestNG ‘@Test‘ annotation, we can do many tests in a single Testing file.
- The number of test in the same test class and all to run in one shot then it required to prioritize our @Test methods.
- You can run one or more test cases in your testng.xml file.
- If we do not give @Test priority then execute @Test in alphabetical order
Syntax:
@Test(priority = 1)
Example:
package testNGFrameWork;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class testNGRunPrioritywise {
WebDriver driver;
//Open the browser
@Test(priority = 1)
public void launchBrowser() {
System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
//Close the browser
@Test(priority = 3)
public void closeBrowser() {
driver.close();
}
//Navigate to google.com
@Test(priority = 2)
public void verifyTitle() {
driver.get("https:www.google.com");
String pageTitle=driver.getTitle();
Assert.assertEquals(pageTitle, "Google");
}
}
In this way, we can prioritize tests in TestNG to control the execution flow.