Tutorials

Tutorials

Driving a Live Browser Through GPAL.OttoMagicClient

GPAL.OttoMagicClient is a ready-made IRESTClient wired to the OttoMagic REST API. Every browser action - navigation, clicks, scrolling, window management, storage, and more - is just an endpoint call away. This tutorial walks through opening tabs, driving the page, and reading results back with Execute<T>().

Complete Program

This is a trimmed version of the actual test program. It launches a browser, gets GPAL.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.WithUseOttoMagic(@"C:OttoMagic").WithPublishToConsole();

IBrowser browser = GPAL.Browser

.WithBrowserType(BrowserType.Chrome)

.WithUseAutomationEngine(AutomationEngine.OttoMagic)

.WithDriverLocation(@"C:drivers")

.ToGPALObject();

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

IRESTClient client = GPAL.OttoMagicClient

.WithName("OttoMagicClient")

.ToGPALObject();

// 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

GPAL.OttoMagicClient saves you from setting WithAPIBase yourself - it is already pointed at GPAL.OttoMagicRestApiBaseUrl, 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 = GPAL.OttoMagicClient

.WithName("OttoMagicClient")

.ToGPALObject();

TIP

ToGPALObject() is only called once. From here every endpoint call is a fluent chain off the same client object - there's no need to re-fetch GPAL.OttoMagicClient 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();

TIP

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.