Careeroza
Real World Example
Login Checks with Many Users
In a bank or insurance project, the login screen is tested with many users: valid, locked, expired password, blank fields. The Selenium part stays the same; only the data changes. First the data and expected results, as runnable Java:
Example
Expected result for each login data row:
import java.util.*;
public class Main {
record LoginCase(String email, String password, String expected) {}
public static void main(String[] args) {
List<LoginCase> cases = List.of(
new LoginCase("[email protected]", "Test@123", "/account"),
new LoginCase("[email protected]", "wrong", "Invalid email or password"),
new LoginCase("[email protected]", "Test@123", "Your account is locked"),
new LoginCase("", "", "Please fill out this field."));
int n = 1;
for (LoginCase c : cases) {
String who = c.email().isEmpty() ? "(blank)" : c.email();
System.out.printf("Case %d: %-16s -> expect %s%n", n++, who, c.expected());
}
}
}Output
Case 1: [email protected] -> expect /account Case 2: [email protected] -> expect Invalid email or password Case 3: [email protected] -> expect Your account is locked Case 4: (blank) -> expect Please fill out this field.
The Selenium Loop
for (LoginCase c : cases) {
driver.get("https://shopease.example.com/login");
driver.findElement(By.id("email")).sendKeys(c.email());
driver.findElement(By.id("password")).sendKeys(c.password());
driver.findElement(By.id("login-btn")).click();
String actual = driver.getCurrentUrl().endsWith("/account") ? "/account"
: driver.findElements(By.cssSelector(".error-message")).isEmpty()
? driver.findElement(By.id("email")).getDomProperty("validationMessage")
: driver.findElement(By.cssSelector(".error-message")).getText();
System.out.println((actual.equals(c.expected()) ? "PASS " : "FAIL ") + c.email());
driver.manage().deleteAllCookies(); // log out for the next case
}Example
Output:
Run LoginDataTest.main()Example output
PASS [email protected] PASS [email protected] PASS [email protected] PASS
In the medium level TestNG's @DataProvider replaces this hand-written loop, adds proper reporting and runs each row as its own test.