Selenium First Test Script
A first test script turns theory into a working result. You write about fifteen lines, run them, and watch a browser perform actions on its own. Think of the script as a recipe card. Each line is one instruction, and Selenium plays the role of a kitchen assistant who follows the card exactly.
Test Plan in Plain Words
The script visits a practice login page, types a username and password, clicks the login button, and verifies the welcome message. Every automated test follows the same shape: arrange, act, and verify.
ARRANGE ACT VERIFY +---------+ +--------------------+ +------------------+ | Open |-->| Type name |-->| Read welcome | | browser | | Type password | | message and | | and URL | | Click Login | | compare text | +---------+ +--------------------+ +------------------+
Complete Script
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://the-internet.herokuapp.com/login");
driver.findElement(By.id("username")).sendKeys("tomsmith");
driver.findElement(By.id("password")).sendKeys("SuperSecretPassword!");
driver.findElement(By.cssSelector("button.radius")).click();
String message = driver.findElement(By.id("flash")).getText();
if (message.contains("You logged into a secure area")) {
System.out.println("PASS: login worked");
} else {
System.out.println("FAIL: unexpected message " + message);
}
driver.quit();
}
}Line-by-Line Meaning
Imports
The three import lines bring in tools. By describes how to find an element, WebDriver is the remote control, and ChromeDriver is the Chrome-specific remote.
Starting the Browser
new ChromeDriver() opens a fresh Chrome window. Selenium Manager downloads a matching driver automatically in Selenium 4.6 and later. No manual driver setup is needed.
Opening the Page
driver.get() loads the address and waits until the page finishes loading.
Finding and Typing
findElement returns one element. sendKeys types text into it, just as a person would use a keyboard.
Reading the Result
getText() returns the visible words of an element. The script compares those words with the expected message.
Closing
quit() shuts every window and ends the session. Always include this line.
What You See on Screen
Console output: PASS: login worked Browser window: opens -> types -> clicks -> closes Total time: about 5 seconds
Making the Script Safer
Wrap the body in a try block and call quit() inside finally. The browser then closes even when a line fails halfway. A stuck browser window wastes memory and confuses later runs.
WebDriver driver = new ChromeDriver();
try {
driver.get("https://the-internet.herokuapp.com/login");
// test steps go here
} finally {
driver.quit();
}Practice Ideas
- Change the password to a wrong value and confirm the script reports FAIL.
- Print the page title with
driver.getTitle(). - Run the script three times and note the time each run takes.
How the Script Behaves Under the Hood
Every line sends a request through the driver to the browser. The browser answers after it finishes the action. Selenium waits for that answer before it moves to the next line, which keeps the script in step with the page. Page loading counts as one action,. get() returns only after the browser reports a loaded page.
Why Locators Matter Here
The script uses two locator types: id and CSS selector. Ids give the fastest and most stable match. A CSS selector works when no id exists. Choose the simplest locator that stays unique on the page.
Why Verification Matters
A test without a check only proves that the browser did not crash. The comparison step confirms that the application behaved correctly. Real projects replace the if statement with an assertion from TestNG or JUnit, which stops the test and records a clear failure message.
Checklist Before You Run
- Install a recent JDK and add the Selenium library to the project.
- Confirm that Chrome is installed and updated.
- Check the internet connection. The practice page lives online.
- Close old browser windows opened by earlier runs.
Reading Errors Calmly
Error messages look scary and carry clear hints. NoSuchElementException names the locator that failed. SessionNotCreatedException points to a browser and driver mismatch. The first line of the message usually states the cause, and the lines below list the code path. Read the top line, compare the locator with the page, and fix one thing at a time.
