CtrlK
BlogDocsLog inGet started
Tessl Logo

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

Chrome DevTools Protocol (CDP) bindings for Java - version 110, enabling advanced browser automation features like network interception, console event handling, JavaScript execution, and target management.

Pending
Overview
Eval results
Files

target.mddocs/

Target Management

Browser target discovery, attachment, and management for working with browser tabs, workers, and other execution contexts using Chrome DevTools Protocol v110.

Capabilities

Target Domain Wrapper

High-level interface for CDP target management providing type-safe access to browser target operations.

/**
 * Target domain implementation for CDP v110 target operations
 */
public class v110Target implements org.openqa.selenium.devtools.idealized.target.Target {
    // Implementation of idealized target interface
}

Target Discovery

Discover and list all available browser targets including tabs, workers, and other execution contexts.

/**
 * Get all available targets
 * @return Command returning list of target information
 */
public Command<List<org.openqa.selenium.devtools.idealized.target.model.TargetInfo>> getTargets();

Usage Example:

import org.openqa.selenium.devtools.idealized.target.model.TargetInfo;
import org.openqa.selenium.devtools.idealized.target.model.TargetID;

v110Target target = new v110Target();

// Get all available targets
List<TargetInfo> targets = devTools.send(target.getTargets());

for (TargetInfo targetInfo : targets) {
    System.out.println("Target ID: " + targetInfo.getTargetId());
    System.out.println("Type: " + targetInfo.getType());
    System.out.println("Title: " + targetInfo.getTitle());
    System.out.println("URL: " + targetInfo.getUrl());
    System.out.println("Attached: " + targetInfo.getAttached());
    System.out.println("---");
}

Target Attachment

Attach to specific targets to control them independently.

/**
 * Attach to specific target
 * @param targetId Target identifier to attach to
 * @return Command returning session ID for attached target
 */
public Command<SessionID> attachToTarget(TargetID targetId);

/**
 * Enable automatic attachment to targets
 * @return Command to set auto-attach behavior
 */
public Command<Void> setAutoAttach();

Usage Example:

import org.openqa.selenium.devtools.idealized.target.model.SessionID;

// Find a specific target (e.g., a new tab)
List<TargetInfo> targets = devTools.send(target.getTargets());
Optional<TargetInfo> pageTarget = targets.stream()
    .filter(t -> "page".equals(t.getType()) && t.getUrl().contains("example.com"))
    .findFirst();

if (pageTarget.isPresent()) {
    TargetID targetId = pageTarget.get().getTargetId();
    
    // Attach to the target
    SessionID sessionId = devTools.send(target.attachToTarget(targetId));
    System.out.println("Attached to target with session: " + sessionId);
    
    // Now you can send commands to this specific target
    // using the session ID
}

Target Detachment

Detach from targets when no longer needed.

/**
 * Detach from target
 * @param sessionId Optional session ID to detach from
 * @param targetId Optional target ID to detach from
 * @return Command to detach
 */
public Command<Void> detachFromTarget(Optional<SessionID> sessionId, Optional<TargetID> targetId);

/**
 * Get target detached events
 * @return Event stream for target detachment
 */
public Event<TargetID> detached();

Usage Example:

// Listen for target detachment events
devTools.addListener(target.detached(), detachedTargetId -> {
    System.out.println("Target detached: " + detachedTargetId);
});

// Detach from specific target
devTools.send(target.detachFromTarget(
    Optional.of(sessionId),
    Optional.empty()
));

// Or detach by target ID
devTools.send(target.detachFromTarget(
    Optional.empty(),
    Optional.of(targetId)
));

Auto-Attachment

Configure automatic attachment to new targets as they are created.

/**
 * Enable auto-attach to targets
 * @return Command to enable automatic target attachment
 */
public Command<Void> setAutoAttach();

Usage Example:

import org.openqa.selenium.devtools.v110.target.Target;

// Enable auto-attach to new targets
devTools.send(target.setAutoAttach());

// Listen for new targets
devTools.addListener(Target.targetCreated(), targetInfo -> {
    System.out.println("New target created: " + targetInfo.getTitle());
    
    // Automatically attach if it's a page
    if ("page".equals(targetInfo.getType())) {
        SessionID session = devTools.send(target.attachToTarget(targetInfo.getTargetId()));
        System.out.println("Auto-attached with session: " + session);
    }
});

