ChromeDPClient

class ChromeDPClient(remoteDebugUrl: String = "http://localhost:9222", overrideHostHeader: Boolean = false, httpClient: HttpClient = if (overrideHostHeader) DEFAULT_HTTP_CLIENT_WITH_HOST_OVERRIDE else DEFAULT_HTTP_CLIENT)(source)

A client using the Chrome Devtools Protocol to communicate with a running Chrome browser via the debugger API.

It provides access to the HTTP endpoints exposed by the Chrome browser, as well as web socket connections to the browser and its targets to make use of the full Chrome Devtools Protocol API.

Note: if you already know the browser target's web socket URL, you don't need to create a ChromeDPClient. Instead, you can directly use HttpClient.chromeWebSocket.

Host override

Chrome doesn't accept a Host header that is not an IP nor localhost, but in some environments it might be hard to provide this (e.g. docker services in a docker swarm, communicating using service names).

To work around this problem, simply set overrideHostHeader to true. This overrides the Host header to "localhost" in the HTTP requests to the Chrome debugger to make it happy, and also replaces the host in subsequent web socket URLs (returned by Chrome) by the initial host provided in remoteDebugUrl. This is necessary because Chrome uses the Host header to build these URLs, and it would be incorrect to keep this.

Constructors

Link copied to clipboard
constructor(remoteDebugUrl: String = "http://localhost:9222", overrideHostHeader: Boolean = false, httpClient: HttpClient = if (overrideHostHeader) DEFAULT_HTTP_CLIENT_WITH_HOST_OVERRIDE else DEFAULT_HTTP_CLIENT)

Functions

Link copied to clipboard
suspend fun activateTab(targetId: String): String

Brings the page identified by the given targetId into the foreground (activates a tab).

Link copied to clipboard
suspend fun closeAllTargets()

Closes all targets.

Link copied to clipboard
suspend fun closeTab(targetId: String): String

Closes the page identified by targetId.

Link copied to clipboard
suspend fun newTab(url: String = "about:blank"): ChromeDPTarget

Opens a new tab, and returns the websocket target data for the new tab.

Link copied to clipboard
suspend fun protocolJson(): String

Fetches the current Chrome DevTools Protocol definition, as a JSON string.

Link copied to clipboard
suspend fun targets(): List<ChromeDPTarget>

Fetches the list of all available web socket targets (e.g. browser tabs).

Link copied to clipboard
suspend fun version(): ChromeVersion

Fetches the browser version metadata via the debugger's HTTP API.

Link copied to clipboard
suspend fun webSocket(): BrowserSession

Opens a web socket connection to interact with the browser.