Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests
Test prioritization in Selenium Java involves organizing and executing test cases based on their importance or urgency. This ensures that the most critical tests are run first, allowing for quicker identification of defects in essential features. Here’s a deep dive into test prioritization in Selenium with Java, including examples:

Why Test Prioritization?

  1. Efficiency: Running critical tests first ensures that major issues are detected early.
  2. Risk Management: Helps in mitigating risks by focusing on high-impact areas.
  3. Resource Optimization: Optimal use of limited test resources (time, infrastructure).

How to Implement Test Prioritization in Selenium Java

Using TestNG

TestNG is a popular testing framework that allows for test prioritization through annotations. Here’s how you can use it:

1. Prioritization Using @Test Annotation:

TestNG allows you to set the priority of test methods using the ‘priority‘ attribute in the ‘@Test‘ annotation.

				
					import org.testng.annotations.Test;

public class TestPriorityExample {

    @Test(priority = 1)
    public void loginTest() {
        System.out.println("Login Test");
        // Your login test code
    }

    @Test(priority = 2)
    public void addToCartTest() {
        System.out.println("Add to Cart Test");
        // Your add to cart test code
    }

    @Test(priority = 3)
    public void checkoutTest() {
        System.out.println("Checkout Test");
        // Your checkout test code
    }
}

				
			

In this example:

  • loginTest‘ has the highest priority and will run first.
  • addToCartTest‘ will run after ‘loginTest‘.
  • checkoutTest‘ will run last.

2. Grouping Tests:

You can also group tests and assign priorities to groups.

				
					import org.testng.annotations.Test;

public class TestGroupingExample {

    @Test(groups = {"sanity"})
    public void sanityTest1() {
        System.out.println("Sanity Test 1");
        // Your test code
    }

    @Test(groups = {"sanity"})
    public void sanityTest2() {
        System.out.println("Sanity Test 2");
        // Your test code
    }

    @Test(groups = {"regression"})
    public void regressionTest1() {
        System.out.println("Regression Test 1");
        // Your test code
    }

    @Test(groups = {"regression"})
    public void regressionTest2() {
        System.out.println("Regression Test 2");
        // Your test code
    }
}

				
			

You can then configure TestNG XML to run tests based on group priorities.

				
					<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite1">
    <test name="TestGroup1">
        <groups>
            <run>
                <include name="sanity" />
                <include name="regression" />
            </run>
        </groups>
        <classes>
            <class name="TestGroupingExample" />
        </classes>
    </test>
</suite>

				
			

3. Dependency-Based Prioritization:

TestNG also supports test dependencies using the ‘dependsOnMethods‘ attribute.

				
					import org.testng.annotations.Test;

public class TestDependencyExample {

    @Test
    public void initializeTest() {
        System.out.println("Initialize Test");
        // Your initialization code
    }

    @Test(dependsOnMethods = {"initializeTest"})
    public void mainTest() {
        System.out.println("Main Test");
        // Your main test code
    }

    @Test(dependsOnMethods = {"mainTest"})
    public void cleanUpTest() {
        System.out.println("Clean Up Test");
        // Your clean-up code
    }
}

				
			

In this example:

  • initializeTest‘ runs first.
  • mainTest‘ runs after ‘initializeTest‘.
  • cleanUpTest‘ runs after ‘mainTest‘.

Example with Selenium and Page Object Model (POM)

Here’s an example integrating test prioritization with Selenium and the Page Object Model:

LoginPage.java

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {
    WebDriver driver;

    @FindBy(id = "username")
    WebElement username;

    @FindBy(id = "password")
    WebElement password;

    @FindBy(id = "login")
    WebElement loginButton;

    public LoginPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public void login(String user, String pass) {
        username.sendKeys(user);
        password.sendKeys(pass);
        loginButton.click();
    }
}

				
			

HomePage.java

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class HomePage {
    WebDriver driver;

    @FindBy(id = "welcome")
    WebElement welcomeMessage;

    public HomePage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public String getWelcomeMessage() {
        return welcomeMessage.getText();
    }
}

				
			

TestClass.java

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestClass {
    WebDriver driver;
    LoginPage loginPage;
    HomePage homePage;

    @BeforeClass
    public void setup() {
        driver = new ChromeDriver();
        driver.get("https://example.com");
        loginPage = new LoginPage(driver);
        homePage = new HomePage(driver);
    }

    @Test(priority = 1)
    public void testLogin() {
        loginPage.login("user", "pass");
        String welcomeMsg = homePage.getWelcomeMessage();
        assert welcomeMsg.contains("Welcome");
    }

    @Test(priority = 2)
    public void testAnotherFeature() {
        // Test another feature
    }

    @AfterClass
    public void tearDown() {
        driver.quit();
    }
}

				
			

Test prioritization in Selenium Java, especially using TestNG, helps streamline test execution and ensures that critical tests are executed first. This is crucial for identifying major issues early in the testing cycle. By organizing tests using priorities, groups, and dependencies, you can effectively manage and optimize your testing efforts.

Scroll to Top