CDP Domain Classes

Target Domain

Direct access to CDP Target domain for low-level target operations.

import org.openqa.selenium.devtools.v110.target.Target;

/**
 * Get available targets
 */
public static Command<GetTargetsResponse> getTargets(Optional<Boolean> filter);

/**
 * Attach to target
 */
public static Command<AttachToTargetResponse> attachToTarget(
    org.openqa.selenium.devtools.v110.target.model.TargetID targetId,
    Optional<Boolean> flatten
);

/**
 * Detach from target
 */
public static Command<Void> detachFromTarget(
    Optional<org.openqa.selenium.devtools.v110.target.model.SessionID> sessionId,
    Optional<org.openqa.selenium.devtools.v110.target.model.TargetID> targetId
);

/**
 * Set auto-attach to targets
 */
public static Command<Void> setAutoAttach(
    Boolean autoAttach,
    Boolean waitForDebuggerOnStart,
    Optional<Boolean> flatten,
    Optional<List<String>> filter
);

/**
 * Target created event
 */
public static Event<TargetCreated> targetCreated();

/**
 * Target destroyed event
 */
public static Event<TargetDestroyed> targetDestroyed();

/**
 * Target crashed event
 */
public static Event<TargetCrashed> targetCrashed();

/**
 * Attached to target event
 */
public static Event<AttachedToTarget> attachedToTarget();

/**
 * Detached from target event
 */
public static Event<DetachedFromTarget> detachedFromTarget();

Model Classes

Target Information Models

import org.openqa.selenium.devtools.v110.target.model.*;
import org.openqa.selenium.devtools.idealized.target.model.*;
import org.openqa.selenium.devtools.idealized.browser.model.BrowserContextID;

/**
 * CDP Target information
 */
public class org.openqa.selenium.devtools.v110.target.model.TargetInfo {
    /**
     * Target identifier
     */
    TargetID getTargetId();
    
    /**
     * Target type (page, background_page, service_worker, etc.)
     */
    String getType();
    
    /**
     * Target title
     */
    String getTitle();
    
    /**
     * Target URL
     */
    String getUrl();
    
    /**
     * Whether target is attached
     */
    Boolean getAttached();
    
    /**
     * Opener target ID if opened by another target
     */
    Optional<TargetID> getOpenerId();
    
    /**
     * Browser context ID
     */
    Optional<BrowserContextID> getBrowserContextId();
}

/**
 * Idealized target information (high-level)
 */
public class org.openqa.selenium.devtools.idealized.target.model.TargetInfo {
    org.openqa.selenium.devtools.idealized.target.model.TargetID getTargetId();
    String getType();
    String getTitle();
    String getUrl();
    Boolean getAttached();
    Optional<org.openqa.selenium.devtools.idealized.target.model.TargetID> getOpenerId();
    Optional<BrowserContextID> getBrowserContextId();
}

/**
 * Target identifier
 */
public class org.openqa.selenium.devtools.v110.target.model.TargetID {
    String toString();
}

/**
 * Idealized target ID
 */
public class org.openqa.selenium.devtools.idealized.target.model.TargetID {
    String toString();
}

/**
 * Session identifier for attached targets
 */
public class org.openqa.selenium.devtools.v110.target.model.SessionID {
    String toString();
}

/**
 * Idealized session ID
 */
public class org.openqa.selenium.devtools.idealized.target.model.SessionID {
    String toString();
}

Target Event Models

/**
 * Target created event
 */
public class TargetCreated {
    TargetInfo getTargetInfo();
}

/**
 * Target destroyed event
 */
public class TargetDestroyed {
    TargetID getTargetId();
}

/**
 * Target crashed event
 */
public class TargetCrashed {
    TargetID getTargetId();
    String getStatus();
    Integer getErrorCode();
}

/**
 * Attached to target event
 */
public class AttachedToTarget {
    SessionID getSessionId();
    TargetInfo getTargetInfo();
    Boolean getWaitingForDebugger();
}

/**
 * Detached from target event
 */
public class DetachedFromTarget {
    SessionID getSessionId();
    Optional<TargetID> getTargetId();
}

