For AI agents and LLMs: a machine-readable index is available at llms.txt. A plain-Markdown version of any documentation page is available by appending .md to its URL.
Skip to main content

How to Run Selenium Tests With Java on TestMu AI


Run your Java Selenium tests on the TestMu AI cloud grid across 10,000+ browser/device combinations. The setup is the same for every framework: you connect to the grid, pass your capabilities, and run. This guide walks through that shared flow once, then gives you a per-framework quickstart for TestNG, JUnit, Cucumber, Selenide, Gauge, Geb, and Serenity, plus a zero-code Java SDK option.

Prerequisites​


Before running any framework below, set up a TestMu AI account, your credentials, the JDK, and Maven.

  1. Create a TestMu AI account if you don't have one.
  2. Get your Username and Access Key from the TestMu AI Dashboard.
  3. Install the Java Development Kit (JDK) 11 or later.
  4. Install Apache Maven. The framework sample projects below all build with Maven.

Set Up Manually Without Maven (Optional)​

If you prefer to run raw Selenium Java without a build tool, add the Selenium JARs to your IDE instead of using Maven.

  1. Download the latest Selenium Java Client and extract the ZIP file to your project directory.
  2. Add the Selenium JARs to your project dependencies in your IDE.
IntelliJ project settings

Navigate to Dependencies in module settings, click +, and add the downloaded Selenium JARs.

Selenium JARs added to project dependencies

With the JARs added, compile and run the test directly from the terminal:

Verified
cd to/file/location
javac -classpath ".:/path/to/selenium/jarfile:" JavaTodo.java
java -classpath ".:/path/to/selenium/jarfile:" JavaTodo

Set Your Credentials​


Every framework authenticates the same way: your Username and Access Key are passed in the grid URL. Set them as environment variables so you don't hard-code them. Pick your operating system:

Verified
export LT_USERNAME="undefined"
export LT_ACCESS_KEY="undefined"

How the Sample Test Works​


All the framework repos below run the same sample test, so you only need to understand it once. The test opens the to-do app, marks the first two items done, adds a new item, and verifies it appears:

Verified
driver.get("https://lambdatest.github.io/sample-todo-app/");
driver.findElement(By.name("li1")).click();
driver.findElement(By.name("li2")).click();
driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list");
driver.findElement(By.id("addbutton")).click();

The driver is a RemoteWebDriver pointed at the grid, with your browser/OS choices passed through LT:Options:

Verified
ChromeOptions browserOptions = new ChromeOptions();
browserOptions.setPlatformName("Windows 10");
browserOptions.setBrowserVersion("latest");

HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("build", "Java Selenium Build");
ltOptions.put("name", "Java Selenium Test");
ltOptions.put("w3c", true);
browserOptions.setCapability("LT:Options", ltOptions);

driver = new RemoteWebDriver(
new URL("https://" + username + ":" + accesskey + "@hub.lambdatest.com/wd/hub"),
browserOptions);
tip

Use the Capabilities Generator to build an LT:Options block for any browser, version, and OS combination.

What changes between frameworks is only how that test is structured and run: the runner, its setup/teardown hooks, and any config files. That's what each tab below covers.

Run Selenium Tests With Java SDK​


The Java SDK is the zero-code option: it runs your existing TestNG tests on the grid unchanged, injecting credentials and capabilities from a YAML file via a Java agent. Use this when you already have a local suite and don't want to edit test code. See the SDK reference project on GitHub.

  1. Add the SDK dependency and agent to your pom.xml (the agent is attached to Surefire via -javaagent):
Verified
pom.xml (key additions)
<dependency>
<groupId>io.github.lambdatest</groupId>
<artifactId>lambdatest-selenium-java-sdk</artifactId>
<version>1.0.1</version>
</dependency>
<!-- plus the maven-dependency-plugin (copies lambdatest-agent.jar) and
maven-surefire-plugin with -javaagent:.../lambdatest-agent.jar -->
  1. Create lambdatest.yml with your credentials, platforms, and features. The SDK loads it automatically:
Verified
lambdatest.yml
username: YOUR_LAMBDATEST_USERNAME
accesskey: YOUR_LAMBDATEST_ACCESS_KEY

platforms:
- browserName: Chrome
browserVersion: latest
platformName: Windows 10

build: SDK Build v1
name: SDK Test
video: true
tunnel: true
  1. Register the SDK's TestNG listener in testng.xml, then run. Your existing test classes execute as-is:
Verified
mvn test -D suite=testng.xml

The test then appears on the Automation Dashboard. A green status confirms it passed.

Run a Test in Your Framework​


Each tab lists just the framework-specific pieces. Clone the matching repo (it contains the full, ready-to-run project), then run the command shown.

TestNG is the most common choice. It wraps the shared test with @BeforeClass (create the driver) and @AfterClass (report status and quit), and drives cross-browser runs from a testng.xml suite.

  1. Clone the sample GitHub project:
Verified
git clone https://github.com/LambdaTest/Java-TestNG-Selenium
cd Java-TestNG-Selenium
  1. Set your browser and OS in the testng.xml suite. Listing several environments with parallel="tests" and a thread-count runs them concurrently:
Verified
testng.xml
<suite thread-count="3" name="LambdaTestSuite" parallel="tests">
<test name="WIN10-Chrome">
<parameter name="browser" value="chrome"/>
<parameter name="platform" value="Windows 10"/>
<classes><class name="LambdaTest.TestNGToDo"/></classes>
</test>
<test name="MAC-Safari">
<parameter name="browser" value="safari"/>
<parameter name="platform" value="macOS Catalina"/>
<classes><class name="LambdaTest.TestNGToDo"/></classes>
</test>
</suite>
  1. Run a single test, or the parallel suite:
Verified
mvn test -D suite=single.xml
mvn test -D suite=parallel.xml

The test then appears on the Automation Dashboard. A green status confirms it passed.

View Your Results​


Your test results, including video, network logs, and command-by-command execution, appear on the TestMu AI Automation Dashboard.

Next steps: If this is your first run, walk through running your first Selenium test end to end. From there, explore the full set of Selenium automation capabilities you can pass to the grid, learn how to debug your Selenium tests, and organize and filter your Selenium tests as your suite grows.

Next Steps​


Continue with these related guides:

Terminal First Testing With Kane CLI

Natural language browser & mobile app tests right from terminal.

×
Schedule Your Personal Demo
Kane CLI terminal

Help and Support

Related Articles