How to automate WPF and WinForms application testing
Updated:
WPF and WinForms applications can be tested automatically if their controls provide enough stable information. A test does not need to click based on an image or a position on the screen. Before creating a complete suite, however, it is important to verify the specific application, especially custom tables, charts and older components.
Why a large proportion of repeated checks remain manual
Desktop processes tend to be long and depend on the state of the computer, local files or a database. Before release, people therefore go through the same forms repeatedly, but when time is short, they reduce the checklist to the area around the latest change. An error may then appear in a seemingly unrelated part of the application.
A team may use a click recorder for its first automation attempt. The suite works on its author’s computer but starts failing at a different resolution or after a layout change. The problem is not the desktop itself, but the way elements are identified and an unprepared test environment.
How Windows UI Automation works
Microsoft UI Automation provides an interface through which a testing tool examines and controls application elements. WPF provides automation peers for supported controls. Depending on the type of element, WinForms may use UI Automation or the classic Win32 interface.
A test can therefore find a text field by its type, name and identifier rather than by fixed coordinates. This makes it more resilient to ordinary visual changes, but not to every structural change. Custom components must expose the required properties, and AutomationId needs to be assessed within the correct window or container.
pywinauto in Python or FlaUI in .NET are often suitable for implementation. We discuss their differences and selection criteria in the pywinauto or FlaUI comparison.
From technical assessment to the first suite
A reliable process has four steps:
- Interface inspection. We check which elements the tool can see and what properties they provide.
- Pilot scenario. We choose an important process with both common and more complex elements and verify it from input to outcome.
- Stable environment. We prepare known test data, accounts, files and a way to restore the environment after a run.
- Maintainable architecture. We centralise access to windows and elements in shared components, and the tests wait for the application’s state rather than for a fixed time.
Graphical tests need a Windows session in which the application can actually run. A local computer, virtual machine and CI are all possible, but the runner needs to be prepared according to the application’s permissions, display requirements, parallel execution and dependencies. We then set the frequency according to the suite’s duration and risk.
What to watch out for
UI Automation is not a guarantee for every custom element. If inspection does not reveal a meaningful structure, identifiers or accessibility support may need to be added to the application’s code. Access to the source code is not always a prerequisite for a pilot, but sometimes a small modification is the least expensive route to stable tests.
A technology name is not enough to choose a tool
During inspection, find the specific field, button, table and dialog the pilot will use. Determine whether you can read a value, change it and check state. A visible element name does not establish that it provides the required operation. Pywinauto supports different backends, so verify your choice against the application’s actual controls. FlaUI is a .NET library offering UIA2 and UIA3. The newer option need not be more reliable for every WinForms component.
Microsoft describes custom WPF control support through automation peers. Not every visual object has its own peer. If a drawn component lacks the required structure or operations, switching testing libraries again may not solve the problem. Agree with the developer on exposing that control’s meaning and interaction.
If an application combines technologies, assess each important part separately. A pilot on a simple sign-in window does not yet cover a custom table, system dialog or embedded web component. Record supported activities and those needing changes or a different testing layer.
Identifiers and virtualised tables
Scope searches to the correct application instance, window and container. AutomationId is not unique across the entire tree. The same button in two forms can have the same identifier. Finding the first matching element therefore need not be the correct result.
Find table records by business identifier rather than row index. Sorting or filtering can change their order. Virtualisation also makes complete elements available only after realisation. An unloaded row need not be absent from the data. Check support for the relevant patterns or controlled searching and scrolling. Realising an element does not necessarily make it visible.
Include a record outside the first displayed section in the pilot, then repeat selection after changing sort order. After editing, check both the record identifier and saved value. This distinguishes selection of the wrong neighbouring row from a saving defect.
Wait for completion and verify the result
A window can exist and a button be enabled while the application is still loading data. Pywinauto describes waiting for control state or lower process CPU usage. Lower CPU usage is a supporting signal. It does not prove that a network request completed or that data was saved correctly.
Define a readiness condition before each important action, such as the correct record being loaded. After saving, wait for the agreed result or error within a timeout. Adding a longer fixed delay does not solve an operation ending in rejection.
Verify success by fetching the record again or using an agreed read interface. If checking an export, the file must belong to the current run and contain the expected data. An old file already present or a loading indicator disappearing is insufficient.
A short pilot: editing one record
Consider a test order register containing ORDER-A and ORDER-B. In this model, the first has quantity 2. The test changes only ORDER-A to quantity 3 and verifies the result after reopening it. The rules and names are illustrative, and the data should be separate from production.
| Check | Expected result |
|---|---|
| Selection after sorting | ORDER-A opens even when it is no longer on the original row. |
| Saving quantity 3 | After fetching again, ORDER-A has quantity 3 and ORDER-B remains unchanged. |
| Rejected invalid input | The agreed error appears and the saved value remains unchanged. |
| Application restart | After safe exit and launch, the saved result is available in the correct environment. |
Then repeat the scenario on the prepared Windows runner. For methods requiring active input, a locked or disconnected session can affect the result. Two runs must not compete for the same keyboard, window or test records. Record the application version, profile, backend, target window and last verified step.
If the application never started or the test lacks required access, mark the scenario unverified. When designing restoration, distinguish local files, the database and effects in external systems. Restoring one file does not automatically undo every consequence of the previous run.
What you gain
Repeatable scenarios can reduce manual checks before release and give the team the same verification after every relevant change. Testers can devote more time to exploratory testing, new features and situations that cannot easily be scripted.
The pilot provides evidence for estimating the return before investing in the complete suite. Include preparation, maintenance and failure investigation time. You know which screens are ready for automation, which technical changes they need and which checks are more sensible to keep manual.
Next step
Choose one critical scenario that the team repeats before every release and inspect its elements with an inspection tool. If the pilot remains stable over several repetitions, expand the suite according to business risk. For a combined product, you can also assess automation of the web component separately.