CtrlK
BlogDocsLog inGet started
Tessl Logo

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

Chrome DevTools Protocol bindings for Selenium WebDriver version 111

Pending
Overview
Eval results
Files

target.mddocs/

Target Domain

Target domain implementation for browser target and session management. Enables control over browser tabs, windows, debugging sessions, and provides capabilities for multi-target debugging scenarios.

Capabilities

Target Attachment and Detachment

Attach to and detach from browser targets for debugging control.

public Command<SessionID> attachToTarget(TargetID targetId);
public Command<Void> detachFromTarget(Optional<SessionID> sessionId, Optional<TargetID> targetId);
public Event<TargetID> detached();

Usage example:

v111Target target = domains.target();

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

// Find a specific target (e.g., a specific tab)
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 for debugging
    SessionID sessionId = devTools.send(target.attachToTarget(targetId));
    System.out.println("Attached to target with session: " + sessionId);
    
    // Later detach when done
    devTools.send(target.detachFromTarget(Optional.of(sessionId), Optional.of(targetId)));
}

Target Discovery

Discover and list all available browser targets.

public Command<List<org.openqa.selenium.devtools.idealized.target.model.TargetInfo>> getTargets();

Usage example:

// 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());
    
    // Check if target has opener (popup/new window relationship)
    targetInfo.getOpenerId().ifPresent(openerId -> 
        System.out.println("Opened by: " + openerId));
    
    // Check browser context
    targetInfo.getBrowserContextId().ifPresent(contextId ->
        System.out.println("Browser context: " + contextId));
    
    System.out.println("---");
}

Auto-Attach Configuration

Configure automatic attachment to new targets.

public Command<Void> setAutoAttach();

Usage example:

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

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

Multi-Target Debugging Example

Complete example showing how to manage multiple browser targets:

public class MultiTargetManager {
    private final DevTools devTools;
    private final v111Target target;
    private final Map<TargetID, SessionID> activeSessions = new HashMap<>();
    private final Map<TargetID, TargetInfo> knownTargets = new HashMap<>();
    
    public void initializeTargetManagement() {
        // Enable auto-attach for new targets
        devTools.send(target.setAutoAttach());
        
        // Listen for target detachment
        devTools.addListener(target.detached(), this::handleTargetDetached);
        
        // Discover existing targets
        refreshTargetList();
    }
    
    public void refreshTargetList() {
        List<TargetInfo> targets = devTools.send(target.getTargets());
        
        for (TargetInfo targetInfo : targets) {
            knownTargets.put(targetInfo.getTargetId(), targetInfo);
            
            // Auto-attach to page targets
            if ("page".equals(targetInfo.getType()) && !targetInfo.getAttached()) {
                attachToTarget(targetInfo.getTargetId());
            }
        }
    }
    
    public void attachToTarget(TargetID targetId) {
        try {
            SessionID sessionId = devTools.send(target.attachToTarget(targetId));
            activeSessions.put(targetId, sessionId);
            
            TargetInfo targetInfo = knownTargets.get(targetId);
            System.out.println("Attached to target: " + targetInfo.getTitle());
            
            // Configure attached target (enable domains, set up listeners, etc.)
            configureAttachedTarget(targetId, sessionId);
            
        } catch (Exception e) {
            System.err.println("Failed to attach to target " + targetId + ": " + e.getMessage());
        }
    }
    
    private void configureAttachedTarget(TargetID targetId, SessionID sessionId) {
        // Configure domains for this specific target
        // Note: You would typically create separate domain instances per target
        
        TargetInfo targetInfo = knownTargets.get(targetId);
        System.out.println("Configuring target: " + targetInfo.getUrl());
        
        // Example: Enable network monitoring for this target
        // devTools.send(sessionId, networkDomain.enableFetchForAllPatterns());
    }
    
    private void handleTargetDetached(TargetID targetId) {
        SessionID sessionId = activeSessions.remove(targetId);
        TargetInfo targetInfo = knownTargets.remove(targetId);
        
        if (targetInfo != null) {
            System.out.println("Target detached: " + targetInfo.getTitle());
        }
        
        // Clean up resources for this target
        cleanupTargetResources(targetId, sessionId);
    }
    
    public void detachFromTarget(TargetID targetId) {
        SessionID sessionId = activeSessions.get(targetId);
        if (sessionId != null) {
            devTools.send(target.detachFromTarget(Optional.of(sessionId), Optional.of(targetId)));
        }
    }
    
    public void detachFromAllTargets() {
        for (Map.Entry<TargetID, SessionID> entry : activeSessions.entrySet()) {
            devTools.send(target.detachFromTarget(
                Optional.of(entry.getValue()), 
                Optional.of(entry.getKey())
            ));
        }
        activeSessions.clear();
    }
    
    public List<TargetInfo> getPageTargets() {
        return knownTargets.values().stream()
                          .filter(t -> "page".equals(t.getType()))
                          .collect(Collectors.toList());
    }
    
    public List<TargetInfo> getWorkerTargets() {
        return knownTargets.values().stream()
                          .filter(t -> t.getType().contains("worker"))
                          .collect(Collectors.toList());
    }
}

Types

Target Information

class TargetInfo {
    public TargetInfo(TargetID targetId, String type, String title, String url, 
                     boolean attached, Optional<TargetID> openerId, 
                     Optional<BrowserContextID> browserContextId);
    
    public TargetID getTargetId();                        // Unique target identifier
    public String getType();                              // Target type: "page", "background_page", "service_worker", etc.
    public String getTitle();                             // Target title (page title for pages)
    public String getUrl();                               // Target URL
    public boolean getAttached();                         // Whether target is currently attached for debugging
    public Optional<TargetID> getOpenerId();              // ID of target that opened this target (for popups)
    public Optional<BrowserContextID> getBrowserContextId(); // Browser context (for incognito/profile separation)
}

Session and Context Types

class TargetID {
    public TargetID(String id);
    public String toString();    // Get the string representation of the target ID
}

class SessionID {
    public SessionID(String id);
    public String toString();    // Get the string representation of the session ID  
}

class BrowserContextID {
    public BrowserContextID(String id);
    public String toString();    // Get the string representation of the browser context ID
}

Target Types

Common target types you'll encounter:

  • page: Regular web pages and tabs
  • background_page: Extension background pages
  • service_worker: Service worker instances
  • shared_worker: Shared worker instances
  • browser: Browser-level target
  • other: Other specialized targets

Target Relationships

Targets can have relationships through the openerId field:

// Find all targets opened by a specific target
TargetID parentId = new TargetID("parent-target-id");
List<TargetInfo> childTargets = targets.stream()
    .filter(t -> t.getOpenerId().map(id -> id.equals(parentId)).orElse(false))
    .collect(Collectors.toList());

// Find the parent of a target
Optional<TargetInfo> parent = targets.stream()
    .filter(t -> childTarget.getOpenerId().map(id -> id.equals(t.getTargetId())).orElse(false))
    .findFirst();

Browser Context Management

Browser contexts provide isolation similar to incognito windows:

// Group targets by browser context
Map<BrowserContextID, List<TargetInfo>> targetsByContext = targets.stream()
    .filter(t -> t.getBrowserContextId().isPresent())
    .collect(Collectors.groupingBy(t -> t.getBrowserContextId().get()));

// Find targets in default context (no browser context ID)
List<TargetInfo> defaultContextTargets = targets.stream()
    .filter(t -> t.getBrowserContextId().isEmpty())
    .collect(Collectors.toList());

Install with Tessl CLI

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

docs

events.md

index.md

javascript.md

logging.md

network.md

target.md

tile.json