Skip to main content

Rendering and visual tests

The default headless drawing backend supports behavior tests without producing pixels. Add official Avalonia.Skia version 12.1.4 when tests need screenshots, recording, or pixel comparisons. Add the same-version theme and fonts used by the application.

public static AppBuilder BuildAvaloniaApp() =>
AppBuilder.Configure<TestApplication>()
.UseSkia()
.UseHeadless(new AvaloniaHeadlessPlatformOptions
{
UseHeadlessDrawing = false
});

Capture a frame​

var window = new Window
{
Width = 160,
Height = 90,
Content = new Border { Background = Brushes.CornflowerBlue }
};
window.Show();
try
{
using var bitmap = HeadlessWindowExtensions.CaptureRenderedFrame(window);
Assert.NotNull(bitmap);
bitmap.Save("actual.png");
}
finally
{
window.Close();
}

The explicit static call selects the platform's capture method even if you also import the recording namespace, which contains a helper with the same extension-method name. The bitmap is owned by the caller and must be disposed.

Compare output​

Store a reviewed baseline with the test suite, capture an image at the same dimensions and DPI, and compare pixel buffers or use an image comparison library. Record the maximum tolerated difference in the test. Save the actual image and a difference image as CI artifacts when an assertion fails.

Use fixed fonts, theme, control dimensions, scaling, and operating-system images for stable baselines. A structural tree comparison is often easier to maintain when the requirement concerns hierarchy or properties rather than pixels.

Headless platform options​

PropertyDefaultEffect
UseHeadlessDrawingtrueUses the fake drawing backend; set false with Skia to render pixels
FrameBufferFormatRgba8888Chooses the headless framebuffer pixel layout
Fps60Sets the render timer frequency
ShouldRenderOnUIThreadtrueProcesses rendering on the dispatcher thread
OverlayPopupstrueHosts popups in the parent top-level overlay
UseSharedMouseDevicenullSet true for shared mouse capture and click state across windows
SupportsWindowPositioningtrueSet false to model a compositor that owns top-level positions
UseLogicalDesktopCoordinatesfalseSet true for logical desktop coordinates independent of render scaling

Record interactions​

Screen recording captures PNG/JPEG sequences, raw frames, or GIF output. Recorded input associates gesture steps and timeline markers with frames. Video conversion uses an optional FFmpeg installation to convert PNG sequences.

Topic reference: Avalonia visual regression guidance.