CtrlK
BlogDocsLog inGet started
Tessl Logo

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

Chrome DevTools Protocol (CDP) bindings for Selenium WebDriver version 138

Pending
Overview
Eval results
Files

target-management.mddocs/

Target Management

Browser target (tab/window) management and session control for Chrome DevTools Protocol v138. Enables working with multiple browser contexts, coordinating cross-target operations, and managing debugging sessions.

Capabilities

Target Handler

Main target management handler providing browser context and session control.

/**
 * Target/tab management for CDP v138
 * Default constructor - stateless implementation
 */
public class v138Target implements org.openqa.selenium.devtools.idealized.target.Target {
    // No explicit constructor - uses default constructor
}

Usage Example:

import org.openqa.selenium.devtools.v138.v138Target;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.idealized.target.model.*;

v138Target target = new v138Target();

// Get list of available targets
List<TargetInfo> targets = devTools.send(target.getTargets());
targets.forEach(info -> 
    System.out.println("Target: " + info.getTitle() + " - " + info.getUrl()));

Target Discovery

Discovers and lists all available browser targets (tabs, windows, service workers).

/**
 * Get list of all available targets
 * @return Command that returns list of target information
 */
@Override
public Command<List<TargetInfo>> getTargets();

Usage Example:

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

for (TargetInfo info : targets) {
    System.out.printf("Target ID: %s%n", info.getTargetId());
    System.out.printf("  Type: %s%n", info.getType());
    System.out.printf("  Title: %s%n", info.getTitle());
    System.out.printf("  URL: %s%n", info.getUrl());
    System.out.printf("  Attached: %s%n", info.getAttached());
    
    if (info.getOpenerId().isPresent()) {
        System.out.printf("  Opener: %s%n", info.getOpenerId().get());
    }
    
    System.out.println("---");
}

Target Attachment

Attaches to specific targets to enable debugging and control.

/**
 * Attach to a specific target for debugging
 * @param targetId ID of the target to attach to
 * @return Command that returns SessionID for the attached session
 */
@Override
public Command<SessionID> attachToTarget(TargetID targetId);

Usage Example:

// Find a page target and attach to it
List<TargetInfo> targets = devTools.send(target.getTargets());
Optional<TargetInfo> pageTarget = targets.stream()
    .filter(info -> "page".equals(info.getType()))
    .filter(info -> info.getUrl().contains("example.com"))
    .findFirst();

if (pageTarget.isPresent()) {
    TargetID targetId = pageTarget.get().getTargetId();
    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 for target-specific operations
}

Target Detachment

Detaches from targets to release debugging connections.

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

Usage Example:

// Detach using session ID
SessionID sessionId = devTools.send(target.attachToTarget(targetId));
// ... do work with attached target ...
devTools.send(target.detachFromTarget(Optional.of(sessionId), Optional.empty()));

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

Auto-Attach Configuration

Configures automatic attachment to new targets as they are created.

/**
 * Set up automatic attachment to new targets
 * @return Command to enable auto-attach with flattening
 */
@Override
public Command<Void> setAutoAttach();

Usage Example:

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

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

// Now when new tabs/windows are created, they will be automatically
// attached for debugging without manual intervention
driver.executeScript("window.open('https://example.com', '_blank');");

Target Event Monitoring

Monitors target lifecycle events such as detachment.

/**
 * Stream of target detachment events
 * @return Event stream for when targets are detached
 */
@Override
public Event<TargetID> detached();

Usage Example:

// Monitor target lifecycle
devTools.addListener(target.detached(), targetId -> {
    System.out.println("Target detached: " + targetId);
    
    // Clean up any resources associated with this target
    cleanupTargetResources(targetId);
});

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

Complete Target Management Example

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v138.v138Target;
import org.openqa.selenium.devtools.idealized.target.model.*;
import java.util.*;

ChromeDriver driver = new ChromeDriver();
DevTools devTools = driver.getDevTools();
devTools.createSession();

v138Target target = new v138Target();

// Set up target monitoring
devTools.addListener(target.detached(), targetId -> {
    System.out.println("Target detached: " + targetId);
});

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

// Initial target discovery
System.out.println("=== Initial Targets ===");
List<TargetInfo> initialTargets = devTools.send(target.getTargets());
Map<TargetID, SessionID> attachedSessions = new HashMap<>();

for (TargetInfo info : initialTargets) {
    System.out.printf("%s: %s (%s)%n", 
        info.getType(), info.getTitle(), info.getUrl());
    
    // Attach to page targets
    if ("page".equals(info.getType()) && !info.getAttached()) {
        try {
            SessionID sessionId = devTools.send(target.attachToTarget(info.getTargetId()));
            attachedSessions.put(info.getTargetId(), sessionId);
            System.out.println("  → Attached with session: " + sessionId);
        } catch (Exception e) {
            System.out.println("  → Failed to attach: " + e.getMessage());
        }
    }
}

// Navigate and create new targets
System.out.println("\n=== Creating New Targets ===");
driver.get("https://example.com");

// Open new tab - should auto-attach due to setAutoAttach()
driver.executeScript("window.open('https://httpbin.org/get', '_blank');");

// Wait for new target creation
Thread.sleep(2000);

// Check updated target list
System.out.println("\n=== Updated Targets ===");
List<TargetInfo> updatedTargets = devTools.send(target.getTargets());
for (TargetInfo info : updatedTargets) {
    System.out.printf("%s: %s (%s) - Attached: %s%n", 
        info.getType(), info.getTitle(), info.getUrl(), info.getAttached());
}

// Demonstrate target-specific operations
Optional<TargetInfo> httpbinTarget = updatedTargets.stream()
    .filter(info -> info.getUrl().contains("httpbin.org"))
    .findFirst();

if (httpbinTarget.isPresent()) {
    TargetID httpbinId = httpbinTarget.get().getTargetId();
    
    if (!attachedSessions.containsKey(httpbinId)) {
        // Attach to the new target if not already attached
        SessionID sessionId = devTools.send(target.attachToTarget(httpbinId));
        attachedSessions.put(httpbinId, sessionId);
        System.out.println("Manually attached to httpbin target: " + sessionId);
    }
}

// Clean up - detach from all attached targets
System.out.println("\n=== Cleanup ===");
for (Map.Entry<TargetID, SessionID> entry : attachedSessions.entrySet()) {
    try {
        devTools.send(target.detachFromTarget(
            Optional.of(entry.getValue()), 
            Optional.of(entry.getKey())));
        System.out.println("Detached from target: " + entry.getKey());
    } catch (Exception e) {
        System.out.println("Failed to detach from " + entry.getKey() + ": " + e.getMessage());
    }
}

devTools.close();
driver.quit();

Types

// Target identification and session management
class TargetID {
    String toString();  // Unique target identifier
}

class SessionID {
    String toString();  // Unique session identifier for attached targets
}

// Target information structure
class TargetInfo {
    TargetID getTargetId();              // Unique target identifier
    String getType();                    // Target type ("page", "background_page", "service_worker", etc.)
    String getTitle();                   // Target title (page title, extension name, etc.)
    String getUrl();                     // Target URL
    Boolean getAttached();               // Whether target is currently attached for debugging
    Optional<TargetID> getOpenerId();    // ID of target that opened this target (for popups)
    Optional<BrowserContextID> getBrowserContextId(); // Browser context ID if applicable
}

// Browser context identification
class BrowserContextID {
    String toString();  // Browser context identifier
}

Install with Tessl CLI

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

docs

console-logging.md

domain-management.md

index.md

javascript-integration.md

network-operations.md

runtime-events.md

target-management.md

version-system.md

tile.json