CtrlK
BlogDocsLog inGet started
Tessl Logo

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

Chrome DevTools Protocol version 129 bindings for Selenium WebDriver.

Pending
Overview
Eval results
Files

javascript.mddocs/

JavaScript Execution

The v129Javascript class provides comprehensive JavaScript execution and binding capabilities through the Chrome DevTools Protocol. It enables script injection, JavaScript binding management, and code execution in browser contexts.

Capabilities

JavaScript Domain

Core JavaScript domain for script execution and binding management.

/**
 * JavaScript domain for script execution and binding management
 * @param devtools - DevTools instance for protocol communication
 */
public class v129Javascript extends Javascript<ScriptIdentifier, BindingCalled> {
    public v129Javascript(DevTools devtools);
}

Runtime Domain Control

Control the Runtime domain for JavaScript execution capabilities.

/**
 * Enables the Runtime domain for JavaScript execution
 * @return Command to enable Runtime domain
 */
protected Command<Void> enableRuntime();

/**
 * Disables the Runtime domain
 * @return Command to disable Runtime domain
 */
protected Command<Void> disableRuntime();

JavaScript Binding Management

Manage JavaScript bindings between browser and Java code.

/**
 * Adds a JavaScript binding with the specified name
 * @param scriptName - Name of the binding to add
 * @return Command to add JavaScript binding
 */
protected Command<Void> doAddJsBinding(String scriptName);

/**
 * Removes a JavaScript binding with the specified name
 * @param scriptName - Name of the binding to remove
 * @return Command to remove JavaScript binding
 */
protected Command<Void> doRemoveJsBinding(String scriptName);

/**
 * Event fired when a JavaScript binding is called
 * @return Event for binding calls
 */
protected Event<BindingCalled> bindingCalledEvent();

/**
 * Extracts payload from binding called event
 * @param event - BindingCalled event
 * @return Payload string from the binding call
 */
protected String extractPayload(BindingCalled event);

Page Domain Control

Control the Page domain for document-level script management.

/**
 * Enables the Page domain for document script management
 * @return Command to enable Page domain
 */
protected Command<Void> enablePage();

/**
 * Disables the Page domain
 * @return Command to disable Page domain
 */
protected Command<Void> disablePage();

Script Injection Management

Manage scripts that execute automatically on new documents.

/**
 * Adds script to evaluate 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 script from evaluation on new documents
 * @param id - ScriptIdentifier of the script to remove
 * @return Command to remove the script
 */
protected Command<Void> removeScriptToEvaluateOnNewDocument(ScriptIdentifier id);

Usage Examples:

import org.openqa.selenium.devtools.v129.v129Javascript;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v129.page.model.ScriptIdentifier;

// Create JavaScript domain
DevTools devTools = driver.getDevTools();
v129Javascript javascript = new v129Javascript(devTools);

// Enable domains
devTools.send(javascript.enableRuntime());
devTools.send(javascript.enablePage());

// Add JavaScript binding
devTools.send(javascript.doAddJsBinding("myBinding"));

// Listen for binding calls
devTools.addListener(javascript.bindingCalledEvent(), bindingEvent -> {
    String payload = javascript.extractPayload(bindingEvent);
    System.out.println("Binding called with payload: " + payload);
    
    // Process the payload and potentially respond
    if (payload.contains("getData")) {
        // Handle data request from JavaScript
    }
});

// Add script to execute on new documents
String initScript = """
    window.myBinding = function(data) {
        // Call Java binding
        window.binding('myBinding', JSON.stringify(data));
    };
    
    console.log('Initialization script loaded');
    """;

ScriptIdentifier scriptId = devTools.send(
    javascript.addScriptToEvaluateOnNewDocument(initScript)
);

// Navigate to trigger script execution
driver.get("https://example.com");

// Execute JavaScript that uses the binding
driver.executeScript("""
    window.myBinding({
        action: 'getData',
        timestamp: Date.now()
    });
    """);

Advanced JavaScript Patterns

// Dynamic script injection based on page conditions
String conditionalScript = """
    if (window.location.hostname === 'specific-site.com') {
        // Site-specific initialization
        window.customAPI = {
            sendData: function(data) {
                window.binding('dataBinding', JSON.stringify(data));
            }
        };
    }
    """;

devTools.send(javascript.addScriptToEvaluateOnNewDocument(conditionalScript));

// Multiple bindings for different purposes
devTools.send(javascript.doAddJsBinding("dataBinding"));
devTools.send(javascript.doAddJsBinding("errorBinding"));
devTools.send(javascript.doAddJsBinding("logBinding"));

// Handle different binding types
devTools.addListener(javascript.bindingCalledEvent(), bindingEvent -> {
    String payload = javascript.extractPayload(bindingEvent);
    
    try {
        JsonObject data = JsonParser.parseString(payload).getAsJsonObject();
        String bindingType = data.get("binding").getAsString();
        
        switch (bindingType) {
            case "dataBinding":
                handleDataBinding(data);
                break;
            case "errorBinding":
                handleErrorBinding(data);
                break;
            case "logBinding":
                handleLogBinding(data);
                break;
        }
    } catch (Exception e) {
        System.err.println("Error processing binding: " + e.getMessage());
    }
});

// Cleanup scripts when done
devTools.send(javascript.removeScriptToEvaluateOnNewDocument(scriptId));
devTools.send(javascript.doRemoveJsBinding("myBinding"));

Types

JavaScript Event Types

// From v129.page.model package
import org.openqa.selenium.devtools.v129.page.model.ScriptIdentifier;

// From v129.runtime.model package
import org.openqa.selenium.devtools.v129.runtime.model.BindingCalled;

Script Identifier

// ScriptIdentifier for managing injected scripts
class ScriptIdentifier {
    public ScriptIdentifier(String value);
    public String getValue();
    public String toString();
}

Binding Event Types

// BindingCalled event structure
class BindingCalled {
    public String getName();        // Binding name
    public String getPayload();     // Data payload
    public Optional<Integer> getExecutionContextId();
}

JavaScript Execution Context

// Base JavaScript interface from idealized package
import org.openqa.selenium.devtools.idealized.Javascript;

// Command and Event base types
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.Event;

Integration Types

// For advanced JavaScript integration
import org.openqa.selenium.devtools.v129.runtime.Runtime;
import org.openqa.selenium.devtools.v129.page.Page;

// Optional parameters for enhanced functionality
import java.util.Optional;

Install with Tessl CLI

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

docs

domains.md

events.md

index.md

javascript.md

logging.md

network.md

target.md

tile.json