Table of Contents

Quick Reference

A cheat sheet of common WebDriverBiDi.NET commands and patterns.

Driver Lifecycle

Operation Code
Create driver BiDiDriver driver = new BiDiDriver(TimeSpan.FromSeconds(30));
Start connection await driver.StartAsync("ws://localhost:9515/session/YOUR-SESSION-ID"); (the webSocketUrl from a chromedriver/geckodriver session; see Browser Setup)
Check if started bool isStarted = driver.IsStarted;
Stop connection await driver.StopAsync();
Dispose await driver.DisposeAsync();

Session

Operation Code
Check status await driver.Session.StatusAsync();
Subscribe to events SubscribeCommandParameters sub = new SubscribeCommandParameters(driver.Network.OnBeforeRequestSent.EventName); await driver.Session.SubscribeAsync(sub);
End session await driver.Session.EndAsync();

Browsing Context

Operation Code
Get context tree GetTreeCommandResult tree = await driver.BrowsingContext.GetTreeAsync();
Create context CreateCommandResult ctx = await driver.BrowsingContext.CreateAsync(new CreateCommandParameters(CreateType.Tab));
Navigate await driver.BrowsingContext.NavigateAsync(new NavigateCommandParameters(contextId, url) { Wait = ReadinessState.Complete });
Close context await driver.BrowsingContext.CloseAsync(new CloseCommandParameters(contextId));
Capture screenshot await driver.BrowsingContext.CaptureScreenshotAsync(new CaptureScreenshotCommandParameters(contextId));

Script

Operation Code
Evaluate expression EvaluateResult r = await driver.Script.EvaluateAsync(new EvaluateCommandParameters(expression, new ContextTarget(contextId), true));
Call function EvaluateResult r = await driver.Script.CallFunctionAsync(new CallFunctionCommandParameters(functionDeclaration, new ContextTarget(contextId), true));
Add preload script AddPreloadScriptCommandResult r = await driver.Script.AddPreloadScriptAsync(new AddPreloadScriptCommandParameters(script));
Get realms GetRealmsCommandResult realms = await driver.Script.GetRealmsAsync();

Network

Operation Code
Add intercept AddInterceptCommandParameters p = new AddInterceptCommandParameters(InterceptPhase.BeforeRequestSent); await driver.Network.AddInterceptAsync(p);
Add data collector AddDataCollectorCommandParameters p = new AddDataCollectorCommandParameters(1024 * 1024, DataType.Response); p.Contexts.Add(contextId); await driver.Network.AddDataCollectorAsync(p);
Continue request await driver.Network.ContinueRequestAsync(new ContinueRequestCommandParameters(requestId));
Provide response ProvideResponseCommandParameters p = new ProvideResponseCommandParameters(requestId) { Body = BytesValue.FromString(body) }; await driver.Network.ProvideResponseAsync(p);

Storage

Operation Code
Get cookies GetCookiesCommandParameters p = new GetCookiesCommandParameters(); p.Partition = new BrowsingContextPartitionDescriptor(contextId); GetCookiesCommandResult r = await driver.Storage.GetCookiesAsync(p);
Set cookie await driver.Storage.SetCookieAsync(new SetCookieCommandParameters(new PartialCookie(name, BytesValue.FromString(value), domain)));
Delete cookies DeleteCookiesCommandParameters p = new DeleteCookiesCommandParameters(); p.Partition = new BrowsingContextPartitionDescriptor(contextId); await driver.Storage.DeleteCookiesAsync(p);

Input

Operation Code
Perform actions PerformActionsCommandParameters p = new PerformActionsCommandParameters(contextId); PointerSourceActions mouse = new PointerSourceActions(); mouse.Actions.Add(new PointerMoveAction { X = 100, Y = 100 }); p.Actions.Add(mouse); await driver.Input.PerformActionsAsync(p);

Digital Credentials

Operation Code
Decline credential request await driver.DigitalCredentials.SetVirtualWalletBehaviorAsync(new SetVirtualWalletBehaviorCommandParameters(VirtualWalletAction.Decline));
Respond with credential SetVirtualWalletBehaviorCommandParameters p = new SetVirtualWalletBehaviorCommandParameters(VirtualWalletAction.Respond) { Protocol = "openid4vp-v1-unsigned", Response = credentialResponse }; await driver.DigitalCredentials.SetVirtualWalletBehaviorAsync(p);
Leave request pending await driver.DigitalCredentials.SetVirtualWalletBehaviorAsync(new SetVirtualWalletBehaviorCommandParameters(VirtualWalletAction.Wait));
Clear wallet behavior await driver.DigitalCredentials.SetVirtualWalletBehaviorAsync(new SetVirtualWalletBehaviorCommandParameters(VirtualWalletAction.Clear));

Event Subscription Pattern

// 1. Add observer
driver.Network.OnBeforeRequestSent.AddObserver((e) => Console.WriteLine(e.Request.Url));

// 2. Subscribe via session
SubscribeCommandParameters sub =
    new SubscribeCommandParameters(driver.Network.OnBeforeRequestSent.EventName);
await driver.Session.SubscribeAsync(sub);

Error Configuration

// Fail fast during development
driver.TransportConfiguration.EventHandlerExceptionBehavior = TransportErrorBehavior.Terminate;
driver.TransportConfiguration.ProtocolErrorBehavior = TransportErrorBehavior.Terminate;

// Collect for debugging
driver.TransportConfiguration.EventHandlerExceptionBehavior = TransportErrorBehavior.Collect;
driver.TransportConfiguration.ProtocolErrorBehavior = TransportErrorBehavior.Collect;

See Also