ThisCalculator

Everyday

Java 11 HttpClient With Selenium: A Practical Guide

Updated · Standard formula, shown below

Java 11 HttpClient With Selenium: A Practical Guide

Java 11’s built-in HttpClient can send API requests from a Selenium test without opening a browser tab for each request. Use it for setup, test-data creation, or API checks, while Selenium handles the user interface; you will need Java 11 or newer, Selenium, and access to the HTTP service you intend to call.

What You’ll Need

  • A Java 11-or-newer JDK. The HttpClient API became part of the standard library in Java 11.
  • A Java project with Selenium WebDriver installed and a browser driver configured. Use a current Selenium release compatible with your project’s Java version.
  • The target service’s URL, request format, and any required authentication. Use a test environment and credentials intended for automated tests.
  • An IDE or build tool such as Maven or Gradle, plus a test framework if you are integrating this into automated tests.

Step-by-Step

  1. Decide which work belongs to HTTP and which belongs to Selenium. Use HttpClient when you need to call an API directly—for example, to create a test record or verify a service response. Use Selenium when you need to interact with or inspect a page. An API response does not prove that the browser UI works, so retain UI assertions for behaviors that matter to users.
  2. Create an HttpClient. Import java.net.http.HttpClient and build a client once for the test class or test fixture. For example:
    private final HttpClient httpClient = HttpClient.newBuilder()
        .connectTimeout(Duration.ofSeconds(10))
        .build();

    Also import java.time.Duration. The connection timeout limits the time allowed to establish a connection; it is not a complete limit on how long a response may take.

  3. Build a request with a URI and method. Import HttpRequest, URI, and HttpResponse. A simple GET request can be created as follows:
    HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://example.test/api/items"))
        .timeout(Duration.ofSeconds(20))
        .header("Accept", "application/json")
        .GET()
        .build();

    Replace the example address with your test service. Set a request timeout when an unbounded wait would stall your test run. Add headers required by the API, such as an authorization header, using a secure test credential rather than a password embedded in source control.

  4. Send the request and inspect the response. Use a body handler appropriate for the response. ofString() is convenient for small text or JSON responses:
    HttpResponse<String> response = httpClient.send(
        request, HttpResponse.BodyHandlers.ofString());
    
    if (response.statusCode() != 200) {
        throw new AssertionError("Unexpected HTTP status: " + response.statusCode());
    }
    String body = response.body();

    send() blocks until a response arrives or the request fails. It can throw IOException or InterruptedException, so handle or declare those exceptions. In a test, fail clearly rather than swallowing them. If you catch InterruptedException, restore the interrupt flag with Thread.currentThread().interrupt() before reporting or propagating the failure.

  5. Use the API call alongside WebDriver. Initialize Selenium as usual, then call the API at the point your test needs it. For example, create a record through the API, open the relevant page with driver.get(...), and verify the record is displayed using a Selenium locator and assertion. Keep WebDriver and HttpClient as separate tools; HttpClient does not control the browser or share its page state automatically.
  6. Handle authentication and browser state deliberately. An API token or session cookie used by HttpClient is not automatically available to the browser. If the UI requires a logged-in session, log in through the UI, or deliberately transfer the required session data using an approved test setup. Cookie formats and security attributes matter; do not assume that copying a cookie name and value is sufficient. Avoid logging tokens, cookies, or sensitive response bodies.
  7. Clean up test data and resources. If the API creates records, remove them through an appropriate test endpoint or use isolated data that can be discarded. In shared test environments, use unique identifiers to prevent collisions. Quit the WebDriver in a test teardown or a finally block. HttpClient is reusable and does not need to be recreated for every request.
  8. Run the test and check both layers. Confirm that the API status and response meet expectations, then make Selenium assert the intended page behavior. Keep assertions specific: check the expected status code, important response fields, and a visible UI element rather than merely checking that no exception occurred.

Common Mistakes

  • Expecting shared cookies or login state: HttpClient and the browser maintain separate networking state unless you explicitly coordinate it.
  • Checking only for a successful HTTP call: A 2xx response does not confirm that Selenium found the right page content or that the UI rendered correctly.
  • Ignoring status codes and response bodies: HTTP errors may still produce a response object. Assert the status and inspect useful error details without exposing secrets.
  • Leaving requests without timeouts: A slow or unresponsive service can make a test hang. Set a request timeout and choose a connection timeout suitable for the test environment.
  • Putting credentials in code or logs: Read test secrets from an appropriate environment or secret store, and redact them from diagnostics.
  • Using real production data for setup: Prefer isolated test accounts and services. Keep test creation and cleanup predictable.

Tips

  • Build one reusable HttpClient per test fixture or suite; it is designed to be reused.
  • Use BodyHandlers.ofString() for small text responses and a streaming or file body handler for large downloads.
  • For JSON, use a JSON library to parse and assert fields instead of relying on fragile substring checks.
  • Keep API setup helpers separate from page objects so the test clearly distinguishes backend preparation from browser interaction.
  • Use sendAsync() only when asynchronous requests help your test design. You must still wait for and validate the resulting future before depending on its response.

Troubleshooting

Compilation says HttpClient or HttpRequest cannot be found: Check that the project compiler and runtime target Java 11 or newer. Verify the JDK selected by your IDE and build tool, not just the Java version installed on your computer.

The request times out or cannot connect: Check the URL, DNS, network access, proxy settings, and whether the test service is running. Distinguish a connection timeout from the request timeout, and check whether a firewall or test environment blocks access.

You receive an unexpected status: Print the status code and a safe, limited portion of the response body. Confirm the endpoint, HTTP method, headers, payload, and authentication. Do not assume every successful operation returns 200; the API contract may specify another success status.

The API succeeds but Selenium shows no data: Confirm that the UI uses the same environment and account, that the created record is visible to that user, and that any processing delay has completed. Wait for a meaningful UI condition rather than adding a fixed sleep as the first remedy.

The browser is logged out: This is expected if only HttpClient authenticated. Sign in through the UI or implement a deliberate, supported session-sharing setup. If cookie transfer is involved, check domain, path, expiry, and security attributes.

The test hangs or fails intermittently: Set request timeouts, preserve interruption when handling InterruptedException, and review service logs. Ensure cleanup runs even after assertions fail, and avoid depending on shared mutable test data.

Frequently Asked Questions

Can Java 11 HttpClient replace Selenium?
No. HttpClient makes HTTP requests; it does not render pages, click controls, or verify browser behavior. Use Selenium for browser-based interactions.

Can I use HttpClient and Selenium in the same test?
Yes. A common pattern is to prepare or check data through an API and then use Selenium to verify the user-facing result.

Does HttpClient use the browser’s cookies?
No, not automatically. The client and browser have separate cookie stores and sessions unless your test explicitly coordinates them.

Is HttpClient available without an extra dependency?
Yes. It is included in the Java standard library starting with Java 11; Selenium and any JSON parser are separate dependencies.

Should I use send() or sendAsync()?
Use send() for straightforward, sequential test setup. Use sendAsync() when asynchronous work is genuinely useful, and ensure the test waits for and checks completion.

Can I use HttpClient to download a large file?
Yes. Choose a suitable body handler, such as one that writes to a file, rather than loading a large response entirely into memory as a string.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.