CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-seleniumhq-selenium--selenium-chromium-driver

Selenium Chrome Driver provides WebDriver implementation for Google Chrome with Chrome DevTools Protocol support, network conditions simulation, permissions management, media casting capabilities, and Chrome-specific service configuration.

Pending
Overview
Eval results
Files

media-casting.mddocs/

Media Casting

Media casting capabilities allow applications to discover, connect to, and stream content to Cast-enabled devices such as Chromecast, Android TV, and other Cast receivers.

Cast Device Discovery

Get Available Cast Sinks

Retrieve list of available Cast devices on the network.

public List<Map<String, String>> getCastSinks();

Returns: List<Map<String, String>> - List of available cast devices with ID and name pairs

Usage Example:

// ChromiumDriver is typically used through subclasses like ChromeDriver, EdgeDriver
ChromiumDriver driver = ...; // Obtain from ChromeDriver, EdgeDriver, etc.

// Get available cast devices
List<Map<String, String>> castSinks = driver.getCastSinks();
for (Map<String, String> sink : castSinks) {
    String deviceId = sink.get("id");
    String deviceName = sink.get("name");
    System.out.println("Found device: " + deviceName + " (ID: " + deviceId + ")");
}

Cast Device Selection

Select Cast Sink

Select a Cast device as the target for media operations.

public void selectCastSink(String deviceName);

Parameters:

  • deviceName (String): Name of the target Cast device

Usage Example:

// Select a specific cast device
driver.selectCastSink("Living Room TV");

// Or select using device discovered from getCastSinks()
List<Map<String, String>> sinks = driver.getCastSinks();
if (!sinks.isEmpty()) {
    String firstDevice = sinks.get(0).get("name");
    driver.selectCastSink(firstDevice);
}

Screen Mirroring

Start Desktop Mirroring

Mirror the entire desktop to a Cast device.

public void startDesktopMirroring(String deviceName);

Parameters:

  • deviceName (String): Name of the target Cast device

Usage Example:

// Start mirroring desktop to Cast device
driver.startDesktopMirroring("Living Room TV");

// Application continues running while desktop is mirrored
// User can see desktop content on the Cast device

Start Tab Mirroring

Mirror the current browser tab to a Cast device.

public void startTabMirroring(String deviceName);

Parameters:

  • deviceName (String): Name of the target Cast device

Usage Example:

// Navigate to content to cast
driver.get("https://video-streaming-site.com");

// Start mirroring current tab
driver.startTabMirroring("Bedroom Chromecast");

// The current tab content will be displayed on the Cast device

Stop Casting

Stop active casting session to a specific device.

public void stopCasting(String deviceName);

Parameters:

  • deviceName (String): Name of the Cast device to stop casting to

Usage Example:

// Stop casting to specific device
driver.stopCasting("Living Room TV");

// Or stop all active casting sessions
List<Map<String, String>> sinks = driver.getCastSinks();
for (Map<String, String> sink : sinks) {
    driver.stopCasting(sink.get("name"));
}

Error Handling and Monitoring

Get Cast Issue Message

Retrieve any error messages from active Cast sessions.

public String getCastIssueMessage();

Returns: String - Error message if there are Cast session issues, empty string otherwise

Usage Example:

// Check for casting issues
String issueMessage = driver.getCastIssueMessage();
if (!issueMessage.isEmpty()) {
    System.err.println("Cast error: " + issueMessage);
    
    // Handle the error (retry, select different device, etc.)
    driver.stopCasting("Problem Device");
}

Complete Casting Workflow

import org.openqa.selenium.chromium.ChromiumDriver;
import java.util.List;
import java.util.Map;

public class CastingExample {
    public void demonstrateCasting() {
        ChromiumDriver driver = new ChromiumDriver(commandExecutor, capabilities, "chrome");
        
        try {
            // Discover available Cast devices
            List<Map<String, String>> devices = driver.getCastSinks();
            if (devices.isEmpty()) {
                System.out.println("No Cast devices found");
                return;
            }
            
            // Display available devices
            System.out.println("Available Cast devices:");
            for (int i = 0; i < devices.size(); i++) {
                Map<String, String> device = devices.get(i);
                System.out.println((i + 1) + ". " + device.get("name"));
            }
            
            // Select first available device
            String deviceName = devices.get(0).get("name");
            driver.selectCastSink(deviceName);
            
            // Navigate to content
            driver.get("https://video-site.com/video/12345");
            
            // Start tab mirroring
            driver.startTabMirroring(deviceName);
            
            // Monitor for issues
            Thread.sleep(5000); // Wait for casting to establish
            String issues = driver.getCastIssueMessage();
            if (!issues.isEmpty()) {
                System.err.println("Casting issues: " + issues);
            }
            
            // Continue with test or user interaction
            // ...
            
            // Stop casting when done
            driver.stopCasting(deviceName);
            
        } catch (Exception e) {
            System.err.println("Casting failed: " + e.getMessage());
        } finally {
            driver.quit();
        }
    }
}

