Skip to main content
Version: 11.0.x

Manual Setup of Headless Platform

warning

This page explains an advanced usage scenario with the Headless platform. We recommend using the XUnit or NUnit testing frameworks instead.

Install Packages

To set up the Headless platform, you need to install two packages:

tip

The Headless platform doesn't require any specific theme, and it is possible to swap FluentTheme with any other.

Setup Application

As in any other Avalonia app, an Application instance needs to be created, and themes need to be applied. When using the Headless platform, the setup is not much different from a regular Avalonia app and can mostly be reused.

<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Tests.App">
<Application.Styles>
<FluentTheme />
</Application.Styles>
</Application>

And the code:

using Avalonia;
using Avalonia.Headless;

public class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
}

Run Headless Session

using Avalonia.Controls;
using Avalonia.Headless;

// Start Headless session passing Application type.
using var session = HeadlessUnitTestSession.StartNew(typeof(App));

// Since the Headless session has its own thread internally, we need to dispatch actions there:
await session.Dispatch(() =>
{
// Setup controls:
var textBox = new TextBox();
var window = new Window { Content = textBox };

// Open window:
window.Show();

// Focus text box:
textBox.Focus();

// Simulate text input:
window.KeyTextInput("Hello World");

// Assert:
if (textBox.Text != "Hello World")
{
throw new Exception("Assert");
}
}, CancellationToken.None);