Response Models

/**
 * Get targets response
 */
public class GetTargetsResponse {
    List<TargetInfo> getTargetInfos();
}

/**
 * Attach to target response
 */
public class AttachToTargetResponse {
    SessionID getSessionId();
}

Advanced Usage Examples

Multi-Target Coordination

v110Target target = new v110Target();

// Get all page targets
List<TargetInfo> allTargets = devTools.send(target.getTargets());
List<TargetInfo> pageTargets = allTargets.stream()
    .filter(t -> "page".equals(t.getType()))
    .collect(Collectors.toList());

// Attach to multiple targets
Map<TargetID, SessionID> attachedTargets = new HashMap<>();

for (TargetInfo targetInfo : pageTargets) {
    try {
        SessionID sessionId = devTools.send(target.attachToTarget(targetInfo.getTargetId()));
        attachedTargets.put(targetInfo.getTargetId(), sessionId);
        System.out.println("Attached to: " + targetInfo.getTitle());
    } catch (DevToolsException e) {
        System.err.println("Failed to attach to target: " + e.getMessage());
    }
}

// Later, detach from all targets
for (Map.Entry<TargetID, SessionID> entry : attachedTargets.entrySet()) {
    devTools.send(target.detachFromTarget(
        Optional.of(entry.getValue()),
        Optional.of(entry.getKey())
    ));
}

Service Worker Monitoring

// Monitor for service worker targets
devTools.addListener(Target.targetCreated(), event -> {
    TargetInfo targetInfo = event.getTargetInfo();
    
    if ("service_worker".equals(targetInfo.getType())) {
        System.out.println("Service worker created: " + targetInfo.getUrl());
        
        // Attach to service worker for debugging
        try {
            SessionID sessionId = devTools.send(target.attachToTarget(targetInfo.getTargetId()));
            System.out.println("Attached to service worker: " + sessionId);
            
            // Enable runtime in service worker context
            // (would need to send commands with session context)
        } catch (DevToolsException e) {
            System.err.println("Failed to attach to service worker: " + e.getMessage());
        }
    }
});

// Enable auto-attach for new targets
devTools.send(target.setAutoAttach());

Tab Management

// Monitor tab creation and destruction
devTools.addListener(Target.targetCreated(), event -> {
    TargetInfo info = event.getTargetInfo();
    if ("page".equals(info.getType())) {
        System.out.println("New tab opened: " + info.getTitle());
        System.out.println("URL: " + info.getUrl());
    }
});

devTools.addListener(Target.targetDestroyed(), event -> {
    System.out.println("Tab closed: " + event.getTargetId());
});

// Find and focus specific tab
List<TargetInfo> targets = devTools.send(target.getTargets());
Optional<TargetInfo> specificTab = targets.stream()
    .filter(t -> "page".equals(t.getType()))
    .filter(t -> t.getUrl().contains("important-page.com"))
    .findFirst();

if (specificTab.isPresent()) {
    // Attach to bring into focus (implementation specific)
    SessionID session = devTools.send(target.attachToTarget(specificTab.get().getTargetId()));
    System.out.println("Focused tab: " + specificTab.get().getTitle());
}

Error Handling

Target management operations may encounter various error conditions:

import org.openqa.selenium.devtools.DevToolsException;

// Handle target discovery failures
try {
    List<TargetInfo> targets = devTools.send(target.getTargets());
} catch (DevToolsException e) {
    System.err.println("Failed to get targets: " + e.getMessage());
}

// Handle attachment failures
devTools.addListener(Target.targetCreated(), event -> {
    try {
        SessionID session = devTools.send(target.attachToTarget(event.getTargetInfo().getTargetId()));
        System.out.println("Successfully attached: " + session);
    } catch (DevToolsException e) {
        System.err.println("Failed to attach to target: " + e.getMessage());
        // Continue without attachment
    }
});

// Handle unexpected detachment
devTools.addListener(target.detached(), targetId -> {
    System.out.println("Target unexpectedly detached: " + targetId);
    // Clean up resources associated with this target
    cleanupTarget(targetId);
});

Install with Tessl CLI

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

docs

events.md

index.md

javascript.md

logging.md

network.md

target.md

tile.json