CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-seleniumhq-selenium--selenium-devtools-v133

Java bindings for Chrome DevTools Protocol v133, enabling advanced browser automation capabilities through Selenium WebDriver.

Pending
Overview
Eval results
Files

javascript.mddocs/

JavaScript Control

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.

Capabilities

v133Javascript Class

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);
}

Runtime Domain Control

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();

JavaScript Binding Management

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");

Page Domain Control

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();

Document Script Management

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));

JavaScript Data Types

ScriptIdentifier

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
}

BindingCalled

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
}

Advanced JavaScript Patterns

Bidirectional Communication

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()));
    }
});

Script State Management

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();
}

Integration with Runtime Domain

The v133Javascript class seamlessly integrates with the Runtime domain for:

  • Script Evaluation: Direct JavaScript code execution
  • Object Inspection: Examining JavaScript objects and values
  • Context Management: Managing execution contexts across frames
  • Exception Handling: Capturing and processing JavaScript errors

Error Handling

JavaScript operations include comprehensive error handling for:

  • Binding Failures: When JavaScript bindings cannot be created or called
  • Script Injection Errors: When scripts fail to inject or execute
  • Communication Failures: When bidirectional communication breaks down
  • Context Loss: When execution contexts become invalid

Install with Tessl CLI

npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-devtools-v133

docs

events.md

index.md

javascript.md

logging.md

network.md

targets.md

tile.json