Selenium with Java · Getting Started

Write Your First Selenium Test

Lesson 3 of 9 · 9 min read

A test is not a test until it checks something. In this lesson you log in to the practice login page and assert that the welcome text appears.

The manual test first

  1. Open the practice login page.
  2. Enter demo@testqualityhub.com and Demo@1234.
  3. Click Log in.
  4. Expected: the account page opens and shows "Demo Tester".

Always write the manual steps before you automate. If you cannot describe the test, you cannot automate it.

The automated test

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
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.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.time.Duration;

public class LoginTest {
    private WebDriver driver;

    @BeforeMethod
    public void openBrowser() {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test
    public void validUserCanLogIn() {
        driver.get("https://testqualityhub.com/practice/login/");
        driver.findElement(By.id("email")).sendKeys("demo@testqualityhub.com");
        driver.findElement(By.id("password")).sendKeys("Demo@1234");
        driver.findElement(By.id("submit")).click();

        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        String name = wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.cssSelector("[data-testid='account-name']"))).getText();

        Assert.assertEquals(name, "Demo Tester");
    }

    @AfterMethod
    public void closeBrowser() {
        if (driver != null) {
            driver.quit();
        }
    }
}

What each part does

  • @BeforeMethod runs before every test. Each test gets a fresh browser, so tests do not affect each other.
  • driver.get() opens a URL and waits for the page to load.
  • findElement(By.id(...)) finds one element. sendKeys types, click clicks.
  • WebDriverWait waits up to 10 seconds for the name to appear on the next page. You will learn waits properly in lesson 6.
  • Assert.assertEquals fails the test if the text is different. Without an assertion the test always "passes".
  • @AfterMethod closes the browser even when the test fails.

Add a negative test

@Test
public void wrongPasswordShowsError() {
    driver.get("https://testqualityhub.com/practice/login/");
    driver.findElement(By.id("email")).sendKeys("demo@testqualityhub.com");
    driver.findElement(By.id("password")).sendKeys("wrong-password");
    driver.findElement(By.id("submit")).click();

    String error = driver.findElement(By.cssSelector("[data-testid='login-error']")).getText();
    Assert.assertTrue(error.startsWith("Invalid email or password"));
}
Try it: make the test fail on purpose (change "Demo Tester" to "Demo") and read the error message. Knowing what a failure looks like is half of debugging.
All 9 lessons in Selenium with Java
  1. What Is Selenium WebDriver?
  2. Set Up Java, Maven and Selenium
  3. Write Your First Selenium Test
  4. Selenium Locators: ID, CSS, XPath and More
  5. Clicks, Typing, Dropdowns, Alerts, Frames and Windows
  6. Waits in Selenium: Implicit, Explicit and Fluent
  7. TestNG for Selenium: Annotations, Assertions and Data
  8. Page Object Model in Selenium Java
  9. Selenium Framework Structure, Reports and CI