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

Use Selenium to perform a browser action, wait until the page reaches the state you expect, then use a TestNG-compatible assertion to compare the observed result with that expectation. For example, after submitting a form, wait for a confirmation message and assert its text. TestNG records an assertion failure as a failed test method.

How Selenium and TestNG work together

Selenium drives the browser and lets your test inspect observable page state, such as a title or visible text. TestNG runs the Java test and reports whether it passed or failed. Selenium lists TestNG among Java test-runner options; JUnit is another option. TestNG also supports parallel execution and parameterized tests. Selenium: Organizing and Executing Selenium Code

Assert a browser result with TestNG

A useful assertion checks the result of the action, not merely that the action ran. The example below submits a form, waits for its confirmation message to become visible, and then compares its text with the expected value.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

import java.time.Duration;

import static org.junit.Assert.assertEquals;

public class SignupTest {
    @Test
    public void submittingFormShowsConfirmation() {
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com/signup");
            driver.findElement(By.id("email")).sendKeys("reader@example.com");
            driver.findElement(By.cssSelector("button[type='submit']")).click();

            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
            String confirmation = wait.until(
                ExpectedConditions.visibilityOfElementLocated(By.id("confirmation"))
            ).getText();

            assertEquals("Expected confirmation text", "Thanks for signing up", confirmation);
        } finally {
            driver.quit();
        }
    }
}

Replace the example URL, selectors, and expected message with those from your application. This example uses the JUnit Assert class, which TestNG documentation demonstrates for comparisons such as assertEquals; the static import lets the test call that method directly. TestNG Documentation

Free tools Windows power users keep installed

One-click scans. No signup required.

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.

What the assertion checks

  • assertEquals(expected, actual) compares the outcome Selenium observed with the value the test requires.
  • The first argument is the expected value and the second is the actual value. Keeping that order makes failure output easier to interpret.
  • For a title check, read driver.getTitle() and compare it with the expected title. For an input value, read the element’s getAttribute("value") and compare that result.

What happens when it fails

TestNG considers a test successful if it completes without an exception or throws an exception declared as expected. A failed assertion raises an AssertionError, so TestNG marks that test method as failed. The failure points to the assertion and its expected-versus-actual comparison. TestNG Documentation

Wait for dynamic pages before asserting

A browser navigation or click can return before JavaScript has updated the page. Reading the result too soon creates a timing race: the assertion compares the expected state with stale or missing content. Selenium recommends waiting for a specific condition rather than relying on an arbitrary pause. Its explicit waits poll for a condition until it succeeds or times out; available expected conditions include element presence, visibility, visible text, and title content. Selenium: Waiting Strategies

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.
  1. Trigger the browser action, such as clicking Submit.
  2. Wait for a condition that represents the desired outcome, such as a confirmation element becoming visible or a title containing expected text.
  3. Read the resulting page value.
  4. Assert that the value matches the expectation.

Use the condition that reflects what the user should actually see. Waiting only for an element to exist may not be enough if the element is hidden or its text has not appeared yet.

Why a fixed sleep is brittle

A fixed pause does not establish that the page is ready: it may waste time when the page is fast and still be too short when it is slow. A condition-based wait proceeds as soon as the required state is observed and fails after its timeout if that state never arrives.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Implicit and explicit waits

Selenium’s implicit wait applies globally to element-location calls and defaults to zero. Selenium warns against mixing implicit and explicit waits because the combined timing can become unpredictable. Prefer an explicit wait for the particular state your assertion depends on, and do not layer it over a nonzero implicit wait without understanding the resulting behavior. Selenium: Waiting Strategies

Java assert versus an assertion method

Java’s language-level assert is distinct from calling an assertion method such as assertEquals. TestNG documentation notes that Java assertions require the JVM to run with -ea (enable assertions); otherwise, a Java assert may appear not to fire. The documented JUnit Assert methods provide explicit comparison operations and can be statically imported in a TestNG test. TestNG Documentation

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

Choose an assertion that makes the expected and actual values clear. Regardless of assertion style, synchronize the browser state first: no assertion API can fix a check made before the page has updated.

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

Troubleshoot common assertion failures

  • Expected text differs from actual text: Inspect the actual value at the failure point, including whitespace or wording differences, then verify that the test is checking the intended element and state.
  • The expected element cannot be found: Confirm the locator matches the current page and wait for the relevant condition if the element is added asynchronously.
  • The assertion fails intermittently: Treat it as a likely synchronization issue. Wait for the expected visible state or text instead of adding a guessed sleep.
  • A Java assert does not fail: Check that the JVM is launched with -ea. This applies to the Java keyword, not to an assertion method that throws an error on mismatch.
  • The wait times out: The expected condition may never become true, the selector may be wrong, or the application may have failed to reach the expected state. Check the page and locator before increasing the timeout.

Or skip the browser setup

If your goal is to capture a page image or PDF rather than verify an interactive browser workflow, ScreenshotNeo offers a one-request screenshot API. Its request can return an image or PDF; it does not replace Selenium assertions for testing application behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.
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 request options. Cookie banners, popups, and chat widgets are removed before the shot; bot checks, blank pages, and failed loads are not billed. Its MCP server provides screenshot tools for AI agents, and the free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. Sign up for free.

Frequently Asked Questions

Can I use TestNG with Selenium in Java?

Yes. Selenium identifies TestNG as one of the Java test-runner choices; JUnit is another.

Does an assertion check that a click itself succeeded?

An assertion should check an observable result of the action, such as the resulting title or visible confirmation text.

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.