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

Grouping tests in Selenium with Java is an effective way to organize and manage your test cases, especially when dealing with a large suite of tests. This allows you to run specific groups of tests based on different criteria, such as functionality, priority, or module. Here’s an in-depth guide with examples on how to group tests using TestNG, a popular testing framework for Java.

Step-by-Step Guide to Grouping Tests in Selenium with Java

Step 1: Set Up Your Project

First, ensure you have the following set up in your project:

  • Java Development Kit (JDK)
  • Selenium WebDriver
  • TestNG

Add the necessary dependencies in your ‘pom.xml‘ file if you’re using Maven:

				
					<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

				
			

Step 2: Create Your Test Classes

Create test classes and annotate your test methods with @Test from TestNG. Use the groups attribute to assign tests to specific groups.

				
					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 LoginTests {

    WebDriver driver;

    @BeforeClass
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
    }

    @Test(groups = {"smoke"})
    public void validLoginTest() {
        // Test code for valid login
    }

    @Test(groups = {"regression"})
    public void invalidLoginTest() {
        // Test code for invalid login
    }

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

				
			
				
					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 RegistrationTests {

    WebDriver driver;

    @BeforeClass
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
    }

    @Test(groups = {"smoke"})
    public void validRegistrationTest() {
        // Test code for valid registration
    }

    @Test(groups = {"regression"})
    public void invalidRegistrationTest() {
        // Test code for invalid registration
    }

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

				
			

Step 3: Create TestNG XML Configuration

Create a TestNG XML file to define your test suite and specify which groups of tests you want to run.

				
					<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="AllTestsSuite">
    <test name="SmokeTests">
        <groups>
            <run>
                <include name="smoke" />
            </run>
        </groups>
        <classes>
            <class name="LoginTests" />
            <class name="RegistrationTests" />
        </classes>
    </test>

    <test name="RegressionTests">
        <groups>
            <run>
                <include name="regression" />
            </run>
        </groups>
        <classes>
            <class name="LoginTests" />
            <class name="RegistrationTests" />
        </classes>
    </test>
</suite>

				
			

Step 4: Run Your Tests

Run the TestNG XML file as a TestNG suite from your IDE or command line.

  • From IDE: Right-click the XML file and select “Run As > TestNG Suite”.
  • From Command Line:
				
					mvn test -DsuiteXmlFile=testng.xml

				
			

Additional Grouping Features

Group Dependencies

You can specify that a test group depends on another group using the ‘dependsOnGroups‘ attribute.

				
					@Test(groups = {"regression"}, dependsOnGroups = {"smoke"})
public void dependentTest() {
    // This test will only run if the smoke tests pass
}

				
			

Excluding Groups

You can exclude specific groups from running in the TestNG XML file.

				
					<groups>
    <run>
        <include name="smoke" />
        <exclude name="regression" />
    </run>
</groups>

				
			

Grouping tests in Selenium with Java using TestNG is a powerful way to organize and run your test cases. By using groups, you can easily manage and execute tests based on different criteria, which helps in maintaining a scalable and efficient test automation suite.

Scroll to Top