0
0
JUnittesting~15 mins

Why organization scales test suites in JUnit - Automation Benefits in Action

Choose your learning style9 modes available
Verify that the login functionality works correctly for multiple users
Preconditions (2)
Step 1: Open the login page
Step 2: Enter username 'user1' and password 'Password1!'
Step 3: Click the login button
Step 4: Verify that the user is redirected to the dashboard page
Step 5: Logout
Step 6: Repeat steps 2-5 for 'user2' with password 'Password2!'
Step 7: Repeat steps 2-5 for 'user3' with password 'Password3!'
✅ Expected Result: Each user should be able to login successfully and reach the dashboard page without errors
Automation Requirements - JUnit 5
Assertions Needed:
Verify current URL is dashboard URL after login
Verify login button is clickable
Verify logout button is present after login
Best Practices:
Use parameterized tests to run the same test with multiple user credentials
Use explicit waits if needed for page load
Keep test methods small and focused
Use descriptive test method names
Automated Solution
JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
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 java.time.Duration;

public class LoginTest {
    WebDriver driver;
    WebDriverWait wait;

    void setup() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.get("https://example.com/login");
    }

    void teardown() {
        if (driver != null) {
            driver.quit();
        }
    }

    @ParameterizedTest(name = "Login test for user: {0}")
    @CsvSource({
        "user1, Password1!",
        "user2, Password2!",
        "user3, Password3!"
    })
    void testLogin(String username, String password) {
        setup();
        try {
            WebElement usernameField = wait.until(ExpectedConditions.elementToBeClickable(By.id("username")));
            usernameField.clear();
            usernameField.sendKeys(username);

            WebElement passwordField = driver.findElement(By.id("password"));
            passwordField.clear();
            passwordField.sendKeys(password);

            WebElement loginButton = driver.findElement(By.id("loginButton"));
            assertTrue(loginButton.isEnabled(), "Login button should be enabled");
            loginButton.click();

            wait.until(ExpectedConditions.urlContains("/dashboard"));
            assertTrue(driver.getCurrentUrl().contains("/dashboard"), "URL should contain /dashboard after login");

            WebElement logoutButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("logoutButton")));
            assertNotNull(logoutButton, "Logout button should be visible after login");

            logoutButton.click();
            wait.until(ExpectedConditions.urlContains("/login"));
        } finally {
            teardown();
        }
    }
}

This test uses JUnit 5 with parameterized tests to run the login test for multiple users.

The @CsvSource annotation provides three sets of username and password inputs.

In setup(), we open the browser and navigate to the login page.

We wait explicitly for the username field to be clickable before entering credentials.

We assert the login button is enabled before clicking it.

After login, we wait for the URL to contain '/dashboard' and assert it.

We verify the logout button is visible, then click it to logout and wait for the login page again.

Finally, teardown() closes the browser to clean up.

This approach scales well because adding more users only requires adding more lines in the @CsvSource. It avoids repeating code and keeps tests clear and maintainable.

Common Mistakes - 3 Pitfalls
Hardcoding user credentials inside the test method instead of using parameterized tests
Not using explicit waits and relying on Thread.sleep()
Not closing the browser after each test run
Bonus Challenge

Now add data-driven testing with 3 different invalid login inputs and verify error messages

Show Hint