0
0
Selenium Javatesting~15 mins

Why TestNG structures test execution in Selenium Java - Automation Benefits in Action

Choose your learning style9 modes available
Verify TestNG test execution order with multiple test methods
Preconditions (2)
Step 1: Create a TestNG test class with three test methods: testA, testB, testC
Step 2: Annotate test methods with @Test and assign priorities: testA(priority=2), testB(priority=1), testC(priority=3)
Step 3: In each test method, open the browser and navigate to https://example.com
Step 4: Print a message to the console indicating which test method is running
Step 5: Run the TestNG test suite
Step 6: Observe the order in which test methods execute
✅ Expected Result: Test methods execute in the order of their priority: testB, testA, testC. Console output shows messages in this order.
Automation Requirements - TestNG with Selenium WebDriver
Assertions Needed:
Verify that test methods run in the order of their assigned priority
Verify that the browser navigates to the correct URL in each test
Best Practices:
Use @BeforeMethod and @AfterMethod for setup and teardown
Use explicit waits if needed
Use meaningful test method names
Avoid hardcoded waits
Use assertions to verify expected conditions
Automated Solution
Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestNGExecutionOrder {
    private WebDriver driver;
    private final String url = "https://example.com";

    @BeforeMethod
    public void setUp() {
        // Set path to chromedriver executable if needed
        driver = new ChromeDriver();
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }

    @Test(priority = 2)
    public void testA() {
        driver.get(url);
        System.out.println("Running testA");
        Assert.assertEquals(driver.getCurrentUrl(), url, "URL should be https://example.com");
    }

    @Test(priority = 1)
    public void testB() {
        driver.get(url);
        System.out.println("Running testB");
        Assert.assertEquals(driver.getCurrentUrl(), url, "URL should be https://example.com");
    }

    @Test(priority = 3)
    public void testC() {
        driver.get(url);
        System.out.println("Running testC");
        Assert.assertEquals(driver.getCurrentUrl(), url, "URL should be https://example.com");
    }
}

This test class uses TestNG to demonstrate how test execution order is controlled by the priority attribute.

The @BeforeMethod sets up the browser before each test, and @AfterMethod closes it after each test to keep tests independent.

Each test method navigates to the same URL and prints a message to the console to show execution order.

Assertions check that the browser is on the expected URL.

When running this test, TestNG runs testB first because it has priority 1, then testA (priority 2), then testC (priority 3).

This shows how TestNG structures test execution order based on priority, helping organize tests clearly and predictably.

Common Mistakes - 4 Pitfalls
Not using @BeforeMethod and @AfterMethod for setup and teardown
Not assigning priorities and expecting tests to run in code order
Using Thread.sleep instead of proper waits
Hardcoding URLs or test data inside test methods
Bonus Challenge

Now add data-driven testing with 3 different URLs to verify TestNG execution order with different inputs

Show Hint