Java bindings for Chrome DevTools Protocol version 102, providing programmatic access to Chrome browser debugging and automation capabilities
—
Manages JavaScript execution, binding operations, and script injection for new documents. This domain enables advanced JavaScript interaction capabilities including custom function bindings and automatic script injection.
Main class for JavaScript execution and binding operations. Extends the idealized Javascript interface to provide v102-specific implementations for script management and JavaScript bindings.
/**
* Manages JavaScript execution and binding operations
*/
public class V102Javascript extends Javascript<ScriptIdentifier, BindingCalled> {
/**
* Creates a new V102Javascript instance with the specified DevTools connection
* @param devtools DevTools connection instance (required)
*/
public V102Javascript(DevTools devtools);
/**
* Enables the Runtime domain for JavaScript operations
* @return Command to enable runtime domain
*/
protected Command<Void> enableRuntime();
/**
* Disables the Runtime domain
* @return Command to disable runtime domain
*/
protected Command<Void> disableRuntime();
/**
* Enables the Page domain for document-level script operations
* @return Command to enable page domain
*/
protected Command<Void> enablePage();
/**
* Disables the Page domain
* @return Command to disable page domain
*/
protected Command<Void> disablePage();
/**
* Adds a JavaScript binding to make Java functions callable from JavaScript
* @param scriptName Name of the binding function available in JavaScript
* @return Command to add the binding
*/
protected Command<Void> doAddJsBinding(String scriptName);
/**
* Removes a previously added JavaScript binding
* @param scriptName Name of the binding to remove
* @return Command to remove the binding
*/
protected Command<Void> doRemoveJsBinding(String scriptName);
/**
* Adds a script to be evaluated automatically on every new document
* @param script JavaScript code to execute on new documents
* @return Command returning ScriptIdentifier for the added script
*/
protected Command<ScriptIdentifier> addScriptToEvaluateOnNewDocument(String script);
/**
* Removes a script that was set to evaluate on new documents
* @param id ScriptIdentifier of the script to remove
* @return Command to remove the script
*/
protected Command<Void> removeScriptToEvaluateOnNewDocument(ScriptIdentifier id);
/**
* Returns the binding called event for monitoring JavaScript binding invocations
* @return Event for binding call notifications
*/
protected Event<BindingCalled> bindingCalledEvent();
/**
* Extracts payload from a binding called event
* @param event BindingCalled event containing the payload
* @return String payload from the binding call
*/
protected String extractPayload(BindingCalled event);
}Usage Examples:
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v102.V102Domains;
// Setup
ChromeDriver driver = new ChromeDriver();
DevTools devTools = driver.getDevTools();
devTools.createSession();
V102Domains domains = new V102Domains(devTools);
// Enable JavaScript domain
domains.javascript().enable();
// Add a JavaScript binding
devTools.send(domains.javascript().doAddJsBinding("myJavaFunction"));
// Listen for binding calls
devTools.addListener(domains.javascript().bindingCalledEvent(), (event) -> {
String payload = domains.javascript().extractPayload(event);
System.out.println("JavaScript called Java with payload: " + payload);
// Process the payload and potentially respond
// (Response mechanism depends on your application logic)
});
// Navigate to a page
driver.get("https://example.com");
// Execute JavaScript that calls the binding
driver.executeScript("window.myJavaFunction('Hello from JavaScript');");
// Cleanup
devTools.send(domains.javascript().doRemoveJsBinding("myJavaFunction"));
domains.javascript().disable();import org.openqa.selenium.devtools.v102.page.model.ScriptIdentifier;
// Enable both Runtime and Page domains
domains.javascript().enable();
devTools.send(domains.javascript().enablePage());
// Add script to run on every new document
String initScript = "window.customInit = true; console.log('Custom script loaded');";
ScriptIdentifier scriptId = devTools.send(
domains.javascript().addScriptToEvaluateOnNewDocument(initScript)
);
// Navigate to pages - the script will run automatically
driver.get("https://example.com");
driver.get("https://another-site.com");
// Verify script execution
Boolean isInitialized = (Boolean) driver.executeScript("return window.customInit;");
System.out.println("Custom script initialized: " + isInitialized);
// Remove the script when no longer needed
devTools.send(domains.javascript().removeScriptToEvaluateOnNewDocument(scriptId));
// Cleanup
devTools.send(domains.javascript().disablePage());import com.google.gson.Gson;
import java.util.Map;
// Setup binding with JSON payload processing
devTools.send(domains.javascript().doAddJsBinding("processData"));
devTools.addListener(domains.javascript().bindingCalledEvent(), (event) -> {
String payload = domains.javascript().extractPayload(event);
try {
// Parse JSON payload
Gson gson = new Gson();
Map<String, Object> data = gson.fromJson(payload, Map.class);
// Process the data
String action = (String) data.get("action");
Object params = data.get("params");
switch (action) {
case "getData":
// Return data to JavaScript via executeScript
String result = processGetData(params);
driver.executeScript("window.handleJavaResponse('" + result + "');");
break;
case "saveData":
processSaveData(params);
driver.executeScript("window.handleJavaResponse('saved');");
break;
}
} catch (Exception e) {
System.err.println("Error processing binding payload: " + e.getMessage());
}
});
// JavaScript side would call:
// window.processData(JSON.stringify({action: 'getData', params: {id: 123}}));The V102Javascript class interacts with generated CDP protocol classes from Runtime and Page domains:
// Generated CDP runtime classes (available at runtime)
class Runtime {
static Command<Void> enable();
static Command<Void> disable();
static Command<Void> addBinding(String name, Optional<String> executionContextId, Optional<String> executionContextName);
static Command<Void> removeBinding(String name);
static Event<BindingCalled> bindingCalled();
}
// Binding event data
class BindingCalled {
String getName();
String getPayload();
Optional<Integer> getExecutionContextId();
}// Generated CDP page classes (available at runtime)
class Page {
static Command<Void> enable();
static Command<Void> disable();
static Command<ScriptIdentifier> addScriptToEvaluateOnNewDocument(
String source,
Optional<String> worldName,
Optional<Boolean> includeCommandLineAPI
);
static Command<Void> removeScriptToEvaluateOnNewDocument(ScriptIdentifier identifier);
}
// Script identifier for document scripts
class ScriptIdentifier {
// Implementation details are version-specific
String toString();
}// Selenium DevTools integration classes
abstract class Javascript<ScriptId, BindingEvent> {
protected Javascript(DevTools devtools);
public void enable();
public void disable();
}
class DevTools {
<T> T send(Command<T> command);
<T> void addListener(Event<T> event, Consumer<T> listener);
void createSession();
}// Add binding
devTools.send(domains.javascript().doAddJsBinding("log"));
// Handle calls
devTools.addListener(domains.javascript().bindingCalledEvent(), (event) -> {
if ("log".equals(event.getName())) {
System.out.println("JS Log: " + domains.javascript().extractPayload(event));
}
});
// JavaScript usage: window.log("Hello from JS");// Bidirectional communication binding
devTools.send(domains.javascript().doAddJsBinding("exchange"));
devTools.addListener(domains.javascript().bindingCalledEvent(), (event) -> {
String payload = domains.javascript().extractPayload(event);
String response = processPayload(payload);
// Send response back to JavaScript
driver.executeScript("window.handleResponse(arguments[0]);", response);
});// Add multiple bindings
String[] bindings = {"dataService", "logger", "fileHandler"};
for (String binding : bindings) {
devTools.send(domains.javascript().doAddJsBinding(binding));
}
// Handle all bindings in one listener
devTools.addListener(domains.javascript().bindingCalledEvent(), (event) -> {
String bindingName = event.getName();
String payload = domains.javascript().extractPayload(event);
switch (bindingName) {
case "dataService":
handleDataService(payload);
break;
case "logger":
handleLogger(payload);
break;
case "fileHandler":
handleFileHandler(payload);
break;
}
});
// Cleanup all bindings
for (String binding : bindings) {
devTools.send(domains.javascript().doRemoveJsBinding(binding));
}Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-devtools-v102