Beginner Chapter 3 · 9 min read

Writing Your First Test

Write, run, and understand your first Selenium test — launch a browser, navigate to a page, interact with elements, and verify results.

Your First Selenium Test

Every Selenium journey starts with launching a browser, navigating to a URL, and verifying something on the page. Let's write a complete test that opens a website, checks the title, and interacts with the page.

The Test Lifecycle

Every Selenium test follows the same pattern: Setup (create driver, configure options) → Action (navigate, click, type) → Assert (verify results) → Teardown (close browser). This pattern keeps tests clean, independent, and reliable.

Browser Options

Selenium 4 uses Options classes to configure browsers — headless mode, window size, user agent, download directory, and more. Always configure options before creating the driver instance.

FirstTest.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;
import org.testng.annotations.*;

public class FirstTest {

    WebDriver driver;

    @BeforeMethod
    public void setup() {
        ChromeOptions options = new ChromeOptions();
        // options.addArguments("--headless=new");  // Headless mode
        options.addArguments("--window-size=1920,1080");
        options.addArguments("--no-sandbox");

        driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(
            java.time.Duration.ofSeconds(10)
        );
    }

    @Test
    public void testPageTitle() {
        driver.get("https://www.selenium.dev/");

        String title = driver.getTitle();
        System.out.println("Page title: " + title);
        Assert.assertTrue(title.contains("Selenium"),
            "Title should contain 'Selenium'");
    }

    @Test
    public void testNavigation() {
        driver.get("https://www.selenium.dev/");

        // Navigate to documentation
        WebElement docsLink = driver.findElement(
            By.linkText("Documentation")
        );
        docsLink.click();

        String currentUrl = driver.getCurrentUrl();
        System.out.println("Current URL: " + currentUrl);
        Assert.assertTrue(currentUrl.contains("documentation"),
            "Should navigate to docs page");
    }

    @Test
    public void testBrowserNavigation() {
        driver.get("https://www.selenium.dev/");
        String firstUrl = driver.getCurrentUrl();

        driver.get("https://www.selenium.dev/documentation/");
        Assert.assertTrue(driver.getCurrentUrl().contains("documentation"));

        // Navigate back
        driver.navigate().back();
        Assert.assertEquals(driver.getCurrentUrl(), firstUrl);

        // Navigate forward
        driver.navigate().forward();
        Assert.assertTrue(driver.getCurrentUrl().contains("documentation"));

        // Refresh
        driver.navigate().refresh();
    }

    @AfterMethod
    public void teardown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
test_first.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import pytest

@pytest.fixture
def driver():
    options = Options()
    # options.add_argument('--headless=new')
    options.add_argument('--window-size=1920,1080')
    options.add_argument('--no-sandbox')

    driver = webdriver.Chrome(options=options)
    driver.implicitly_wait(10)
    yield driver
    driver.quit()


def test_page_title(driver):
    """Verify page title"""
    driver.get("https://www.selenium.dev/")

    title = driver.title
    print(f"Page title: {title}")
    assert "Selenium" in title, "Title should contain 'Selenium'"


def test_navigation(driver):
    """Navigate and verify URL"""
    driver.get("https://www.selenium.dev/")

    docs_link = driver.find_element(By.LINK_TEXT, "Documentation")
    docs_link.click()

    current_url = driver.current_url
    print(f"Current URL: {current_url}")
    assert "documentation" in current_url, "Should navigate to docs"


def test_browser_navigation(driver):
    """Test back, forward, refresh"""
    driver.get("https://www.selenium.dev/")
    first_url = driver.current_url

    driver.get("https://www.selenium.dev/documentation/")
    assert "documentation" in driver.current_url

    driver.back()
    assert driver.current_url == first_url

    driver.forward()
    assert "documentation" in driver.current_url

    driver.refresh()

Selenium Beginner Writing Your First Test

Written by PV

© 2026 All Rights Reserved