Integrations

Casting a Tab or Desktop to a Network Device

Complete Program

Here's the whole workflow, start to finish. Each piece is broken down and explained below.

using GenerallyPositive;

using GenerallyPositive.Browser;

using static GenerallyPositive.Enums;


GPAL

.WithPublishToConsole()

.WithDriverLocation("C:\drivers\");


IBrowser browser = GPAL.Browser

.WithBrowserType(BrowserType.Chrome)

.WithAutomationEngine(AutomationEngine.Selenium)

.ToGPALObject();


browser

.GoTo("https://www.youtube.com/watch?v=example")

.CastTab("Living Room TV")

.WaitFor(30_000)

.StopCasting();


browser

.GoTo("https://www.youtube.com/watch?v=example")

.CastDesktop("Living Room TV")

.WaitFor(30_000);


browser.Close(true);

Configure GPAL and Launch the Browser

Casting uses Chrome's DevTools Cast commands, so this workflow uses BrowserType.Chrome with the Selenium automation engine. Selenium sends Cast.enable through the chromedriver session, then starts mirroring to the device by name; no cast menu or cast UI is involved. Puppeteer sends the same commands. OttoMagic cannot cast and publishes a WARNING instead.

GPAL

.WithPublishToConsole()

.WithDriverLocation("C:\drivers\");


IBrowser browser = GPAL.Browser

.WithBrowserType(BrowserType.Chrome)

.WithAutomationEngine(AutomationEngine.Selenium)

.ToGPALObject();

CastTab Sends the Active Tab

GoTo loads the page you want to cast, CastTab("Living Room TV") asks Chrome to mirror the current tab to the named device, trying up to 10 times, 3 seconds apart, until the device is found, WaitFor gives the cast time to play, and StopCasting ends the cast session cleanly.

browser

.GoTo("https://www.youtube.com/watch?v=example")

.CastTab("Living Room TV")

.WaitFor(30_000)

.StopCasting();

NOTE

The string passed to CastTab and CastDesktop must match the device name as it appears in Chrome's cast device list - check the device's display name on your network before hardcoding it.

CastDesktop Sends the Whole Screen

CastDesktop works the same way as CastTab but casts the entire desktop instead of just the tab. This is useful for content that doesn't play nicely inside a single browser tab, at the cost of casting everything visible on screen.

browser

.GoTo("https://www.youtube.com/watch?v=example")

.CastDesktop("Living Room TV")

.WaitFor(30_000);


browser.Close(true);

WARNING

Unlike CastTab, CastDesktop shares whatever is visible on the monitor for the duration of the cast - close sensitive windows before running this in an unattended environment.

Looping Over Automation Engines

The real CastTest program loops over every AutomationEngine value and uses a switch with continue to skip engines that don't support cast operations, running the cast workflow only for engines like Selenium and PuppeteerPort that do.

Array automationEngines = Enum.GetValues(typeof(AutomationEngine));


foreach (AutomationEngine ae in automationEngines)

{

switch (ae)

{

case AutomationEngine.Selenium:

case AutomationEngine.PuppeteerPort:

break;


default:

continue;

}


IBrowser browser = GPAL.Browser.WithBrowserType(BrowserType.Chrome).WithAutomationEngine(ae).ToGPALObject();


// cast workflow here


browser.Close(true);

}

💬 Ask GPAL