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, IDisposableExample
// 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
| Name | Description |
|---|---|
| WebAppBrowser(Uri, DocumentLoader, WebAppBrowserOptions?) | Initializes a new instance of the WebAppBrowser class. |
WebAppBrowser(Uri, DocumentLoader, WebAppBrowserOptions?)
WebAppBrowser.csInitializes a new instance of the WebAppBrowser class.
public WebAppBrowser(Uri baseAddress, DocumentLoader loader, WebAppBrowserOptions? options = null)Parameters
baseAddressUri- The URL that the relative URLs of
WebAppBrowser.OpenAsyncare resolved against. loaderDocumentLoader- The loader of the requests of the pages (see
TestDocumentLoaders). optionsWebAppBrowserOptions?- The options, or null for the default options.
Properties
| Name | Description |
|---|---|
| BaseAddress | Gets the URL that the relative URLs of WebAppBrowser.OpenAsync are resolved against. |
| Cookies | Gets the cookies of the pages. |
| Pages | Gets the open pages. |
BaseAddress
WebAppBrowser.csGets the URL that the relative URLs of WebAppBrowser.OpenAsync are resolved against.
public Uri BaseAddress { get; }Property value
Cookies
WebAppBrowser.csPages
WebAppBrowser.csMethods
| Name | Description |
|---|---|
| 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.csCreates a browser connected to an ASP.NET Core application in memory.
public static WebAppBrowser Create(TestServer server, WebAppBrowserOptions? options = null)Parameters
serverTestServer- The test server (
WebApplicationFactory.Server). optionsWebAppBrowserOptions?- The options, or null for the default options.
Returns
WebAppBrowser The browser.
Create(Uri, WebAppBrowserOptions?)
WebAppBrowser.csCreates a browser connected to a server over the network (HTTP).
public static WebAppBrowser Create(Uri baseAddress, WebAppBrowserOptions? options = null)Parameters
baseAddressUri- The URL of the application.
optionsWebAppBrowserOptions?- The options, or null for the default options.
Returns
WebAppBrowser The browser.
Dispose()
WebAppBrowser.csPerforms application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()DisposeAsync()
WebAppBrowser.csNewPage()
WebAppBrowser.csOpenAsync(string, CancellationToken)
WebAppBrowser.csOpens 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
urlstring- The URL, relative to
WebAppBrowser.BaseAddress. cancellationTokenCancellationToken- A token to cancel the wait.
Returns
Task<BrowserPage> The page.