World’s largest virtual agentic engineering & quality conference
A practical guide to Selenium with Java covering environment setup, Maven configuration, TestNG test structure, the Page Object Model, and parallel cloud execution.

Salman Khan
Author
Srinivasan Sekar
Reviewer
Published on: August 1, 2025
Last Updated on: June 14, 2026
Selenium with Java is the most widely used pairing in UI test automation. Java contributes static typing, mature build tooling, and a deep ecosystem, while Selenium WebDriver handles cross-browser control.
Together they support suites that scale to thousands of cases and survive years of refactoring. This guide follows one example end to end: set up the environment, write a test, walk through the code, then scale it to parallel cloud execution.
TL;DR
To automate browser testing, use Selenium with Java to build maintainable, statically typed test suites, and run them in parallel on TestMu AI's cloud infrastructure. This combination allows teams to programmatically control browsers through code and execute scalable tests across various browser and operating system combinations.
Why Use Selenium With Java
How Do You Run Selenium Tests With Java?
Teams use Selenium with Java because Java's static typing, mature Maven build tools, and large ecosystem make test suites scalable and maintainable over years of continuous UI changes.
Selenium is the open-source framework automating the browser; Java is the language enterprise teams most often pair it with.
Three integrations shape how the suite is structured, executed, and scaled:
To set up Selenium with Java, install the JDK, pick an IDE such as Eclipse or IntelliJ, add the Selenium client library through Maven, and configure browser drivers automatically with Selenium Manager.
Getting these right up front avoids the classpath and driver-mismatch errors behind most failed first runs.
Once the JDK is installed, set the environment variables so the toolchain can resolve Java:
Confirm with java -version in a terminal. If the version printed does not match the JDK you intended, your PATH is resolving an older install first, which is worth fixing now rather than debugging later.
Note: Run Selenium Java tests across 10,000+ browser and OS combinations without managing infrastructure. Try TestMu AI Today!
To create a Selenium Java project in Eclipse, open a new Java project, add a package and class, then add the Selenium JARs to the build path. The steps below walk through it; IntelliJ follows the same flow.
That manual JAR path is fine to see once, but in any real project use Maven instead. A pom.xml declares dependencies, pins versions so every machine and CI runner builds identically, and ends the "works on my machine" class of failures.
This is the whole dependency block you need. Selenium 4.46.0 and TestNG 7.12.0 are the current releases on Maven Central as of August 2026:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.46.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>Two details save time later. Pin exact versions rather than using a range, because a silent minor bump is a miserable thing to debug when a suite starts failing overnight. And wire testng.xml into the Surefire plugin now, since that is what lets mvn test run the whole suite in CI instead of one class at a time.
Notice there is no driver-manager dependency in that block. Selenium Manager has shipped with every Selenium release since 4.6 and acts as a fallback: when no driver is supplied on the PATH or through a system property, it resolves and downloads the matching one for you. The Selenium project still labels it beta, so keep manual driver management as an option if your CI runners are locked down, but for most projects the extra dependency is no longer needed.
Skip the setup and install the Selenium Skill for Claude Code, Copilot & Cursor with one command.
To write and run a Selenium Java test, create a TestNG class that initializes WebDriver, define a test method with your browser actions, and run it from the IDE or with mvn test.
The example below is intentionally minimal so the WebDriver mechanics stay visible: launch Chrome, open Selenium Playground, verify the H1, then close the browser.
package TestMuAI;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class FirstTestScriptUsingWebDriver {
public static WebDriver driver = null;
@BeforeTest
public void setUp() {
driver = new ChromeDriver();
}
@Test
public void firstTestCase() {
driver.get("https://www.testmuai.com/selenium-playground/");
WebElement pageH1 = driver.findElement(By.tagName("h1"));
Assert.assertEquals(pageH1.getText(), "Selenium Playground");
System.out.println("H1 verified: " + pageH1.getText());
}
@AfterTest
public void closeBrowser() {
driver.quit();
System.out.println("The driver has been closed.");
}
}Dependencies come from the pom.xml shown earlier, declaring Selenium and TestNG, with the compiler set to Java 17. A minimal testng.xml registers the class so the suite runs. Each step prints to the console on execution.
Code Walkthrough:
Run the test locally from the project root:
mvn test -Dtest=FirstTestScriptUsingWebDriverTo run parallel Selenium Java tests on the cloud, add browser and platform parameters to testng.xml, point the driver at the cloud grid hub, and pass your authentication capabilities including username, access key, and browser version.
A local run only validates one browser on one machine, and past a few parallel threads the local machine becomes the bottleneck. A cloud grid removes that ceiling, provided your driver is thread-safe.
TestMu AI (Formerly LambdaTest) Automation Cloud is a cloud-based automated browser testing infrastructure that runs Selenium Java tests across different browser and OS combinations, with parallel execution to cut total runtime.
Features:
For the full configuration reference, see how to run Selenium Java tests on TestMu AI.
The test logic stays the same; you parameterize the environment in testng.xml:
<test name="WIN11TEST">
<parameter name="browser" value="chrome"/>
<parameter name="version" value="latest"/>
<parameter name="platform" value="Windows 11"/>
<classes>
<class name="FirstTestScriptUsingWebDriver"/>
</classes>
</test>
<test name="MACTEST">
<parameter name="browser" value="safari"/>
<parameter name="version" value="latest"/>
<parameter name="platform" value="macOS Sequoia"/>
<classes>
<class name="FirstTestScriptUsingWebDriver"/>
</classes>
</test>This configuration does the following:
Then point the driver at the grid hub and pass your capabilities:
ChromeOptions browserOptions = new ChromeOptions();
browserOptions.setPlatformName("Windows 11");
browserOptions.setBrowserVersion("latest");
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("username", "YOUR_LT_USERNAME");
ltOptions.put("accessKey", "YOUR_LT_ACCESS_KEY");
ltOptions.put("project", "Selenium with Java");
ltOptions.put("w3c", true);
ltOptions.put("plugin", "java-testNG");
browserOptions.setCapability("LT:Options", ltOptions);The capabilities block configures the cloud session:
Keep the username and access key in environment variables, never hardcoded, since committed credentials in a test repo are a routine security finding. Run with mvn test -D suite=parallel.xml, then review results in the Web Automation dashboard.
Advanced Selenium Java use cases include automating registration forms, handling authentication pop-ups, managing CAPTCHAs, uploading and downloading files, and handling cookies.
Each becomes a source of flaky test failures once you move past a basic happy-path script.
DOM refreshes mid-interaction also cause handling stale element exceptions in Selenium Java to become a routine part of form test maintenance.
The core Selenium Java best practices are designing test cases before scripting, using the Page Object Model, choosing stable locators, adding logging and reporting, and using explicit waits instead of Thread.sleep().
Apply them from the first test, since retrofitting across hundreds of cases is significantly more costly.
Selenium with Java is still the default for durable UI automation testing, and the path from first script to scaled coverage is short if you avoid the traps along the way.
You set up the environment, wrote a working WebDriver test, saw exactly where that sample code would fail in production, and scaled the corrected approach to parallel execution across browsers with only configuration changes.
The next steps that separate a maintainable suite from a fragile one are consistent: a thread-safe driver, the Page Object Model across the suite, stable locators, explicit waits over sleeps, and a cloud grid so coverage grows without local overhead. Build on those and the suite keeps paying off long after the first test.
Author
Salman is a Test Automation Evangelist and Community Contributor at TestMu AI, with over 6 years of hands-on experience in software testing and automation. He has completed his Master of Technology in Computer Science and Engineering, demonstrating strong technical expertise in software development, testing, AI agents and LLMs. He is certified in KaneAI, Automation Testing, Selenium, Cypress, Playwright, and Appium, with deep experience in CI/CD pipelines, cross-browser testing, AI in testing, and mobile automation. Salman works closely with engineering teams to convert complex testing concepts into actionable, developer-first content. Salman has authored 120+ technical tutorials, guides, and documentation on test automation, web development, and related domains, making him a strong voice in the QA and testing community.
Reviewer
Srinivasan Sekar is Director of Engineering at TestMu AI (formerly LambdaTest), where he leads engineering and open-source initiatives behind the Selenium and Appium automation grid and owns TestMu AI's MCP Server. A committer to Appium and a contributor to Selenium, WebdriverIO, Taiko, and AppiumTestDistribution, he brings over 15 years of experience in quality engineering and open-source technologies. He is the author of the Apress book 'The MCP Standard: A Developer's Guide to Building Universal AI Tools with the Model Context Protocol,' a Certified Kubernetes and Cloud Native Associate, and an international conference speaker. Before TestMu AI he spent over eight years at Thoughtworks as a Principal Consultant and Quality Architect. Srinivasan holds a B.Tech in Information Technology from Anna University.
Did you find this page helpful?
More Related Blogs
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance