Tutorials

Tutorials

Casting a Tab or Desktop to a Network Device

CastTab and CastDesktop drive Chrome's built-in cast feature to send a tab or the whole desktop to a network device such as a smart TV, then StopCasting ends the session. The workflow also shows the common (IBrowser)/(Browser) cast pattern of looping over AutomationEngine and skipping engines that don't support a given feature.

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)

.WithUseAutomationEngine(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 relies on Chrome's native cast menu, so this workflow uses BrowserType.Chrome with the Selenium automation engine - the engine that talks directly to a real chromedriver session capable of reaching Chrome's cast UI.

GPAL

.WithPublishToConsole()

.WithDriverLocation(@"C:drivers");

IBrowser browser = GPAL.Browser

.WithBrowserType(BrowserType.Chrome)

.WithUseAutomationEngine(AutomationEngine.Selenium)

.ToGPALObject();

CastTab Sends the Active Tab

GoTo loads the page you want to cast, CastTab("Living Room TV") opens Chrome's cast menu and casts the current tab to the named device, 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();

TIP

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).WithUseAutomationEngine(ae).ToGPALObject();

// cast workflow here

browser.Close(true);

}