Meziantou.AspNetCore.Testing
0.1.4
Prefix Reserved
dotnet add package Meziantou.AspNetCore.Testing --version 0.1.4
NuGet\Install-Package Meziantou.AspNetCore.Testing -Version 0.1.4
<PackageReference Include="Meziantou.AspNetCore.Testing" Version="0.1.4" />
<PackageVersion Include="Meziantou.AspNetCore.Testing" Version="0.1.4" />
<PackageReference Include="Meziantou.AspNetCore.Testing" />
paket add Meziantou.AspNetCore.Testing --version 0.1.4
#r "nuget: Meziantou.AspNetCore.Testing, 0.1.4"
#:package Meziantou.AspNetCore.Testing@0.1.4
#addin nuget:?package=Meziantou.AspNetCore.Testing&version=0.1.4
#tool nuget:?package=Meziantou.AspNetCore.Testing&version=0.1.4
Meziantou.AspNetCore.Testing
Tests of Blazor components and of ASP.NET Core applications (MVC, Razor Pages, Blazor, minimal APIs...) in a browser engine written in .NET (Meziantou.Framework.WebPlatform): the pages have a real DOM, the CSS applies, the scripts run, and the user input (mouse, keyboard, touch) goes through the hit testing of the page. Compare the HTML, the accessibility tree and the screenshots with snapshots.
Render a Blazor component alone with ComponentTestHost (in the Meziantou.AspNetCore.Testing.Components namespace):
await using var host = new ComponentTestHost();
var counter = await host.RenderAsync<Counter>(parameters => parameters.Add(c => c.IncrementAmount, 2));
// A real click: the pointer moves to the center of the button, the hit testing finds the button
await counter.Find("button").ClickAsync();
await counter.GetByRole("button", "Click me").ClickAsync();
Assert.Equal("Current count: 4", counter.GetByRole("status").TextContent);
Assert.Equal("""
<p role="status">Current count: 4</p>
<button class="btn">Click me</button>
""", counter.Markup);
Open the pages of an application, in memory with TestServer or on a server, with WebAppBrowser:
// 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");
As in Playwright, GetByRole, GetByLabel, GetByText and the other locators find their element again each time they
are used, the actions wait until the element is visible, stable, enabled and not covered, and the assertions of
Expectations.Expect retry until they pass. The elements are the DOM elements of Meziantou.Framework.WebPlatform.Dom.
WebAssembly and Blazor WebAssembly
The pages run no WebAssembly by default: the WebAssembly namespace is not defined, so blazor.webassembly.js fails to
start. Set WebAppBrowserOptions.WebAssemblyRuntime (or ComponentTestHostOptions.WebAssemblyRuntime) to the
WasmtimeRuntime of the Meziantou.Framework.WebPlatform.Wasmtime
package:
// The runtime of the Meziantou.Framework.WebPlatform.Wasmtime package: the pages share it
using var runtime = new WasmtimeRuntime();
await using var factory = new WebApplicationFactory<Program>();
await using var browser = WebAppBrowser.Create(factory.Server, new WebAppBrowserOptions { WebAssemblyRuntime = runtime });
// The page instantiates a WebAssembly module and calls its add function
var page = await browser.OpenAsync("/wasm");
await Expect(page.Locator("output")).ToHaveTextAsync("2 + 3 = 5");
A Blazor WebAssembly application must also serve the files of its runtime (_framework/; for a standalone application,
the wwwroot folder of its published output, with TestDocumentLoaders.FromDirectories and a fallback file for its
routes). The applications of .NET 10, whose runtime uses the legacy exception handling instructions of WebAssembly, run
too: the Wasmtime runtime translates these instructions to the ones Wasmtime supports.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- Meziantou.Framework.WebPlatform (>= 1.0.16)
- Meziantou.Framework.WebPlatform.Imaging (>= 1.0.14)
- Meziantou.Framework.WebPlatform.JavaScript (>= 1.0.12)
- Microsoft.AspNetCore.TestHost (>= 10.0.14)
-
net11.0
- Meziantou.Framework.WebPlatform (>= 1.0.16)
- Meziantou.Framework.WebPlatform.Imaging (>= 1.0.14)
- Meziantou.Framework.WebPlatform.JavaScript (>= 1.0.12)
- Microsoft.AspNetCore.TestHost (>= 11.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.