Tutorials

Tutorials

Chaining Browser Actions Through GPAL.RESTClient with AndThen

GPAL.RESTClient is the general-purpose fluent REST client behind GPAL.OttoMagicClient. Point it at any API base, pick an ApiEndpoint, and chain calls together with AndThen<T>() - each step's result can be named and reused by a later step, building a multi-step browser workflow out of individual REST calls.

Complete Program

This is a trimmed version of the actual test program. It builds a RESTClient pointed at the OttoMagic REST API, reads a product name off the page, types it into a search box, reads the resulting URL, and navigates to it - each step feeding the next through named results.

using System;

using GenerallyPositive;

using static GenerallyPositive.Enums;

GPAL.WithUseOttoMagic(@"C:OttoMagic").WithPublishToConsole();

RESTClient client = GPAL.RESTClient

.WithAPIBase(GPAL.OttoMagicRestApiBaseUrl)

.WithName("ChainClient")

.WithEndpoint(ApiEndpoint.Evaluate)

.WithXPath("//div[@class='product-name']")

.WithResultName("productName")

.AndThen<string>() // Result 0: "Laptop"

.WithEndpoint(ApiEndpoint.FillInOverwrite)

.WithXPath("//input[@id='search']")

.WithTextFromResult("productName") // Use Result "productName": "Laptop"

.WithResultName("inputResult")

.AndThen() // Submits "Laptop" to the input field

.WithEndpoint(ApiEndpoint.GetCurrentUrl)

.WithResultName("currentUrl")

.AndThen<string>() // Result 1: "http://localhost:3000/search?q=Laptop"

.WithEndpoint(ApiEndpoint.Goto)

.WithUrlFromResult("currentUrl") // Use Result "currentUrl"

.AndThen(); // Navigates to the URL

var results = client.GetExecutionResults();

Console.WriteLine($"Product Name (Result 0): {results[0]}");

Console.WriteLine($"Current URL (Result 1): {results[1]}");

GPAL.RESTClient: The General-Purpose Client

WithAPIBase points the client at any REST API base URL - here, GPAL.OttoMagicRestApiBaseUrl, the local OttoMagic server. WithName labels the client for logging. From here you configure each call with WithEndpoint and whatever With* parameters that endpoint needs.

RESTClient client = GPAL.RESTClient

.WithAPIBase(GPAL.OttoMagicRestApiBaseUrl)

.WithName("ChainClient")

TIP

GPAL.OttoMagicClient skips the WithAPIBase call by pointing at GPAL.OttoMagicRestApiBaseUrl automatically, but it's the same RESTClient underneath. Anything you can do with one, you can do with the other.

AndThen<T>() Runs the Call and Names the Result

WithEndpoint picks the operation - Evaluate runs an XPath lookup. WithXPath supplies the path, WithResultName labels the outcome "productName", and AndThen<string>() executes the call, returning a string and storing it both by name and by ordinal position (Result 0).

client

.WithEndpoint(ApiEndpoint.Evaluate)

.WithXPath("//div[@class='product-name']")

.WithResultName("productName")

.AndThen<string>(); // Result 0: "Laptop"

Feed One Result Into the Next Call

WithTextFromResult("productName") pulls the value stored under that name from the previous step and uses it as the text for FillInOverwrite. AndThen() without a type parameter runs the call without expecting a typed return - useful for actions like filling in a field where you don't need the result value itself.

client

.WithEndpoint(ApiEndpoint.FillInOverwrite)

.WithXPath("//input[@id='search']")

.WithTextFromResult("productName") // Use Result "productName": "Laptop"

.WithResultName("inputResult")

.AndThen(); // Submits "Laptop" to the input field

TIP

Results can be looked up by the name given to WithResultName, or by their ordinal position in GetExecutionResults() (Result 0, Result 1, ...). Mixing both in the same chain is fine - use whichever is clearer for a given step.

Read the URL Back, Then Navigate to It

GetCurrentUrl reads the page's current URL into "currentUrl". The final step uses WithUrlFromResult to plug that value into a Goto call - the chain ends by navigating using a value the workflow itself produced two steps earlier.

client

.WithEndpoint(ApiEndpoint.GetCurrentUrl)

.WithResultName("currentUrl")

.AndThen<string>() // Result 1

.WithEndpoint(ApiEndpoint.Goto)

.WithUrlFromResult("currentUrl")

.AndThen();

var results = client.GetExecutionResults();

Console.WriteLine($"Current URL (Result 1): {results[1]}");