Lesson 1 of 8 · 10 min
What Selenium is and setting up your first project
Selenium is a free, open-source tool that controls a real web browser from code. Your test opens Chrome, clicks buttons, types text and reads what is on the page, exactly as a person would. Testers use it to check a website automatically instead of repeating the same steps by hand after every release.
The three parts of Selenium
- Selenium WebDriver: the part you will use most. It is a set of Java classes that send commands to a browser through a small program called a driver (for example chromedriver for Chrome).
- Selenium Grid: runs your tests on many machines and browsers at the same time. Teams use it to save time and to test different browsers.
- Selenium IDE: a browser extension that records your clicks and plays them back. It is good for quick experiments, but real projects use WebDriver code.
This course teaches WebDriver with Java, and TestNG as the test framework. TestNG runs your test methods and tells you which passed or failed. Maven downloads the libraries and runs the build.
Create a Maven project
You need a JDK (17 or 21 is a good choice), Maven, and Google Chrome. Create a Maven project in your IDE, then add two dependencies to pom.xml. Check Maven Central for the latest versions; these were tested together.
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.52.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>The scope test means TestNG is only used for tests and is not packaged with the main code. Test classes go in src/test/java. Run them with mvn test.
Selenium Manager handles the driver
In the past you had to download chromedriver yourself, match it to your Chrome version, and set a system property. From Selenium 4.6, a tool called Selenium Manager is built in. When you write new ChromeDriver(), it finds your browser version, downloads the matching driver, and keeps it in a cache folder on your computer. You do not install anything extra.
If you do set the path yourself with System.setProperty("webdriver.chrome.driver", path), Selenium uses your driver. Selenium Manager only works when no driver is given.
Open a browser and navigate
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.get("https://example.com");
System.out.println("Title: " + driver.getTitle());
driver.navigate().to("https://example.org");
driver.navigate().back();
System.out.println("Now at: " + driver.getCurrentUrl());
} finally {
driver.quit();
}
}
}Output:
Title: Example Domain
Now at: https://example.com/driver.get(url) opens a page and waits until it has loaded. driver.navigate() gives you to(), back(), forward() and refresh(), like the browser buttons. Declare the variable as WebDriver, the interface, and create a ChromeDriver object. Then you can change to another browser later with very little change.
close() and quit()
driver.close() closes only the current window or tab. driver.quit() ends the whole session: every window, the browser and the driver process. Always call quit() at the end, in a finally block or a TestNG @AfterMethod. If you forget, browser processes keep running in the background.
Try it yourself
- Create a Maven project, add both dependencies, and run mvn test-compile to confirm they download.
- Write the FirstTest class above, but open a website of your choice and print its title.
- Add driver.navigate().refresh() and print the current URL before and after.
- Remove the quit() call, run twice, and look in your task manager for leftover browser processes.