
📌TestNG and Test Priority in Selenium Introduction to TestNG Priority
TestNG and Test Priority in Selenium: Testing is a powerful testing framework that helps automate test execution in Selenium. When running multiple test cases, TestNG provides a feature called priority, which allows you to define the order in which test cases should execute.
🔹 By default, TestNG assigns all @Test methods a priority of 0.
🔹 Tests with lower priority numbers execute first.
🔹 If no priority is assigned, TestNG follows alphabetical order of method names.
Why Use Priority in TestNG?
✔TestNG @Test Priority in Selenium: Ensures test cases execute in a specific order
✔ Helps in managing dependencies between tests
✔ Prevents test failures due to incorrect execution sequence
📌How TestNG Executes @Test Methods Without Priority
TestNG and Test Priority in Selenium: If no priority is assigned, TestNG will execute test methods in alphabetical order.
Example: Test Execution Without Priority
java
import org.testng.annotations.Test;
public class TestWithoutPriority {
@Test
public void openBrowser() {
System.out.println(“Opening Browser”);
}
@Test
public void launchGoogle() {
System.out.println(“Launching Google”);
}
@Test
public void performSearch() {
System.out.println(“Performing Search”);
}
@Test
public void verifyTitle() {
System.out.println(“Verifying Page Title”);
}
}
Expected Execution Order (Alphabetical Order)
scss
launchGoogle()
openBrowser()
performSearch()
verifyTitle()
🔴 Problem: TestNG @Test Priority in Selenium: The browser should open first before launching Google, and the search should be performed before verifying the title. Since there is no priority assigned, TestNG executes them in alphabetical order, leading to failures.
📌How to Set Priority in TestNG
TestNG and Test Priority in Selenium: To define the execution order, use the priority attribute in the @Test annotation.
Syntax:
java
@Test(priority = 0) // Highest priority, runs first
@Test(priority = 1) // Runs second
@Test(priority = 2) // Runs third
Example: Assigning Priority to Test Cases
java
import org.testng.annotations.Test;
public class TestWithPriority {
@Test(priority = 0)
public void openBrowser() {
System.out.println(“Opening Browser”);
}
@Test(priority = 1)
public void launchGoogle() {
System.out.println(“Launching Google”);
}
@Test(priority = 2)
public void performSearch() {
System.out.println(“Performing Search”);
}
@Test(priority = 3)
public void verifyTitle() {
System.out.println(“Verifying Page Title”);
}
}
Expected Execution Order:
scss
openBrowser() // Priority 0
launchGoogle() // Priority 1
performSearch() // Priority 2
verifyTitle() // Priority 3
✅ Solution: Now, the test cases execute in the correct sequence!
📌Handling Test Cases with the Same Priority
TestNG and Test Priority in Selenium: If multiple test methods have the same priority, TestNG executes them in alphabetical order.
Example: Test Cases with Same Priority
java
import org.testng.annotations.Test;
public class TestSamePriority {
@Test(priority = 1)
public void methodA() {
System.out.println(“Method A”);
}
@Test(priority = 1)
public void methodB() {
System.out.println(“Method B”);
}
@Test(priority = 1)
public void methodC() {
System.out.println(“Method C”);
}
}
Expected Execution Order (Alphabetical Order):
scss
methodA()
methodB()
methodC()
💡 Tip: If the order of execution is important, assign different priority values.
📌Mixing Prioritized and Non-Prioritized Methods
TestNG and Test Priority in Selenium: If some test cases have a priority assigned while others don’t, TestNG executes:
1️⃣ Non-prioritized methods first (in alphabetical order)
2️⃣ Prioritized methods in ascending order of priority
Example: Mixing Prioritized & Non-Prioritized Methods
java
import org.testng.annotations.Test;
public class TestMixedPriority {
@Test
public void methodX() { // No priority assigned
System.out.println(“Method X”);
}
@Test(priority = 0)
public void methodY() {
System.out.println(“Method Y”);
}
@Test(priority = 1)
public void methodZ() {
System.out.println(“Method Z”);
}
}
Expected Execution Order:
scss
methodX() // Non-prioritized, executed first
methodY() // Priority 0
methodZ() // Priority 1
Issue: If methodX() is dependent on methodY(), execution might fail.
✅ Solution: Assign priorities to all methods to avoid unpredictable execution.
📌Case Sensitivity in TestNG Priority
TestNG and Test Priority in Selenium: TestNG is case-sensitive when defining priorities. The correct syntax is:
java
@Test(priority = 1) // ✅ Correct
@Test(PRIORITY = 1) // ❌ Incorrect (will cause a compilation error)
🚨 Ensure you use lowercase priority in the annotation.
📌Key Takeaways on TestNG @Test Priority
✔ Lower priority numbers execute first
✔ Methods without priority run first (in alphabetical order)
✔ Same-priority methods execute in alphabetical order
✔ Mixing prioritized and non-prioritized methods can lead to unexpected results
✔ TestNG priority is case-sensitive (priority = X, not PRIORITY = X)
📌Conclusion
TestNG and Test Priority in Selenium: Setting @Test priority in TestNG allows better control over test execution order. It helps in organizing dependent test cases and ensures that tests run in the correct sequence.
Best Practices for Using Priority in TestNG
✅ Always assign priority when test execution order matters
✅ Use unique priority values to avoid alphabetical ordering issues
✅ Avoid mixing prioritized and non-prioritized methods
✅ Use dependsOnMethods along with priority if tests have dependencies By implementing priority correctly, you can ensure smooth and structured test execution in Selenium automation!