October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk7 min

Java Test Automation: Getting Started with a First Selenium Test

Learn how Java test automation fits unit, API, and browser testing, then set up a reproducible Selenium WebDriver test with Maven or Gradle and JUnit Jupiter.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Start with one Java test that runs from your build tool and, when the behavior depends on a browser, opens a browser, checks a visible result, and closes the session. Java test automation can mean unit tests, service or API tests, or browser UI tests; Selenium is for the browser layer, not a replacement for a test framework or build tool.

Choose the layer you need to test

  • Unit tests: Check Java logic without starting a browser. Use a test framework such as JUnit Jupiter.
  • Service or API tests: Exercise application interfaces without driving a browser. Choose an appropriate Java testing library for the service and keep the test in your project’s build.
  • Browser UI tests: Use Selenium WebDriver when the behavior depends on browser interactions, such as entering data into a form and verifying the page response.

Browser tests require additional setup because WebDriver controls a browser through a browser-specific driver implementation. Selenium’s documentation says, “Selenium supports automation of all the major browsers in the market through the use of WebDriver.” Selenium’s installation guide explains the Java bindings and setup. You do not need Selenium for every Java test.

Understand the parts of a Java browser-test stack

  • Build tool: Maven or Gradle declares dependencies and runs tests, helping make execution reproducible from a command line or CI.
  • Test framework: JUnit or TestNG organizes test cases, lifecycle hooks, and assertions.
  • Selenium Java bindings: The library your Java test uses to issue WebDriver commands.
  • Browser and driver implementation: The browser you want to exercise and its WebDriver implementation. Check Selenium’s current setup and language requirements when selecting versions.

Selenium controls the browser; it does not itself supply the test runner. JUnit or TestNG provides test organization and assertions, while Maven or Gradle manages and runs the project’s tests. For a new JUnit example, Jupiter is the modern programming and extension model. JUnit Platform is its launch and engine infrastructure; Vintage supports running JUnit 3 and 4 tests on that platform. See the JUnit 5 User Guide.

Create a project using the build system your team has

Maven and Gradle are both valid choices. Match an existing project’s conventions when possible; for a new project, choose the build tool your team can maintain. IntelliJ IDEA’s Selenium workflow also presents Maven or Gradle and JUnit or TestNG choices, rather than prescribing one universal stack. IntelliJ IDEA’s Selenium documentation describes those setup options.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • 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.

Maven

Add Selenium Java and a test framework as dependencies in pom.xml. Selenium’s Java installation guide uses the org.seleniumhq.selenium:selenium-java artifact. Use the current version guidance on the official page rather than copying an old version number or treating an example compiler setting as a universal Java minimum.

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>REPLACE_WITH_CURRENT_SELENIUM_VERSION</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>REPLACE_WITH_CURRENT_JUNIT_VERSION</version>
        <scope>test</scope>
    </dependency>
</dependencies>

The version labels above are instructions to select versions, not literal Maven versions. Consult the Selenium dependency setup and JUnit guide before pinning them. Use the Maven test command documented for the current Selenium example and project configuration; confirm the project’s Maven test setup if the command does not discover tests.

Gradle

Gradle’s Java testing support provides the test source set and task. Put tests in src/test/java, declare test dependencies in the build, and configure JUnit Platform when using JUnit Jupiter. The current guide documents both JUnit and TestNG integration; use one framework unless the project has a specific reason to combine them.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 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.
dependencies {
    testImplementation 'org.seleniumhq.selenium:selenium-java:REPLACE_WITH_CURRENT_SELENIUM_VERSION'
    testImplementation 'org.junit.jupiter:junit-jupiter:REPLACE_WITH_CURRENT_JUNIT_VERSION'
}

test {
    useJUnitPlatform()
}

Replace the version labels with current versions, then run gradle test (or the project’s wrapper command, if it provides one). For configuration details, see Gradle’s Java testing guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Write and run one browser test

This JUnit Jupiter example uses Selenium’s public web form demo. It opens the page, enters text, submits the form, checks the resulting heading, and quits the browser after each test. Place the class under src/test/java in a Gradle Java project configured as above. It assumes the selected browser is installed and Selenium can obtain or use the matching WebDriver implementation under the current installation setup.

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import static org.junit.jupiter.api.Assertions.assertEquals;

