Write Your First Selenium Test
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
- Open the practice login page.
- Enter
demo@testqualityhub.comandDemo@1234. - Click Log in.
- 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
@BeforeMethodruns 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.sendKeystypes,clickclicks.WebDriverWaitwaits up to 10 seconds for the name to appear on the next page. You will learn waits properly in lesson 6.Assert.assertEqualsfails the test if the text is different. Without an assertion the test always "passes".@AfterMethodcloses 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
- What Is Selenium WebDriver?
- Set Up Java, Maven and Selenium
- Write Your First Selenium Test
- Selenium Locators: ID, CSS, XPath and More
- Clicks, Typing, Dropdowns, Alerts, Frames and Windows
- Waits in Selenium: Implicit, Explicit and Fluent
- TestNG for Selenium: Annotations, Assertions and Data
- Page Object Model in Selenium Java
- Selenium Framework Structure, Reports and CI