Browser tests and accessibility
Browser tests and accessibility
UI tests should assert observable behavior rather than private fields. Component tests can isolate rendering logic, while browser tests exercise real navigation, events, focus, connection activation and accessibility. Neither alone proves a complete application is usable.
Use native buttons and links, meaningful labels, heading order, visible focus and status announcements. Verify keyboard interaction, focus after navigation, validation and error states. Status regions should announce changes without stealing focus. A disabled control may not be focusable; offer a visible explanation when needed. For lists, @key associates rendered elements with stable identities rather than position. Browser tests should wait for meaningful conditions, not arbitrary sleep durations. bUnit is a community component-testing library; browser automation such as Playwright tests actual DOM behavior. These projects supply explicit manual browser assertions and no unverified automation dependencies. Use accessibility tools plus manual keyboard and screen-reader testing; markup alone cannot establish compliance.
Baseline: .NET 10 / C# 14. In an approved environment use the complete fixture project, restore only reviewed dependencies from approved sources, then run dotnet build --no-restore and dotnet run --no-build --no-launch-profile. Blazor projects use localhost:5087; stop the server with Ctrl+C. Other examples start a bounded local server and client in one process. No real credentials or external services. Expected results are reasoned, not executed.
Exercise a keyboard-accessible counter
Tab to the button, press Space once, then Enter once.
using Fixture;var builder = WebApplication.CreateBuilder(args);builder.WebHost.ConfigureKestrel(o => o.ListenLocalhost(5087));builder.Services.AddRazorComponents().AddInteractiveServerComponents();var app = builder.Build();app.UseAntiforgery();app.MapStaticAssets();app.MapRazorComponents<App>().AddInteractiveServerRenderMode();app.Run();App.razor
@using Microsoft.AspNetCore.Components.Routing<!DOCTYPE html><html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><base href="/" /><title>ByteMind local lab</title></head><body><main><Router AppAssembly="typeof(App).Assembly"><Found Context="route"><RouteView RouteData="route" /></Found></Router></main><script src="_framework/blazor.web.js"></script></body></html>Demo.razor
@page "/"@using Microsoft.AspNetCore.Components@using Microsoft.AspNetCore.Components.Web@using Microsoft.AspNetCore.Components.Forms@using static Microsoft.AspNetCore.Components.Web.RenderMode@rendermode InteractiveServer<h1>Reading list</h1><button type="button" @onclick="Add">Add book</button><p role="status">Books: @count</p>@code { private int count; private void Add() => count++; }Expected result (not executed):
Books: 2 after two keyboard activations; focus remains on button.Native button semantics support standard keyboard activation; verify the live announcement manually.
Keep row identity across list reorder
Type a distinct note in the Atlas input, then reverse the list.
using Fixture;var builder = WebApplication.CreateBuilder(args);builder.WebHost.ConfigureKestrel(o => o.ListenLocalhost(5087));builder.Services.AddRazorComponents().AddInteractiveServerComponents();var app = builder.Build();app.UseAntiforgery();app.MapStaticAssets();app.MapRazorComponents<App>().AddInteractiveServerRenderMode();app.Run();App.razor
@using Microsoft.AspNetCore.Components.Routing<!DOCTYPE html><html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><base href="/" /><title>ByteMind local lab</title></head><body><main><Router AppAssembly="typeof(App).Assembly"><Found Context="route"><RouteView RouteData="route" /></Found></Router></main><script src="_framework/blazor.web.js"></script></body></html>Demo.razor
@page "/"@using Microsoft.AspNetCore.Components@using Microsoft.AspNetCore.Components.Web@using Microsoft.AspNetCore.Components.Forms@using static Microsoft.AspNetCore.Components.Web.RenderMode@rendermode InteractiveServer<h1>Book notes</h1><button type="button" @onclick="Reverse">Reverse order</button>@foreach (var book in books) { <div @key="book.Id"><label for="@($"note-{book.Id}")">@book.Name</label> <input id="@($"note-{book.Id}")" @bind="book.Note" /></div>}@code { private List<Book> books = new() { new(1,"Atlas"), new(2,"Comet") }; private void Reverse() => books.Reverse(); private sealed record Book(int Id,string Name) { public string Note { get; set; } = ""; }}Expected result (not executed):
Comet precedes Atlas after reverse; the typed note remains associated with Atlas.A stable key identifies the row, while binding stores its note in the model.
Test empty and populated results
Activate the toggle twice, checking status and list content.
using Fixture;var builder = WebApplication.CreateBuilder(args);builder.WebHost.ConfigureKestrel(o => o.ListenLocalhost(5087));builder.Services.AddRazorComponents().AddInteractiveServerComponents();var app = builder.Build();app.UseAntiforgery();app.MapStaticAssets();app.MapRazorComponents<App>().AddInteractiveServerRenderMode();app.Run();App.razor
@using Microsoft.AspNetCore.Components.Routing<!DOCTYPE html><html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><base href="/" /><title>ByteMind local lab</title></head><body><main><Router AppAssembly="typeof(App).Assembly"><Found Context="route"><RouteView RouteData="route" /></Found></Router></main><script src="_framework/blazor.web.js"></script></body></html>Demo.razor
@page "/"@using Microsoft.AspNetCore.Components@using Microsoft.AspNetCore.Components.Web@using Microsoft.AspNetCore.Components.Forms@using static Microsoft.AspNetCore.Components.Web.RenderMode@rendermode InteractiveServer<h1>Search results</h1><button type="button" @onclick="Toggle">Toggle sample results</button><p role="status">@(show ? "2 results" : "No results")</p>@if (show) { <ul><li>Atlas</li><li>Comet</li></ul> }@code { private bool show; private void Toggle() => show = !show; }Expected result (not executed):
Initially No results; first activation 2 results and two items; second activation No results and no list.The status and semantic list give testable outcomes without inspecting component internals.
Apply the concept
Write a browser test checklist for the reservation form from the previous lesson. Include keyboard, invalid input, valid input, focus and a connection-loss scenario.
Use the manual practice and knowledge check to reason about the boundary. Do not deploy these local demonstrations as production services.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Browser tests and accessibility — knowledge check
Apply the lesson concepts to the following scenarios.
What is a strong browser test assertion?
Which element supplies native keyboard button behavior?
What does a stable @key help preserve?
Can automated accessibility scans establish complete compliance?
Which is a better wait condition for a browser test?