Java bindings for Chrome DevTools Protocol v133, enabling advanced browser automation capabilities through Selenium WebDriver.
—
The v133Javascript class provides comprehensive JavaScript execution and management capabilities, enabling developers to inject scripts, create JavaScript bindings, and control script execution in the browser context.
Main JavaScript control class that extends the base Javascript functionality with v133-specific implementations.
/**
* v133-specific JavaScript control implementation
* Extends Javascript<ScriptIdentifier, BindingCalled> for type-safe script management
*/
public class v133Javascript extends Javascript<ScriptIdentifier, BindingCalled> {
/**
* Creates a new v133Javascript instance
* @param devtools DevTools instance for communication with browser
*/
public v133Javascript(DevTools devtools);
}Methods for enabling and disabling the runtime domain required for JavaScript operations.
/**
* Enable runtime domain for JavaScript operations
* @return Command to enable runtime domain
*/
protected Command<Void> enableRuntime();
/**
* Disable runtime domain to stop JavaScript operations
* @return Command to disable runtime domain
*/
protected Command<Void> disableRuntime();Create and manage JavaScript bindings that allow communication between browser JavaScript and Java code.
/**
* Add a JavaScript binding with specified name
* @param scriptName Name of the binding to create
* @return Command to add the JavaScript binding
*/
protected Command<Void> doAddJsBinding(String scriptName);
/**
* Remove an existing JavaScript binding
* @param scriptName Name of the binding to remove
* @return Command to remove the JavaScript binding
*/
protected Command<Void> doRemoveJsBinding(String scriptName);
/**
* Get the binding called event for monitoring binding usage
* @return Event for JavaScript binding calls
*/
protected Event<BindingCalled> bindingCalledEvent();
/**
* Extract payload from binding called event
* @param event BindingCalled event
* @return String payload from the binding call
*/
protected String extractPayload(BindingCalled event);Usage Example:
import org.openqa.selenium.devtools.v133.v133Javascript;
import org.openqa.selenium.devtools.v133.runtime.model.BindingCalled;
v133Javascript javascript = new v133Javascript(devTools);
// Enable JavaScript operations
devTools.send(javascript.enableRuntime());
// Add a JavaScript binding
devTools.send(javascript.doAddJsBinding("myBinding"));
// Listen for binding calls
devTools.addListener(javascript.bindingCalledEvent(), (BindingCalled event) -> {
String payload = javascript.extractPayload(event);
System.out.println("Binding called with payload: " + payload);
// Process the payload and potentially respond
// ... handle the binding call
});
// In browser JavaScript, you can now call:
// window.myBinding("some data from browser");Methods for enabling and disabling the page domain required for document script injection.
/**
* Enable page domain for document script operations
* @return Command to enable page domain
*/
protected Command<Void> enablePage();
/**
* Disable page domain to stop document script operations
* @return Command to disable page domain
*/
protected Command<Void> disablePage();Inject scripts that automatically execute when new documents are created.
/**
* Add a script to be evaluated when new documents are created
* @param script JavaScript code to inject
* @return Command returning ScriptIdentifier for the injected script
*/
protected Command<ScriptIdentifier> addScriptToEvaluateOnNewDocument(String script);
/**
* Remove a previously injected document script
* @param id ScriptIdentifier of the script to remove
* @return Command to remove the script
*/
protected Command<Void> removeScriptToEvaluateOnNewDocument(ScriptIdentifier id);Usage Example:
import org.openqa.selenium.devtools.v133.page.model.ScriptIdentifier;
// Enable page domain
devTools.send(javascript.enablePage());
// Inject a script that runs on every new document
String initScript = """
window.seleniumTestMarker = true;
console.log('Selenium test script initialized');
""";
ScriptIdentifier scriptId = devTools.send(
javascript.addScriptToEvaluateOnNewDocument(initScript)
);
// Later, remove the script
devTools.send(javascript.removeScriptToEvaluateOnNewDocument(scriptId));Unique identifier for injected scripts that run on document creation.
// From org.openqa.selenium.devtools.v133.page.model.ScriptIdentifier
public class ScriptIdentifier {
public String toString(); // String representation of the script ID
}Event data for JavaScript binding calls from the browser to Java code.
// From org.openqa.selenium.devtools.v133.runtime.model.BindingCalled
public class BindingCalled {
public String getName(); // Name of the binding that was called
public String getPayload(); // Data payload sent from JavaScript
public Optional<Integer> getExecutionContextId(); // Context where binding was called
}Combine bindings with script injection for full bidirectional communication:
// Create a communication channel
devTools.send(javascript.doAddJsBinding("sendToJava"));
// Inject script that sets up JavaScript side
String communicationScript = """
window.javaApi = {
send: function(data) {
window.sendToJava(JSON.stringify(data));
}
};
window.addEventListener('javaMessage', function(event) {
console.log('Received from Java:', event.detail);
});
""";
devTools.send(javascript.addScriptToEvaluateOnNewDocument(communicationScript));
// Listen for messages from JavaScript
devTools.addListener(javascript.bindingCalledEvent(), (BindingCalled event) -> {
if ("sendToJava".equals(event.getName())) {
String data = event.getPayload();
// Process data from JavaScript
// Send response back to JavaScript
String responseScript = String.format(
"window.dispatchEvent(new CustomEvent('javaMessage', {detail: %s}));",
"\"Response from Java\""
);
devTools.send(Runtime.evaluate(responseScript, Optional.empty(),
Optional.empty(), Optional.empty(), Optional.empty(),
Optional.empty(), Optional.empty(), Optional.empty(),
Optional.empty(), Optional.empty(), Optional.empty()));
}
});Track and manage multiple injected scripts:
import java.util.HashMap;
import java.util.Map;
Map<String, ScriptIdentifier> injectedScripts = new HashMap<>();
// Method to add named script
public void addNamedScript(String name, String script) {
ScriptIdentifier id = devTools.send(
javascript.addScriptToEvaluateOnNewDocument(script)
);
injectedScripts.put(name, id);
}
// Method to remove named script
public void removeNamedScript(String name) {
ScriptIdentifier id = injectedScripts.remove(name);
if (id != null) {
devTools.send(javascript.removeScriptToEvaluateOnNewDocument(id));
}
}
// Cleanup all scripts
public void cleanupAllScripts() {
for (ScriptIdentifier id : injectedScripts.values()) {
devTools.send(javascript.removeScriptToEvaluateOnNewDocument(id));
}
injectedScripts.clear();
}The v133Javascript class seamlessly integrates with the Runtime domain for:
JavaScript operations include comprehensive error handling for:
Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-devtools-v133