---
title: "Automate UI tests for a C# desktop app"
url: https://recursiveai.org/oneapp/cookbook/drive-desktop-app-from-tests/
description: "Test a C# desktop app without a display. Launch a Debug build, click a button by CSS selector, and assert the new text with the OneApp CLI."
version: "1.0.0"
---

The app must be built with OneApp; this test doesn’t drive WinForms or WPF apps. To run the commands yourself, start in a new console project with the `Program.cs` shown below.

```shell
dotnet add package RecursiveAI.OneApp
dotnet new tool-manifest
dotnet tool install RecursiveAI.OneApp.Cli
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

The harness is a Debug-only local listener that lets the CLI click controls and read the app’s UI. Run these commands from the sample project in a POSIX shell with `jq`:

```shell
app=$(dotnet oneapp run . --headless --detach | jq -er .info)
dotnet oneapp click '#save' --app "$app"
dotnet oneapp wait '#status' --text Saved \
  --app "$app"
dotnet oneapp call quit --app "$app"
```

[The example](https://recursiveai.org/oneapp/cookbook/drive-desktop-app-from-tests/drive-desktop-app-from-tests.zip) is the full program below: unzip it and `dotnet run`.

For the automated check, use a POSIX shell with `jq` installed (on Windows, use WSL or Git Bash with `jq`). In the unzipped folder run `dotnet tool restore`, then `sh test.sh`. You don’t need to start the window separately.

```text
PASS: clicking Save changed the status to Saved.
```

## Full program

The HTML contains both the `#save` button and the `#status` paragraph. When OneApp sets up the page, it attaches a click handler that changes the paragraph from `Not saved` to `Saved`.

```csharp
using OneApp;
using OneRender.Interaction;

var html = """
    <button id="save">Save</button>
    <p id="status">Not saved</p>
    """;
using var app = AppHost.Create(new(html)
{
    Setup = page => page.Events.AddEventListener(
        EventTarget.Of(page.FindElement("save")), "click", _ =>
        page.SetTextContent(page.FindElement("status"), "Saved"))
});
app.Run();
```

The project sets the harness switch only for Debug:

```xml
<OneAppHarness Condition="'$(Configuration)' == 'Debug'">true</OneAppHarness>
```

The script starts the app in Debug with the test listener enabled. It uses `jq` to read the discovery file path from the launch result, then passes that path to every command. This keeps the test pointed at its own app even when other instances are running.

```shell
#!/bin/sh
set -eu
app=$(dotnet oneapp run . --headless --detach | jq -er .info)
trap 'dotnet oneapp call quit --app "$app" >/dev/null' EXIT

dotnet oneapp wait '#status' --text 'Not saved' --app "$app" >/dev/null
dotnet oneapp click '#save' --app "$app" >/dev/null
dotnet oneapp wait '#status' --text Saved --app "$app" >/dev/null
printf '%s\n' 'PASS: clicking Save changed the status to Saved.'
```

If `#status` doesn’t reach `Saved`, `wait` returns an error, the script exits nonzero, and the cleanup command asks the app to quit.

## Variations

**Check the exact text without waiting.** After a successful test click, ask the harness for the paragraph’s text:

```shell
dotnet oneapp call page_text \
  '{"selector":"#status"}' --app "$app"
```

```json
{"text":"Saved"}
```

## Related recipes

- [Build a desktop app with HTML and CSS in C#](https://recursiveai.org/oneapp/cookbook/build-desktop-app-with-html-css/)
- [Show desktop notifications from a C# tray app](https://recursiveai.org/oneapp/cookbook/show-desktop-notification-from-tray-app/)
- [Open a native file dialog in a C# desktop app](https://recursiveai.org/oneapp/cookbook/open-native-file-picker/)


## The short answer

Use OneApp’s development test harness, a local listener that’s enabled when the test launches the app. In this example the `oneapp` tool clicks a button by ID and asserts an observable result: the paragraph changes from `Not saved` to `Saved`. The tool returns a nonzero exit code when an assertion fails, so a shell script can fail the test without parsing the English error message.

## How do I automate UI testing for a C# desktop app?

Separate what you do from what you expect. First wait for the initial text, so a stale or unexpected starting state can’t pass the test. Then click `#save`, the CSS selector for the button’s ID. Finally, wait for `#status` to equal `Saved`. The initial check matters: a test that checks only the final text would also pass if the page had started out saying `Saved` and the click did nothing.

The harness sends input through the window’s event queue rather than calling the Save handler directly. That exercises the page’s input and event wiring; a unit test that calls a save method can still check what that method does with inputs the UI doesn’t produce. Keep stable IDs on controls you want to test instead of making tests depend on pixel coordinates. When checking a state change, wait for that specific state rather than sleeping for a fixed time.

## Can I run desktop UI tests without a display?

Yes, for OneApp’s own pages. `--headless` selects a backend that runs without a window system. It’s suitable here because the input and the assertion happen inside the app’s page. It doesn’t show that the operating system can display a tray icon or open a native file chooser; run those checks in a desktop session that supplies the corresponding services.

Keep the listener out of production: `OneAppHarness` defaults to false in Release, and the runtime opt-in can’t override that setting. This test needs both sides: a build that allows the listener and a launch that turns it on. See the [harness command reference](https://recursiveai.org/oneapp/docs/harness/) for selectors, window targeting, and exit codes.
