Automating the ShopEase Login

The Login Page

<form id="login-form">
  <input type="email" id="email" required>
  <input type="password" id="password" required>
  <button type="submit" id="login-btn">Login</button>
  <p class="error-message" hidden></p>
</form>
<!-- after success: redirect to /account, header shows <span id="welcome">Welcome, Priya</span> -->

Valid Login Test

public class LoginTest {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://shopease.example.com/login");
            driver.findElement(By.id("email")).sendKeys("[email protected]");
            driver.findElement(By.id("password")).sendKeys("Test@123");
            driver.findElement(By.id("login-btn")).click();

            String url = driver.getCurrentUrl();
            String welcome = driver.findElement(By.id("welcome")).getText();
            boolean pass = url.endsWith("/account") && welcome.equals("Welcome, Priya");
            System.out.println("URL: " + url);
            System.out.println("Header: " + welcome);
            System.out.println(pass ? "PASS valid login" : "FAIL valid login");
        } finally {
            driver.quit();
        }
    }
}

Example

Output:

Run LoginTest.main()

Example output

URL: https://shopease.example.com/account
Header: Welcome, Priya
PASS valid login

Invalid Login Test

driver.get("https://shopease.example.com/login");
driver.findElement(By.id("email")).sendKeys("[email protected]");
driver.findElement(By.id("password")).sendKeys("wrong-password");
driver.findElement(By.id("login-btn")).click();
String error = driver.findElement(By.cssSelector(".error-message")).getText();
System.out.println(error.equals("Invalid email or password") ? "PASS" : "FAIL: " + error);

Example

Output:

Run InvalidLoginTest.main()

Example output

PASS

Note: If the error appears after an AJAX call, findElement may run before the text arrives and the test fails sometimes. That is a timing problem; the fix (explicit waits) is the first module of the medium level.

Practice

Exercise: Write the steps for a test that searches ShopEase for "headphones", prints how many product cards are shown, and prints the name of the first one.

Show answer
driver.get("https://shopease.example.com");
WebElement search = driver.findElement(By.id("search"));
search.clear();
search.sendKeys("headphones", Keys.ENTER);
List<WebElement> cards = driver.findElements(By.cssSelector(".product-card"));
System.out.println("Results: " + cards.size());
if (!cards.isEmpty()) {
    System.out.println("First: " + cards.get(0).findElement(By.tagName("h3")).getText());
}