0
0
Selenium Javatesting~15 mins

Test groups in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality with test groups
Preconditions (2)
Step 1: Open the login page
Step 2: Enter 'user@example.com' in the email field
Step 3: Enter 'Password123' in the password field
Step 4: Click the login button
Step 5: Verify that the user is redirected to the dashboard page
✅ Expected Result: User successfully logs in and dashboard page is displayed
Automation Requirements - TestNG with Selenium WebDriver
Assertions Needed:
Verify current URL is dashboard URL after login
Verify login button is clickable before clicking
Best Practices:
Use @Test(groups = {...}) annotation to assign tests to groups
Use explicit waits to wait for elements
Use Page Object Model to separate page actions
Use assertions from TestNG Assert class
Automated Solution
Selenium Java
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.

Common Mistakes - 4 Pitfalls
Not using explicit waits before interacting with elements
Hardcoding URLs or credentials inside test methods
Not assigning tests to groups or using inconsistent group names
Using Thread.sleep() instead of explicit waits
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid and invalid)

Show Hint