To get started with Java test automation, choose what you need to verify: use JUnit for Java behavior that does not require a browser, and add Selenium WebDriver when you need to test browser interactions. For a first browser test, put Selenium and JUnit dependencies in a Maven or Gradle project, open a page, perform one action, assert a visible result, and close the browser session even if the test fails.
Choose the right kind of Java test
“Java test automation” can mean tests at several layers. Unit tests check Java code directly; service or API tests exercise application interfaces; browser UI tests verify behavior through a browser. Selenium’s getting-started material provides an end-to-end browser example, so this guide uses that as a concrete first project. It is not a reason to use Selenium for every test.
- Use JUnit without Selenium when the behavior can be checked by calling Java code directly.
- Use Selenium WebDriver when the behavior depends on browser navigation, rendered controls, or user interactions.
- Use a service/API test approach when the target is an API rather than a browser. The Selenium examples below are not API-test examples.
The Selenium project describes WebDriver as the means to automate major browsers. A browser test involves distinct pieces: Java bindings in your project, a browser, and a browser-specific driver implementation that communicates with it. See Selenium’s Java installation and setup guide for current requirements and setup details.
Choose a build tool and test framework
Maven and Gradle are both documented options; use the one already adopted by your project when possible. If you are starting from scratch, team familiarity and existing conventions are better criteria than assuming one tool is universally best. IntelliJ IDEA’s Selenium project workflow likewise offers Maven or Gradle and JUnit or TestNG choices: IntelliJ IDEA Selenium documentation.
#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
| Choice | What it does | When it fits |
|---|---|---|
| Maven | Manages project dependencies and test execution through the project descriptor and Maven lifecycle. | Use it when the repository or team already uses Maven. Selenium’s Java installation guide shows a org.seleniumhq.selenium:selenium-java dependency in pom.xml. |
| Gradle | Provides JVM projects a test source set and a standard test task. |
Use it when the repository or team already uses Gradle; its current guide documents JUnit and TestNG integration and JUnit Platform configuration. |
| JUnit Jupiter | The modern JUnit programming and extension model for authoring tests. | A reasonable choice for a new JUnit example, including the Selenium sample used here. |
| JUnit Vintage | Runs JUnit 3 and JUnit 4 tests on the JUnit Platform. | Useful for legacy test compatibility, not needed just to write a new Jupiter test. |
| TestNG | An alternative test framework integrated by Gradle and offered in IntelliJ’s project setup. | Choose it when the project or team already uses it. Avoid adding two frameworks without a concrete need. |
JUnit 5 is a family of components: the JUnit Platform provides launching and engine infrastructure, Jupiter provides the current programming model, and Vintage supports older JUnit 3/4 tests on the Platform. The JUnit 5 User Guide explains those distinctions.
Create a first browser test with Maven
The following is a compact Maven project example using JUnit Jupiter and Selenium’s Java bindings. The dependency versions are illustrative configuration, not a claim about current minimums or the latest releases. Before pinning versions, check Selenium’s current installation guidance and the JUnit guide, then keep versions aligned with your project’s Java and browser setup.
1. Add dependencies in pom.xml
Selenium documents the Maven artifact name org.seleniumhq.selenium:selenium-java. The example below uses Maven Surefire to run JUnit Platform tests; align plugin and library versions with the versions supported by your project.
Rank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>example</groupId>
<artifactId>selenium-first-test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.10.4</junit.version>
<selenium.version>4.34.0</selenium.version>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.3</version>
</plugin>
</plugins>
</build>
</project>
The Java release value above is an example compiler setting, not a universal Selenium minimum. Consult Selenium’s language requirements before selecting a Java version.
2. Write one scenario
Save this class under src/test/java/example/FirstSearchTest.java. It opens a public demo search page, enters a query, submits it, and checks the resulting heading. The selectors and expected text belong to that demo page; for your application, choose stable selectors and an outcome your product guarantees.
package example;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.jupiter.api.Assertions.assertTrue;
class FirstSearchTest {
private WebDriver driver;
@BeforeEach
void setUp() {
driver = new ChromeDriver();
}
@Test
void searchDisplaysResultsHeading() {
driver.get("https://www.selenium.dev/selenium/web/web-form.html");
WebElement input = driver.findElement(By.name("my-text"));
input.sendKeys("Selenium");
driver.findElement(By.cssSelector("button")).click();
String heading = driver.findElement(By.id("message")).getText();
assertTrue(heading.contains("Received!"));
}
@AfterEach
void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
The lifecycle is deliberate: setup creates the driver, the test navigates and interacts, and teardown calls quit(). Using a cleanup hook prevents browser sessions from being left open when an assertion fails. Selenium’s official first-script walkthrough and test-organization example show this general flow: first Selenium script and organizing and executing Selenium code.
Rank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
3. Run it
From the project root, run:
mvn test
Run the same test from your IDE while authoring, but use the build-tool command to make the test reproducible for other developers and CI. Start with a single local test; remote Grid execution and parallel runs are later scaling choices, not prerequisites.
Gradle alternative
If the project uses Gradle, put tests in src/test/java, declare the Selenium and JUnit dependencies in the Gradle build file, and configure JUnit Platform. In Groovy DSL, the relevant structure is:
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.seleniumhq.selenium:selenium-java:SELENIUM_VERSION'
testImplementation 'org.junit.jupiter:junit-jupiter:JUNIT_VERSION'
}
test {
useJUnitPlatform()
}
Replace the version labels with versions selected from the current project documentation; they are explanatory labels, not dependency versions. The test class itself can be the same Java class shown above. Run it with:
Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
./gradlew test
For a system Gradle installation, the corresponding command is gradle test. Gradle’s current guide describes the JVM test source set, standard task, and framework integration: Testing in Java & JVM projects. Prefer this standard test task and JUnit Platform integration over outdated tutorials that add a separate JUnit Gradle plugin without a project-specific reason.
Keep browser checks focused and stable
- Test browser-dependent behavior. Keep pure calculations and business rules in faster, simpler Java tests when no browser is needed.
- Assert a meaningful result. A test should verify a visible state or outcome, not merely that navigation occurred.
- Use stable locators. Prefer application-owned IDs, names, or deliberate test attributes over fragile positional selectors.
- Always quit the driver. Put session cleanup in teardown so test failures do not skip it.
- Keep the first run local and small. Add remote browsers, parallelism, or Grid only after a single test is reliable.
Troubleshoot a first Selenium test
The browser does not start
Confirm that the browser you intend to automate is installed and that your selected Selenium setup supports the project’s Java and browser environment. WebDriver uses a browser-specific implementation; consult the current Selenium installation page rather than assuming the Java dependency alone supplies every part of the setup.
The test compiles but the framework reports no tests
Check that the class is under src/test/java, that test dependencies have test scope/configuration, and that JUnit Platform is enabled for Gradle. For Maven, verify that the test runner configuration supports JUnit Jupiter. Compare the project setup with the official Gradle testing guide or Selenium’s Maven first-test walkthrough.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallBest Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
A locator cannot be found
Check that the page loaded the expected content and that the locator matches the current page markup. A CSS selector or element name from a sample page is not automatically valid for your application. If the element appears asynchronously, use an explicit wait for the relevant condition rather than relying on an arbitrary pause.
The browser remains open after failure
Ensure teardown is an annotated lifecycle method and calls driver.quit() after checking the driver reference. Cleanup should not depend on the assertion passing.
A tutorial suggests an old Gradle plugin or a fixed Java minimum
Use the current Gradle test task and JUnit Platform configuration documented by Gradle. Do not infer Selenium’s universal Java minimum from a sample compiler setting; its installation material links to separate language requirements that can change.
Or skip the browser setup
If your task is to capture a website screenshot rather than verify interactive behavior with a test assertion, ScreenshotNeo offers a one-request screenshot API and an MCP server for AI agents. A screenshot is not a replacement for a Selenium interaction test, but it can avoid setting up a browser automation project just to capture a page.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →For API details, see the ScreenshotNeo documentation.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
ScreenshotNeo accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients. The free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.
Sign up for ScreenshotNeo’s free 1,000 screenshots a month, with no card required.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Recommended Free Tools

