TestNG | How to set Test Priority in Selenium
TestNG is a Testing framework, that covers different types of test designs like a unit test, functional test, end to end test, UI test and integration test.
You can run a single or multiple test cases in your Testng code.
If test priority is not defined while, running multiple test cases, TestNG assigns all @Test a priority as zero(0).
Now, while running; lower priorities will be scheduled first.
How to set Priority in TestNG
We will be modifying the @Test annotation with Priority Parameter so that each test should run against to the priority assigned to them.
Now as you can see we have assigned the Priority to each test case means test case will the lower priority value will be executed first.
Priority in testNG in action
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Test; public class Priority_In_testNG { WebDriver driver; // Method 1: Open Browser say Firefox @Test (priority=1) public void openBrowser() { driver = new FirefoxDriver(); } // Method 2: Launch Google.com @Test (priority=2) public void launchGoogle() { driver.get("http://www.google.co.in"); } // Method 3: Perform a search using "Facebook" @Test (priority=3) public void peformSeachAndClick1stLink() { driver.findElement(By.xpath(".//*[@title='Search']")).sendKeys("Facebook"); } // Method 4: Verify Google search page title. @Test (priority=4) public void FaceBookPageTitleVerification() throws Exception { driver.findElement(By.xpath(".//*[@value='Search']")).click(); Thread.sleep(3000); Assert.assertEquals(driver.getTitle().contains("Facebook - Google Search"), true); } }
Explanation of Code
After assigning priority to each testcases, run the above code using testNG as shown in Video-2 mentioned below.
Here, you can see that test cases are prioritized. Test case having lower priority are executed first i.e. now there is a sequential execution according to priority in the test cases. Hence, all test cases are passing now.
Note the console of eclipse:
Output :
PASSED: openBrowser PASSED: launchGoogle PASSED: peformSearchAndClick1stLink PASSED: FaceBookPageTitleVerification