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.
—
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.
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 + ")");
}Select a Cast device as the target for media operations.
public void selectCastSink(String deviceName);Parameters:
deviceName (String): Name of the target Cast deviceUsage 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);
}Mirror the entire desktop to a Cast device.
public void startDesktopMirroring(String deviceName);Parameters:
deviceName (String): Name of the target Cast deviceUsage 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 deviceMirror the current browser tab to a Cast device.
public void startTabMirroring(String deviceName);Parameters:
deviceName (String): Name of the target Cast deviceUsage 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 deviceStop active casting session to a specific device.
public void stopCasting(String deviceName);Parameters:
deviceName (String): Name of the Cast device to stop casting toUsage 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"));
}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");
}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();
}
}
}// 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";// 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)// 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");Cast functionality requires:
Common error conditions:
// 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;
}
}Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-chromium-driver