class FirstBrowserTest {
    private WebDriver driver;

    @Test
    void submitsTheExampleForm() {
        driver = new ChromeDriver(new ChromeOptions());
        driver.get("https://www.selenium.dev/selenium/web/web-form.html");

        driver.findElement(By.name("my-text")).sendKeys("Java test");
        driver.findElement(By.cssSelector("button")).click();

        String heading = driver.findElement(By.tagName("h1")).getText();
        assertEquals(" form submission ".trim(), heading);
    }

    @AfterEach
    void closeBrowser() {
        if (driver != null) {
            driver.quit();
        }
    }
}

Run it using gradle test or the project’s Gradle wrapper. In an IDE, run the test method or class after importing the project and its dependencies. Selenium’s first-script walkthrough, last modified July 23, 2026, and test organization guide show the same core lifecycle: create a driver, navigate, interact, assert, and call quit() during teardown. The sample uses Chrome as an example; choose another supported browser by using its corresponding WebDriver setup.

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • 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.

What the example demonstrates

  • driver.get(...) navigates to the page under test.
  • findElement(...) locates elements using a name, CSS selector, or tag name.
  • sendKeys(...) and click() perform browser interactions.
  • A JUnit assertion checks an observable page outcome instead of merely checking that no exception occurred.
  • @AfterEach and driver.quit() close the session even when the test fails.

Keep the first test reliable and appropriately small

  • Use browser automation only for behavior that needs a browser; verify ordinary Java logic with faster, simpler tests that do not start one.
  • Keep dependencies in Maven or Gradle files so another developer or CI can reproduce the setup.
  • Use stable locators tied to the page’s intended controls. If a selector stops matching after a page change, update the locator to reflect the current markup.
  • Always quit the driver in a teardown hook. Otherwise, a failed test can leave a browser session running.
  • Run locally and from the build tool before introducing parallel runs or a remote Selenium Grid. Selenium presents Grid as a scale-up path, not a prerequisite for a first test.

Troubleshoot common first-run failures

The test is not discovered or does not run

Check that the test is in the build tool’s test source set, that the chosen framework dependency is declared with the correct test scope or configuration, and that the build is configured for the framework. For Gradle with Jupiter, verify useJUnitPlatform() and run gradle test. Avoid obsolete plugin tutorials when the current Gradle guide documents the standard test task.

The browser does not start

Confirm the requested browser is installed and that the Selenium and browser-driver setup follows the current Selenium installation guidance. WebDriver uses a browser-specific implementation, so a Java dependency alone does not mean every browser is ready to launch. Check current language requirements as well; do not infer a minimum Java version from compiler settings in a sample project.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

An element cannot be found or the assertion fails

Check that navigation reached the expected page, then verify the locator and expected visible result against the page’s current markup. A selector valid for a previous page version may no longer identify the intended element. Keep the initial test on a stable demo page while validating the project setup.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • 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

A browser remains open after a failure

Ensure the driver is stored where the teardown hook can access it and that the hook calls quit() when it is non-null. Use quit() to close the WebDriver session rather than omitting cleanup after assertions.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If the task is to capture a page image or PDF rather than test interactive application behavior, ScreenshotNeo is a website screenshot API and MCP server. One GET request returns a PNG, JPEG, WebP, or PDF. For example, this cURL call saves a WebP screenshot of the target URL:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for parameters and response details. Cookie banners are accepted and removed along with known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses include X-Page-Verdict and X-Billed headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots. This captures pages; it does not replace Selenium tests that must interact with an application and assert behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Sign up for ScreenshotNeo’s free plan: 1,000 screenshots a month, no card required.

Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 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.

Frequently Asked Questions

Should I use JUnit or TestNG for a new Java test project?

Either is a supported choice in the documented Gradle and IntelliJ workflows. Pick the framework that fits the project’s conventions; the example here uses JUnit Jupiter.

Do I need Selenium to automate every Java test?

No. Selenium is appropriate when a test needs to drive a browser. Java logic and many service-level checks can be tested without browser automation.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.