0
0
Selenium Javatesting~15 mins

Why data separation improves test coverage in Selenium Java - Automation Benefits in Action

Choose your learning style9 modes available
Verify login functionality with multiple user credentials
Preconditions (1)
Step 1: Enter username 'user1@example.com' in the username field
Step 2: Enter password 'Password1!' in the password field
Step 3: Click the login button
Step 4: Verify that the user is redirected to the dashboard page
Step 5: Log out and return to the login page
Step 6: Repeat the above steps with username 'user2@example.com' and password 'Password2!'
Step 7: Repeat the above steps with username 'user3@example.com' and password 'Password3!'
✅ Expected Result: Each user is able to log in successfully and is redirected to the dashboard page after login
Automation Requirements - Selenium WebDriver with TestNG
Assertions Needed:
Verify current URL contains '/dashboard' after login
Verify login button is clickable before clicking
Verify logout button is present after login
Best Practices:
Use Page Object Model to separate page interactions
Use explicit waits to wait for elements to be clickable or visible
Separate test data from test logic using data providers
Use assertions to validate expected outcomes clearly
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.DataProvider;
import org.testng.annotations.Test;
import java.time.Duration;

public class LoginTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeClass
    public void setUp() {
        // Set path to chromedriver executable if needed
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.manage().window().maximize();
    }

    @DataProvider(name = "loginData")
    public Object[][] loginData() {
        return new Object[][] {
            {"user1@example.com", "Password1!"},
            {"user2@example.com", "Password2!"},
            {"user3@example.com", "Password3!"}
        };
    }

    @Test(dataProvider = "loginData")
    public void testLogin(String username, String password) {
        driver.get("https://example.com/login");

        WebElement usernameField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
        WebElement passwordField = driver.findElement(By.id("password"));
        WebElement loginButton = driver.findElement(By.id("loginBtn"));

        usernameField.clear();
        usernameField.sendKeys(username);
        passwordField.clear();
        passwordField.sendKeys(password);

        wait.until(ExpectedConditions.elementToBeClickable(loginButton));
        loginButton.click();

        // Verify URL contains '/dashboard'
        wait.until(ExpectedConditions.urlContains("/dashboard"));
        Assert.assertTrue(driver.getCurrentUrl().contains("/dashboard"), "User is not on dashboard page after login");

        // Verify logout button is present
        WebElement logoutButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("logoutBtn")));
        Assert.assertTrue(logoutButton.isDisplayed(), "Logout button is not displayed after login");

        // Logout to prepare for next test
        logoutButton.click();

        // Wait until back on login page
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("loginBtn")));
    }

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

This test uses Selenium WebDriver with TestNG to automate login tests for multiple users.

Setup: We open a Chrome browser and set an explicit wait to handle dynamic elements.

Data separation: The @DataProvider method loginData holds different username and password pairs. This separates test data from test logic, making it easy to add or change users without modifying the test code.

Test method: The testLogin method runs once for each data set. It navigates to the login page, enters credentials, clicks login, and verifies the user lands on the dashboard page by checking the URL and presence of the logout button.

Assertions: We assert the URL contains '/dashboard' and the logout button is visible to confirm successful login.

Best practices: We use explicit waits to avoid timing issues, clear input fields before typing, and use Page Object Model principles by separating data and actions logically (though a full Page Object class is not shown here for simplicity).

Cleanup: After each login, the test logs out to reset the state for the next user. Finally, the browser closes after all tests.

Common Mistakes - 4 Pitfalls
Hardcoding test data inside the test method
Using Thread.sleep() instead of explicit waits
Not verifying page state after login
Not resetting state between test runs
Bonus Challenge

Now add data-driven testing with 3 different inputs using TestNG DataProvider

Show Hint