ComponentTestHost Class

A browser that renders Blazor components: the components are interactive (as in Blazor WebAssembly), their output is the DOM of a page of Meziantou.Framework.WebPlatform, with its styles, its layout and its user input.

public sealed class ComponentTestHost : TestPage

Example

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);
Inheritance
TestPage ComponentTestHost

Constructors

NameDescription
ComponentTestHost(ComponentTestHostOptions?)Initializes a new instance of the ComponentTestHost class.

ComponentTestHost(ComponentTestHostOptions?)

ComponentTestHost.cs

Initializes a new instance of the ComponentTestHost class.

public ComponentTestHost(ComponentTestHostOptions? options = null)

Parameters

options ComponentTestHostOptions?
The options, or null for the default options.

Properties

NameDescription
JSInteropGets the JavaScript interop of the components.
NavigationManagerGets the navigation manager of the components.
ServiceProviderGets the services of the components. The first access builds them from ComponentTestHost.Services.
ServicesGets the services of the components. Add services before the first component is rendered.

Gets the JavaScript interop of the components.

public ComponentJSInterop JSInterop { get; }

Property value

ComponentJSInterop

ServiceProvider

ComponentTestHost.cs

Gets the services of the components. The first access builds them from ComponentTestHost.Services.

public IServiceProvider ServiceProvider { get; }

Property value

IServiceProvider

Gets the services of the components. Add services before the first component is rendered.

public IServiceCollection Services { get; }

Property value

IServiceCollection

Methods

NameDescription
GetElement(ElementReference)Gets the element of an ElementReference captured by a component.
InvokeAsync(Action, CancellationToken)Runs code on the dispatcher of the components (as ComponentBase.InvokeAsync), then waits until the components are idle.
InvokeAsync(Func<Task>, CancellationToken)Runs code on the dispatcher of the components (as ComponentBase.InvokeAsync), then waits until the components are idle.
NavigateAsync(string, CancellationToken)Navigates to a URL of the application, as a click on a link does (the router renders the page of the URL).
Render<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>?, RenderOptions?)Renders a component without waiting for its asynchronous lifecycle methods: the result shows the first render (for instance a loading indicator). Use TestPage.WaitForIdleAsync or TestPage.WaitForAsync afterwards.
RenderAsync(RenderFragment, RenderOptions?, CancellationToken)Renders a fragment (for instance @<MyComponent Value="1" /> in a Razor test), and waits until it is idle.
RenderAsync<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>?, RenderOptions?, CancellationToken)Renders a component, and waits until the component and its descendants are idle (their asynchronous lifecycle methods and the tasks of the page are completed).

GetElement(ElementReference)

ComponentTestHost.cs

Gets the element of an ElementReference captured by a component.

public Element? GetElement(ElementReference reference)

Parameters

reference ElementReference
The reference.

Returns

Element? The element, or null when it is not in the document.

InvokeAsync overloads

InvokeAsync(Action, CancellationToken)

ComponentTestHost.cs

Runs code on the dispatcher of the components (as ComponentBase.InvokeAsync), then waits until the components are idle.

public Task InvokeAsync(Action action, CancellationToken cancellationToken = default)
Parameters
action Action
The code.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task A task completed once the components are idle.

InvokeAsync(Func<Task>, CancellationToken)

ComponentTestHost.cs

Runs code on the dispatcher of the components (as ComponentBase.InvokeAsync), then waits until the components are idle.

net10.0

public Task InvokeAsync(Func<Task> action, CancellationToken cancellationToken = default)

net11.0

public Task InvokeAsync(Func<Task> action, CancellationToken cancellationToken = default)
Parameters
action Func<Task>
The code.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task A task completed once the components are idle.

Render<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>?, RenderOptions?)

ComponentTestHost.cs

Renders a component without waiting for its asynchronous lifecycle methods: the result shows the first render (for instance a loading indicator). Use TestPage.WaitForIdleAsync or TestPage.WaitForAsync afterwards.

public RenderedComponent<TComponent> Render<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameters = null, RenderOptions? options = null) where TComponent : IComponent

Type parameters

TComponent
The type of the component.

Parameters

parameters Action<ComponentParameterCollectionBuilder<TComponent>>?
Sets the parameters of the component, or null.
options RenderOptions?
Where the component is rendered, or null to render it in the body of the page.

Returns

RenderedComponent<TComponent> The rendered component.

RenderAsync overloads

RenderAsync(RenderFragment, RenderOptions?, CancellationToken)

ComponentTestHost.cs

Renders a fragment (for instance @<MyComponent Value="1" /> in a Razor test), and waits until it is idle.

public Task<RenderedComponent<FragmentContainer>> RenderAsync(RenderFragment fragment, RenderOptions? options = null, CancellationToken cancellationToken = default)
Parameters
fragment RenderFragment
The fragment.
options RenderOptions?
Where the fragment is rendered, or null to render it in the body of the page.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task<RenderedComponent<FragmentContainer>> The rendered fragment.

RenderAsync<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>?, RenderOptions?, CancellationToken)

ComponentTestHost.cs

Renders a component, and waits until the component and its descendants are idle (their asynchronous lifecycle methods and the tasks of the page are completed).

net10.0

public Task<RenderedComponent<TComponent>> RenderAsync<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameters = null, RenderOptions? options = null, CancellationToken cancellationToken = default) where TComponent : IComponent

net11.0

public Task<RenderedComponent<TComponent>> RenderAsync<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameters = null, RenderOptions? options = null, CancellationToken cancellationToken = default) where TComponent : IComponent
Type parameters
TComponent
The type of the component.
Parameters
parameters Action<ComponentParameterCollectionBuilder<TComponent>>?
Sets the parameters of the component, or null.
options RenderOptions?
Where the component is rendered, or null to render it in the body of the page.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task<RenderedComponent<TComponent>> The rendered component.