User input

edit

The actions send trusted input events to the page, with the default actions of a browser: the focus moves, the text is typed in the fields, the forms are submitted, the page scrolls. Before an action, the element must be actionable, as in Playwright: in the document, visible, stable, enabled, and the element under the pointer. An element covered by another one (a dialog, an overlay) does not get the click, and the action fails with an ElementNotInteractableException that names the element that covers it. See Locators and assertions for the checks of each action.

await using var host = new ComponentTestHost();
var form = await host.RenderAsync<PersonForm>();

await form.GetByRole("button", "Save").ClickAsync();
Assert.False(form.Instance.Submitted);
Assert.Equal("The Name field is required.", form.Find(".validation-message").TextContent);

await form.GetByLabel("Name").FillAsync("Ada");
await form.GetByLabel("Subscribe").CheckAsync();
await form.GetByLabel("Country").SelectOptionAsync("fr");
await form.GetByRole("button", "Save").ClickAsync();

Assert.True(form.Instance.Submitted);
Assert.Equal("Ada", form.Instance.Person.Name);
Assert.True(form.Instance.Person.Subscribe);
Assert.Equal("fr", form.Instance.Person.Country);
// The snapshot shows the state of the field: Blazor sets the value as a property, not as an attribute
Assert.Equal("""<input id="name" name="Person.Name" class="modified valid" value="Ada">""", form.GetByLabel("Name").GetMarkup());
MethodInput
ClickAsync, DoubleClickAsync, RightClickAsyncThe mouse moves to the center of the element (or to ClickOptions.Position), then presses and releases a button, with ClickOptions.Modifiers
HoverAsyncThe mouse moves over the element
TapAsync, SwipeAsyncA finger: the pointer, touch and compatibility mouse events
WheelAsyncThe mouse wheel: the wheel event, then the scrolling
DragAndDropAsyncThe HTML drag and drop of an element on another one
FocusAsyncFocuses the element
TypeAsync, PressAsyncFocuses the element, then the keys of a US keyboard
FillAsync, ClearAsyncReplaces the text of a field, then leaves it (the change event of @bind). The keyboard cannot type in a date, time, datetime-local, month, week, color or range input: their value is set, then input and change fire, as in Playwright
CheckAsync, UncheckAsync, SetCheckedAsyncClicks a check box or a radio button when needed
SelectOptionAsyncSelects options of a select element by value or by label, then fires input and change

Each action waits until the components are idle, so the result of an asynchronous event handler is there when it returns. To check the page while the handler is still running (a loading indicator), click with ClickOptions.NoWaitAfter, then wait for the result with an assertion of Expect.

The methods are on the locators, which find their element again before each attempt, on PageInput (host.Input), and on the elements, as extension methods of ElementExtensions. host.Input.Raw is the low-level input of WebPlatform: positions in CSS pixels, pointer down and up, keys down and up.

The handlers get the event arguments of Blazor (MouseEventArgs, PointerEventArgs, KeyboardEventArgs, TouchEventArgs, ChangeEventArgs...) built from the DOM events, with their positions and their modifiers. CustomEventArgs creates the arguments of the custom event types.