HTTP client and server abstractions for Selenium WebDriver communication
npx @tessl/cli install tessl/maven-org-seleniumhq-selenium--selenium-http@4.33.0Selenium HTTP provides HTTP client and server abstractions specifically designed for Selenium WebDriver communication. It offers a comprehensive HTTP layer that includes client configuration, request/response handling, routing capabilities, and WebSocket support with pluggable HTTP client factories, built-in retry mechanisms, and specialized utilities for Selenium-specific tasks.
org.seleniumhq.selenium:selenium-http:4.33.0 in your Maven dependencies<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-http</artifactId>
<version>4.33.0</version>
</dependency>import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.http.HttpMessage;
import org.openqa.selenium.remote.http.HttpMethod;
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.remote.http.Contents;
import org.openqa.selenium.remote.http.HttpClientName;import org.openqa.selenium.remote.http.*;
import java.net.URL;
// Create HTTP client with default configuration
URL baseUrl = new URL("https://api.example.com");
HttpClient client = HttpClient.Factory.createDefault().createClient(baseUrl);
// Create and execute a GET request
HttpRequest request = new HttpRequest(HttpMethod.GET, "/users");
request.addHeader("Accept", "application/json");
HttpResponse response = client.execute(request);
System.out.println("Status: " + response.getStatus());
System.out.println("Response: " + Contents.string(response));
// Clean up
client.close();The Selenium HTTP package is built around several key components:
HttpClient interface with pluggable factory implementations using Java's ServiceLoader mechanismHttpRequest and HttpResponse classes extending a common HttpMessage base with header and content managementClientConfig class providing builder pattern for timeouts, proxies, SSL context, and filtersFilter interface enabling request/response interception with built-in filters for user agents, logging, and retriesRoute and Routable interfaces with URL template matching for server-side request handlingContents utility for various content formats and conversionsCore HTTP client functionality for making requests with full configuration support, connection pooling, and async execution capabilities.
public interface HttpClient extends Closeable, HttpHandler {
WebSocket openSocket(HttpRequest request, WebSocket.Listener listener);
default CompletableFuture<HttpResponse> executeAsync(HttpRequest req);
default void close();
interface Factory {
static Factory create(String name);
static Factory createDefault();
default HttpClient createClient(URL url);
HttpClient createClient(ClientConfig config);
default void cleanupIdleClients();
}
}HTTP message abstractions with header management, content handling, and query parameter support for both requests and responses.
public class HttpRequest extends HttpMessage<HttpRequest> {
public HttpRequest(HttpMethod method, String uri);
public String getUri();
public HttpMethod getMethod();
public String getQueryParameter(String name);
public HttpRequest addQueryParameter(String name, String value);
public Iterable<String> getQueryParameterNames();
public Iterable<String> getQueryParameters(String name);
}
public class HttpResponse extends HttpMessage<HttpResponse> {
public boolean isSuccessful();
public int getStatus();
public HttpResponse setStatus(int status);
public HttpResponse setTargetHost(String host);
public String getTargetHost();
}Flexible client configuration system with builder pattern for timeouts, authentication, proxies, SSL context, and filter chains.
public class ClientConfig {
public static ClientConfig defaultConfig();
public ClientConfig baseUri(URI baseUri);
public ClientConfig baseUrl(URL baseUrl);
public ClientConfig connectionTimeout(Duration timeout);
public ClientConfig readTimeout(Duration timeout);
public ClientConfig withFilter(Filter filter);
public ClientConfig withRetries();
public ClientConfig proxy(Proxy proxy);
public ClientConfig authenticateAs(Credentials credentials);
public ClientConfig sslContext(SSLContext sslContext);
public ClientConfig version(String version);
}WebSocket client functionality with message types (text, binary, close) and event-driven listener interface for real-time communication.
public interface WebSocket extends Closeable {
WebSocket send(Message message);
default WebSocket sendText(CharSequence data);
default WebSocket sendBinary(byte[] data);
void close();
interface Listener extends Consumer<Message> {
default void onBinary(byte[] data);
default void onClose(int code, String reason);
default void onText(CharSequence data);
default void onError(Throwable cause);
}
}Comprehensive content utilities for converting between different formats (strings, bytes, JSON) with charset support and streaming capabilities.
public class Contents {
public static Supplier empty();
public static Supplier utf8String(CharSequence value);
public static Supplier string(CharSequence value, Charset charset);
public static Supplier bytes(byte[] bytes);
public static byte[] bytes(Supplier supplier);
public static String utf8String(Supplier supplier);
public static String string(Supplier supplier, Charset charset);
public static String string(HttpMessage<?> message);
public static Reader utf8Reader(Supplier supplier);
public static Reader reader(Supplier supplier, Charset charset);
public static Reader reader(HttpMessage<?> message);
public static Supplier asJson(Object obj);
public static <T> T fromJson(HttpMessage<?> message, Type typeOfT);
public static String string(File input) throws IOException;
public interface Supplier extends java.util.function.Supplier<InputStream>, AutoCloseable {
int length();
void close() throws IOException;
}
}HTTP request/response filtering system using decorator pattern with built-in filters for user agents, logging, and automatic retries.
@FunctionalInterface
public interface Filter extends Function<HttpHandler, HttpHandler> {
Filter andThen(Filter next);
HttpHandler andFinally(HttpHandler end);
Routable andFinally(Routable end);
}Server-side HTTP request routing with URL template matching, parameter extraction, and nested route support for building HTTP servers.
public abstract class Route implements HttpHandler, Routable {
public HttpHandler fallbackTo(Supplier<HttpHandler> handler);
public final HttpResponse execute(HttpRequest req);
protected abstract HttpResponse handle(HttpRequest req);
public static PredicatedConfig matching(Predicate<HttpRequest> predicate);
public static TemplatizedRouteConfig delete(String template);
public static TemplatizedRouteConfig get(String template);
public static TemplatizedRouteConfig post(String template);
public static TemplatizedRouteConfig options(String template);
public static NestedRouteConfig prefix(String prefix);
public static Route combine(Routable first, Routable... others);
}public abstract class HttpMessage<M extends HttpMessage<M>> {
public Object getAttribute(String key);
public M setAttribute(String key, Object value);
public M removeAttribute(String key);
public Iterable<String> getAttributeNames();
public void forEachHeader(BiConsumer<String, String> action);
public Iterable<String> getHeaderNames();
public Iterable<String> getHeaders(String name);
public String getHeader(String name);
public M setHeader(String name, String value);
public M addHeader(String name, String value);
public M removeHeader(String name);
public Charset getContentEncoding();
public M setContent(Contents.Supplier supplier);
public Contents.Supplier getContent();
}
public enum HttpMethod {
DELETE, GET, POST, PUT, OPTIONS, PATCH, HEAD, CONNECT, TRACE;
public static HttpMethod getHttpMethod(String method);
}
public enum HttpHeader {
CacheControl, ContentLength, ContentType, Expires, Host, UserAgent, XForwardedFor;
public String getName();
}
@FunctionalInterface
public interface HttpHandler {
HttpResponse execute(HttpRequest req) throws UncheckedIOException;
default HttpHandler with(Filter filter);
}
public interface Routable extends HttpHandler {
boolean matches(HttpRequest req);
default Routable with(Filter filter);
}
public class ConnectionFailedException extends WebDriverException {
public ConnectionFailedException(String message);
public ConnectionFailedException(String message, Throwable cause);
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface HttpClientName {
String value();
}