import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
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.chrome.ChromeDriver;
public class LoginTest {
WebDriver driver;
@ParameterizedTest
@CsvSource({
"user1@example.com, Password1",
"user2@example.com, Password2",
"user3@example.com, Password3"
})
void testLoginWithMultipleUsers(String username, String password) {
driver = new ChromeDriver();
try {
driver.get("http://example.com/login");
driver.findElement(By.id("email")).sendKeys(username);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.id("loginButton")).click();
// Wait for redirection - simple implicit wait or explicit wait can be used
Thread.sleep(2000); // For simplicity here, better to use explicit wait in real tests
String currentUrl = driver.getCurrentUrl();
assertEquals("http://example.com/dashboard", currentUrl, "User should be redirected to dashboard");
boolean errorDisplayed = driver.findElements(By.id("loginError")).size() > 0;
assertFalse(errorDisplayed, "No error message should be displayed after successful login");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
driver.quit();
}
}
}
This test uses JUnit 5's @ParameterizedTest with @CsvSource to run the same login test with different username and password combinations.
Each test iteration opens the login page, enters the provided credentials, clicks login, and verifies the user is redirected to the dashboard URL.
Assertions check the URL and that no error message is shown.
Parameterization reduces duplication by avoiding writing separate test methods for each user credential set.
The try-finally block ensures the browser closes after each test run.
Note: Thread.sleep is used here for simplicity but explicit waits are recommended in real tests.