Browser Automation

Reading, Writing, and Clearing Browser Storage

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")

.WithUseOttoMagic(@"C:OttoMagic");


IBrowser browser = GPAL.Browser

.WithBrowserType(BrowserType.Chrome)

.WithUseAutomationEngine(AutomationEngine.OttoMagic)

.ToGPALObject();


GPALUrl youtubeUrl = "www.youtube.com";


youtubeUrl.WithStorageType(WebsiteStorageType.cookie).WithStorageDomain("youtube.com").WithStorageKey("VISITOR_PRIVACY_METADATA");

youtubeUrl.WithStorageType(WebsiteStorageType.localStorage).WithStorageKey("ytidb::LAST_RESULT_ENTRY_KEY");


browser.GoTo(youtubeUrl).WaitFor(3_000);


string outData;


browser

.WithStorageDomain("youtube.com")

.WithStorageKey("VISITOR_PRIVACY_METADATA")

.RunGet(WebsiteStorageType.cookie)

.GetStorageData(out outData);


browser

.WithStorageDomain("youtube.com")

.WithStorageKey("VISITOR_PRIVACY_METADATA")

.WithStorageData("hello there")

.RunSet(WebsiteStorageType.cookie);


browser

.WithStorageDomain("youtube.com")

.WithStorageKey("VISITOR_PRIVACY_METADATA")

.RunDelete(WebsiteStorageType.cookie);


browser

.WithStorageKey("ytidb::LAST_RESULT_ENTRY_KEY")

.RunGet(WebsiteStorageType.localStorage)

.GetStorageData(out outData);


browser

.WithStorageKey("ytidb::LAST_RESULT_ENTRY_KEY")

.WithStorageData("{"data":{"hasSucceededOnce":false}}")

.RunSet(WebsiteStorageType.localStorage);


browser

.WithStorageKey("ytidb::LAST_RESULT_ENTRY_KEY")

.RunDelete(WebsiteStorageType.localStorage);


browser.Close(true);

GPALUrl Carries Storage Definitions

youtubeUrl is a GPALUrl - assign it from a string and it reads like one, but it can also carry storage definitions you attach before ever navigating. Each WithStorageType call describes what to look at later: a cookie named VISITOR_PRIVACY_METADATA on youtube.com, and a localStorage entry named ytidb::LAST_RESULT_ENTRY_KEY. Neither line performs an action yet, they just describe targets. Once GoTo makes the URL active, RunGet, RunSet, and RunDelete act on those definitions using WithStorageDomain and WithStorageKey to identify which one to operate on.

GPALUrl youtubeUrl = "www.youtube.com";


youtubeUrl.WithStorageType(WebsiteStorageType.cookie).WithStorageDomain("youtube.com").WithStorageKey("VISITOR_PRIVACY_METADATA");

youtubeUrl.WithStorageType(WebsiteStorageType.localStorage).WithStorageKey("ytidb::LAST_RESULT_ENTRY_KEY");


browser.GoTo(youtubeUrl).WaitFor(3_000);


string outData;


browser

.WithStorageDomain("youtube.com")

.WithStorageKey("VISITOR_PRIVACY_METADATA")

.RunGet(WebsiteStorageType.cookie)

.GetStorageData(out outData);


browser

.WithStorageDomain("youtube.com")

.WithStorageKey("VISITOR_PRIVACY_METADATA")

.WithStorageData("hello there")

.RunSet(WebsiteStorageType.cookie);


browser

.WithStorageDomain("youtube.com")

.WithStorageKey("VISITOR_PRIVACY_METADATA")

.RunDelete(WebsiteStorageType.cookie);

NOTE

Once you GoTo (or Get) a GPALUrl, it becomes the browser's active URL - and the storage definitions attached to it are what RunGet, RunSet, and RunDelete operate against for the rest of the workflow. See Value Objects: More Than a String.

RunGet Reads, GetStorageData Retrieves It

WithStorageDomain and WithStorageKey narrow which definition to act on - useful once a URL has more than one. RunGet(cookie) executes the read against the live browser, and GetStorageData(out outData) copies the result into a variable you can use however you like.

browser

.WithStorageDomain("youtube.com")

.WithStorageKey("VISITOR_PRIVACY_METADATA")

.RunGet(WebsiteStorageType.cookie)

.GetStorageData(out outData);

RunSet Writes, RunDelete Clears

RunSet and RunDelete follow the same shape as RunGet: set up the With* parameters that identify what you're targeting, supply WithStorageData for a write, then call the Run* verb for the storage type you're working with.

browser

.WithStorageDomain("youtube.com")

.WithStorageKey("VISITOR_PRIVACY_METADATA")

.WithStorageData("hello there")

.RunSet(WebsiteStorageType.cookie);


browser

.WithStorageDomain("youtube.com")

.WithStorageKey("VISITOR_PRIVACY_METADATA")

.RunDelete(WebsiteStorageType.cookie);

WARNING

Once a RunGet, RunSet, or RunDelete call executes, the With* parameters that scoped it are cleared. If your next Run* call needs the same domain, key, path, or store name, set them again with With* first - otherwise that call falls back to its broadest scope.

The Same Verbs Reach indexedDb Too

WebsiteStorageType has five values - cookie, localStorage, sessionStorage, cache, and indexedDb - and all five use the same WithStorageType / RunGet / RunSet / RunDelete / GetStorageData verbs. cache and indexedDb add WithStorageStoreName (and, for indexedDb, WithStoragePath for the database name) to address a specific store inside the browser's structured storage, but the pattern doesn't change.

youtubeUrl.WithStorageType(WebsiteStorageType.indexedDb)

.WithStoragePath("YtIdbMeta") // database name

.WithStorageStoreName("databases") // object store within that database

.WithStorageKey("ServiceWorkerLogsDatabase");


browser

.WithStoragePath("YtIdbMeta")

.WithStorageStoreName("databases")

.WithStorageKey("ServiceWorkerLogsDatabase")

.RunGet(WebsiteStorageType.indexedDb)

.GetStorageData(out outData);

NOTE

Whether you're clearing a single cookie or reading an entry out of a nested indexedDb object store, the workflow is always: describe it with WithStorageType and its With* parameters, then act on it with RunGet, RunSet, or RunDelete.

💬 Ask GPAL