Automation Engines

Driving a Live Browser Through the OttoMagic REST Client

Complete Program

This is a trimmed version of the actual test program. It launches a browser, gets browser.OttoMagicClient, opens a few tabs to set up a session, then drives the page through a series of endpoint calls.

using GenerallyPositive;

using GenerallyPositive.Browser;

using static GenerallyPositive.Enums;


GPAL.WithAutomationEngine(AutomationEngine.OttoMagic).WithPublishToConsole();


IBrowser browser = GPAL.Browser

.WithBrowserType(BrowserType.Chrome)

.WithAutomationEngine(AutomationEngine.OttoMagic)

.WithDriverLocation(@"C:drivers")

.ToGPALObject();


browser.GoTo(""); // launches with the default home page


IRESTClient client = browser.OttoMagicClient;


// Set up the session

client.GoTo("https://example.com").Execute();

client.CheckNetworkIdle().Execute();

client.NewTab("https://example.com").Execute();

client.CheckNetworkIdle().Execute();


// Drive the page

dynamic result = client.GetCurrentUrl().Execute<string>();

result = client.IsEndOfPage().Execute<bool>();

result = client.LeftClick("#login-button").Execute();

result = client.FillInOverwrite("#search-input").WithText("hello world").Execute();

result = client.ScrollWindow(0, 400).Execute();

result = client.Maximize().Execute();


browser.Close(true);

OttoMagicClient: A Pre-Wired REST Client

browser.OttoMagicClient saves you from setting WithAPIBase yourself - it is already pointed at that browser's RestApiBaseUrl, the local OttoMagic REST API server. Calling it returns the same fluent IRESTClient you'd get from GPAL.RESTClient, just pre-configured. Give it a WithName so any logged errors are easy to trace back to this client.

IRESTClient client = browser.OttoMagicClient;

NOTE

The client is fetched once. From here every endpoint call is a fluent chain off that same object, and there is no need to reach for browser.OttoMagicClient again between calls.

Setting Up the Session: Tabs and Network Idle

GoTo navigates the current tab, NewTab opens another, and CheckNetworkIdle waits until the page settles before the next action runs. This mirrors how a real workflow primes multiple tabs before doing any scraping or interaction.

client.GoTo("https://example.com").Execute();

client.CheckNetworkIdle().Execute();

client.NewTab("https://example.com").Execute();

client.CheckNetworkIdle().Execute();

Every Browser Action Is an Endpoint Call

Reading the page, clicking, typing, and scrolling all follow the same shape: call the endpoint method, optionally chain a With* parameter, then call Execute() (or Execute<T>() when you want a typed result back). GetCurrentUrl and IsEndOfPage return values; LeftClick, FillInOverwrite, ScrollWindow, and Maximize perform actions.

string currentUrl = client.GetCurrentUrl().Execute<string>();

bool atEnd = client.IsEndOfPage().Execute<bool>();


client.LeftClick("#login-button").Execute();

client.FillInOverwrite("#search-input").WithText("hello world").Execute();

client.ScrollWindow(0, 400).Execute();

client.Maximize().Execute();

NOTE

The real test program exercises every value of the ApiEndpoint enum this way - Back, Forward, Hover, RightClick, GetAttribute, WindowInnerWidth, and dozens more. They all follow the identical call-then-Execute pattern shown here.

Closing Down

Once the workflow is finished, close the browser. Passing true tells GPAL to stop the underlying automation engine entirely, not just the tab.

browser.Close(true);

WARNING

Passing true kills the running automation engine process. If other code in the same run still needs the browser, pass false instead.

💬 Ask GPAL