Test groups in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.time.Duration; public class LoginTest { WebDriver driver; WebDriverWait wait; @BeforeClass public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.manage().window().maximize(); } @Test(groups = {"smoke", "login"}) public void testValidLogin() { driver.get("https://example.com/login"); WebElement emailField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email"))); emailField.sendKeys("user@example.com"); WebElement passwordField = driver.findElement(By.id("password")); passwordField.sendKeys("Password123"); WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn"))); loginButton.click(); wait.until(ExpectedConditions.urlContains("/dashboard")); String currentUrl = driver.getCurrentUrl(); Assert.assertTrue(currentUrl.contains("/dashboard"), "User is not redirected to dashboard after login"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
This test uses TestNG with Selenium WebDriver to automate the login test case.
Setup: The @BeforeClass method initializes the ChromeDriver and WebDriverWait for explicit waits.
Test method: The @Test annotation includes groups = {"smoke", "login"} to assign this test to two groups. This allows running tests selectively by group.
We open the login page, wait for the email field to be visible, enter the email and password, then wait until the login button is clickable before clicking it.
After clicking, we wait until the URL contains '/dashboard' to confirm navigation, then assert that the current URL is correct.
Teardown: The @AfterClass method closes the browser.
This structure follows best practices: explicit waits avoid flaky tests, grouping allows flexible test runs, and assertions verify expected outcomes.
Now add data-driven testing with 3 different sets of login credentials (valid and invalid)
Practice
groups in Selenium Java tests?Solution
Step 1: Understand the role of test groups
Test groups help organize tests into categories, making it easier to run specific sets of tests.Step 2: Identify the correct purpose
Groups do not affect WebDriver speed, report generation, or browser choice directly.Final Answer:
To organize tests into categories for selective execution -> Option BQuick Check:
Test groups = Organize tests [OK]
- Thinking groups speed up WebDriver
- Confusing groups with report generation
- Assuming groups change browser settings
Solution
Step 1: Recall TestNG groups syntax
The correct syntax usesgroups = {"group1", "group2"}with curly braces and double quotes for each group.Step 2: Compare options
@Test(groups = {"smoke", "regression"}) matches the correct syntax. Options A, B, and D misuse quotes or attribute name. @Test(groups = ["smoke", "regression"]) uses square brackets which is invalid in Java annotations.Final Answer:
@Test(groups = {"smoke", "regression"}) -> Option AQuick Check:
Groups use curly braces and quotes [OK]
- Using 'group' instead of 'groups'
- Passing groups as a single string
- Using square brackets instead of curly braces
@Test(groups = {"fast"})
public void testA() {}
@Test(groups = {"slow"})
public void testB() {}
@Test(groups = {"fast", "regression"})
public void testC() {}Solution
Step 1: Identify tests with group "fast"
testA has group "fast"; testC has groups "fast" and "regression"; testB has "slow" only.Step 2: Determine which tests run when "fast" group is executed
Only tests assigned to "fast" group run, so testA and testC run; testB does not.Final Answer:
testA and testC -> Option CQuick Check:
Run group "fast" = testA, testC [OK]
- Running tests from other groups
- Ignoring tests with multiple groups
- Assuming only single-group tests run
@Test(groups = "ui, integration")
public void testUIIntegration() {}Solution
Step 1: Check groups attribute format
Groups must be passed as an array of strings using curly braces, not a single comma-separated string.Step 2: Identify the error in the code
The code uses a single string "ui, integration" instead of {"ui", "integration"}.Final Answer:
Groups should be passed as an array, not a single string -> Option AQuick Check:
Groups need array syntax [OK]
- Passing groups as one string with commas
- Forgetting curly braces for arrays
- Misunderstanding annotation syntax
@Test(groups = {"sanity"})
public void testLogin() {}
@Test(groups = {"regression", "sanity"})
public void testCheckout() {}
@Test(groups = {"regression"})
public void testSearch() {}How would you run only the tests that belong exclusively to the "sanity" group, excluding those that also belong to "regression"?Solution
Step 1: Identify tests with only "sanity" group
testLogin belongs only to "sanity"; testCheckout belongs to both "regression" and "sanity"; testSearch belongs only to "regression".Step 2: Determine how to run only exclusive "sanity" tests
Use TestNG XML to include "sanity" group and exclude "regression" group to run only testLogin.Final Answer:
Run tests with group "sanity" and exclude group "regression" in the TestNG XML -> Option DQuick Check:
Include "sanity" exclude "regression" = exclusive tests [OK]
- Running all tests with "sanity" ignoring exclusions
- Running "regression" group instead
- Running both groups together without filtering
Ready to prove it?
Pass the test and this pattern moves unknown
