Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

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
June 17, 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.
Overview
Why Use Selenium With Java
Selenium is an open-source framework that controls browsers through code. Teams choose Java as the pairing language for its static typing, Maven build ecosystem, and broad library support, all of which keep large test suites maintainable over time.
How Do You Run Selenium Tests With Java?
Getting a Selenium Java test running takes five steps, from project setup to reviewing results.
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.
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 io.github.bonigarcia.wdm.WebDriverManager;
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() throws Exception {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
@Test
public void firstTestCase() {
try {
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());
} catch (Exception e) {
}
}
@AfterTest
public void closeBrowser() {
driver.close();
System.out.println("The driver has been closed.");
}
}Dependencies come from a pom.xml declaring Selenium, TestNG, and WebDriverManager, 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="WIN10TEST">
<parameter name="browser" value="chrome"/>
<parameter name="version" value="121.0"/>
<parameter name="platform" value="WIN10"/>
<classes>
<class name="FirstTestScriptUsingWebDriver"/>
</classes>
</test>
<test name="MACTEST">
<parameter name="browser" value="safari"/>
<parameter name="version" value="11.0"/>
<parameter name="platform" value="macos 10.13"/>
<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 10");
browserOptions.setBrowserVersion("121.0");
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.
Did you find this page helpful?
More Related Blogs
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance