WebAppBrowser Class

A browser connected to a web application: an ASP.NET Core application in memory (TestServer, for instance WebApplicationFactory.Server), or any server. Its pages share their cookies.

public sealed class WebAppBrowser : IAsyncDisposable, IDisposable

Example

// The application runs in memory: Program is the entry point of the application under test
await using var factory = new WebApplicationFactory<Program>();
await using var browser = WebAppBrowser.Create(factory.Server);
var page = await browser.OpenAsync("/");
await Expect(page).ToHaveTitleAsync("Home");

// Follow a link, fill a form and submit it: the POST, the redirect and the cookie go through the application
await page.GetByRole("link", "Sign in").ClickAsync();
await page.GetByLabel("Name").FillAsync("Ada");
await page.GetByRole("button", "Sign in").ClickAsync();

await Expect(page).ToHaveUrlAsync("/profile");
await Expect(page.Locator("#greeting")).ToHaveTextAsync("Hello Ada");
Implements

Constructors

NameDescription
WebAppBrowser(Uri, DocumentLoader, WebAppBrowserOptions?)Initializes a new instance of the WebAppBrowser class.

WebAppBrowser(Uri, DocumentLoader, WebAppBrowserOptions?)

WebAppBrowser.cs

Initializes a new instance of the WebAppBrowser class.

public WebAppBrowser(Uri baseAddress, DocumentLoader loader, WebAppBrowserOptions? options = null)

Parameters

baseAddress Uri
The URL that the relative URLs of WebAppBrowser.OpenAsync are resolved against.
loader DocumentLoader
The loader of the requests of the pages (see TestDocumentLoaders).
options WebAppBrowserOptions?
The options, or null for the default options.

Properties

NameDescription
BaseAddressGets the URL that the relative URLs of WebAppBrowser.OpenAsync are resolved against.
CookiesGets the cookies of the pages.
PagesGets the open pages.

BaseAddress

WebAppBrowser.cs

Gets the URL that the relative URLs of WebAppBrowser.OpenAsync are resolved against.

public Uri BaseAddress { get; }

Property value

Uri

Gets the cookies of the pages.

public CookieJar Cookies { get; }

Property value

CookieJar

Gets the open pages.

public IReadOnlyList<BrowserPage> Pages { get; }

Methods

NameDescription
Create(TestServer, WebAppBrowserOptions?)Creates a browser connected to an ASP.NET Core application in memory.
Create(Uri, WebAppBrowserOptions?)Creates a browser connected to a server over the network (HTTP).
Dispose()Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
DisposeAsync()Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
NewPage()Opens a new page, on about:blank.
OpenAsync(string, CancellationToken)Opens a new page and navigates it to a URL, then waits until the document is loaded.

Create overloads

Create(TestServer, WebAppBrowserOptions?)

WebAppBrowser.cs

Creates a browser connected to an ASP.NET Core application in memory.

public static WebAppBrowser Create(TestServer server, WebAppBrowserOptions? options = null)
Parameters
server TestServer
The test server (WebApplicationFactory.Server).
options WebAppBrowserOptions?
The options, or null for the default options.
Returns

WebAppBrowser The browser.

Create(Uri, WebAppBrowserOptions?)

WebAppBrowser.cs

Creates a browser connected to a server over the network (HTTP).

public static WebAppBrowser Create(Uri baseAddress, WebAppBrowserOptions? options = null)
Parameters
baseAddress Uri
The URL of the application.
options WebAppBrowserOptions?
The options, or null for the default options.
Returns

WebAppBrowser The browser.

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

DisposeAsync()

WebAppBrowser.cs

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

net10.0

public ValueTask DisposeAsync()

net11.0

public ValueTask DisposeAsync()

Returns

ValueTask A task that represents the asynchronous dispose operation.

Opens a new page, on about:blank.

public BrowserPage NewPage()

Returns

BrowserPage The page.

OpenAsync(string, CancellationToken)

WebAppBrowser.cs

Opens a new page and navigates it to a URL, then waits until the document is loaded.

net10.0

public Task<BrowserPage> OpenAsync(string url = "", CancellationToken cancellationToken = default)

net11.0

public Task<BrowserPage> OpenAsync(string url = "", CancellationToken cancellationToken = default)

Parameters

url string
The URL, relative to WebAppBrowser.BaseAddress.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task<BrowserPage> The page.