Cast Command Constants

// Command constants from AddHasCasting
public static final String GET_CAST_SINKS = "getCastSinks";
public static final String SET_CAST_SINK_TO_USE = "selectCastSink"; 
public static final String START_CAST_TAB_MIRRORING = "startCastTabMirroring";
public static final String START_CAST_DESKTOP_MIRRORING = "startDesktopMirroring";
public static final String GET_CAST_ISSUE_MESSAGE = "getCastIssueMessage";
public static final String STOP_CASTING = "stopCasting";

Testing Cast-Enabled Applications

Test Cast Button Functionality

// Navigate to a Cast-enabled web application
driver.get("https://youtube.com/watch?v=example");

// Verify cast button is present
WebElement castButton = driver.findElement(By.className("ytp-cast-button"));
assertTrue(castButton.isDisplayed());

// Get available devices before clicking
List<Map<String, String>> devicesBeforeClick = driver.getCastSinks();

// Click cast button in application
castButton.click();

// Verify application shows available devices
// (Application-specific verification logic)

Test Cast Session Management

// Start casting
driver.selectCastSink("Test Device");
driver.startTabMirroring("Test Device");

// Verify no error messages
String issues = driver.getCastIssueMessage();
assertEquals("", issues);

// Navigate to different content while casting
driver.get("https://another-video-site.com");

// Verify casting continues (no new error messages)
issues = driver.getCastIssueMessage();
assertEquals("", issues);

// Stop casting and verify
driver.stopCasting("Test Device");

Network Requirements

Cast functionality requires:

  1. Network Discovery: Chrome must be able to discover Cast devices on the local network
  2. Multicast DNS: mDNS must be enabled for device discovery
  3. Network Permissions: Appropriate network access permissions
  4. Same Network: Cast devices and Chrome must be on the same network segment

Limitations and Considerations

  1. Null Handling: Methods return empty collections/strings when casting is not available
  2. Device Availability: Cast devices may appear and disappear based on network conditions
  3. Session Management: Only one casting session per device is typically supported
  4. Content Restrictions: Some content may have casting restrictions (DRM, etc.)
  5. Network Dependencies: Casting requires stable network connectivity
  6. Chrome Version: Casting features may vary between Chrome versions

Error Scenarios

Common error conditions:

  • No Devices Found: Network issues or no Cast devices on network
  • Device Unavailable: Selected device goes offline or becomes unavailable
  • Content Restrictions: Content provider blocks casting
  • Network Issues: Intermittent connectivity affects casting quality
// Robust casting with error handling
public boolean safeCast(ChromiumDriver driver, String deviceName, String url) {
    try {
        // Verify device is available
        List<Map<String, String>> devices = driver.getCastSinks();
        boolean deviceFound = devices.stream()
            .anyMatch(device -> deviceName.equals(device.get("name")));
        
        if (!deviceFound) {
            System.err.println("Device not found: " + deviceName);
            return false;
        }
        
        // Navigate and start casting
        driver.get(url);
        driver.selectCastSink(deviceName);
        driver.startTabMirroring(deviceName);
        
        // Check for immediate issues
        Thread.sleep(2000);
        String issues = driver.getCastIssueMessage();
        if (!issues.isEmpty()) {
            System.err.println("Cast issues: " + issues);
            driver.stopCasting(deviceName);
            return false;
        }
        
        return true;
        
    } catch (Exception e) {
        System.err.println("Casting error: " + e.getMessage());
        try {
            driver.stopCasting(deviceName);
        } catch (Exception stopError) {
            // Ignore stop errors
        }
        return false;
    }
}

Best Practices

  1. Device Verification: Always verify devices are available before attempting to cast
  2. Error Monitoring: Regularly check for cast issues during sessions
  3. Graceful Cleanup: Always stop casting sessions when tests complete
  4. Network Stability: Ensure stable network conditions for reliable casting
  5. Content Compatibility: Verify content supports casting before starting sessions
  6. Session Management: Handle multiple device scenarios appropriately
  7. User Experience: Consider the user experience on both the browser and Cast device

Install with Tessl CLI

npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-chromium-driver

docs

application-launching.md

browser-configuration.md

devtools-protocol.md

index.md

media-casting.md

network-conditions.md

permissions-management.md

service-management.md

webdriver-operations.md

tile.json