assert_
glossary ↓ downloads
track: java · selenium
suite 04 · first selenium framework

Your first Selenium script

First Selenium framework15 min hands-on
as a manual tester

Test case TC-001: open the app, verify the login page loads.

in automation

Today that test case becomes code that opens a real Chrome window by itself. This is the moment the career change gets real.

Everything so far — Java, the terminal, locators — converges here. Selenium's WebDriver is an object (OOP lesson!) whose methods drive a real browser. Modern Selenium handles the browser driver automatically via Selenium Manager, so there's nothing to download: if Chrome is installed, this just runs.

Add this class to your starter project at src/test/java/FirstSeleniumTest.java:

FirstSeleniumTest.java
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class FirstSeleniumTest {

    @Test
    void loginPageLoads() {
        WebDriver driver = new ChromeDriver();      // opens a real Chrome window

        driver.get("https://www.saucedemo.com");    // navigate

        String title = driver.getTitle();            // read state from the browser
        System.out.println("Page title is: " + title);

        assertEquals("Swag Labs", title);            // the assertion — expected vs actual

        driver.quit();                               // ALWAYS close the browser
    }
}

Run it with the green arrow, or from PowerShell:

PowerShell — in the project folder
mvn test -Dtest=FirstSeleniumTest
predict: what does this print? think it through, then reveal
output
[INFO] Running FirstSeleniumTest
Page title is: Swag Labs
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS

A Chrome window flashed open, loaded the page, and closed. That flash was your code driving a browser. Read the test again top to bottom — it's your manual TC-001, line for line: navigate, observe, compare to expected.

The two lines that matter most

new ChromeDriver() starts a browser session; driver.quit() ends it. Forget quit() and you leak browser processes — run a suite of 50 tests that way and your machine crawls. (Suite 05's @AfterEach will guarantee it runs even when tests fail.)

If it fails to start: "Unable to obtain driver" usually means Chrome isn't installed or a corporate proxy blocks Selenium Manager's one-time download — install Chrome, try once on a personal network. A SessionNotCreatedException mentioning versions means Chrome just auto-updated; simply re-run.
⚠ when it breaks — the classics
Selenium can't start the browser
org.openqa.selenium.SessionNotCreatedException: Could not start a new session… unable to obtain driver for chrome

Fix: Chrome isn't installed, or a corporate proxy blocked Selenium Manager's one-time driver download. Install Chrome; try once on a personal network — the driver caches afterwards. A version-mismatch message usually just means Chrome auto-updated: re-run.

Java doesn't know what ChromeDriver is
java: cannot find symbol  symbol: class ChromeDriver

Fix: Either the import is missing (org.openqa.selenium.chrome.ChromeDriver) or Maven hasn't finished downloading dependencies — check the bottom-right progress bar, then Maven → Reload project.

The browser flashed open and instantly closed — is that broken?
Tests run: 1, Failures: 0, Errors: 0 — BUILD SUCCESS

Fix: That flash is success: your test opened Chrome, asserted, and quit. Add a breakpoint or a temporary wait if you want to watch it work.

⚡ exercise · three variations
  1. Change the assertion to expect "Wrong Title", run, and read the failure: expected: <Wrong Title> but was: <Swag Labs>. That message format will be your daily companion. Fix it.
  2. Add a second test method googleLoads() that opens https://www.google.com and asserts the title equals "Google".
  3. Print driver.getCurrentUrl() too — notice saucedemo redirects to a trailing slash. URL and title are your two cheapest checkpoints.
key takeaways
  • new ChromeDriver() opens a browser; get() navigates; getTitle() reads state; quit() cleans up.
  • Selenium Manager handles drivers automatically — nothing to download.
  • Your first Selenium test is your first manual test case, translated.
← Back to roadmap Finding & acting on elements →