Why desktop tests should not rely on coordinates and images
Updated:
Recording clicks is a quick way to create a first automated scenario. If the test remembers only a cursor position or an image of a button, however, it responds to the appearance of the screen rather than the meaning of the element. A long-term regression suite therefore needs a more stable foundation.
Why tests start failing without an application defect
A test that clicks point 412 × 260 does not know whether the Save button is at that position. Moving the window, using a different resolution, changing Windows scaling or adding a new row to the form may cause it to click beside the button. Sometimes the test fails. At other times, it performs the wrong action and produces a misleading result.
Image-based matching is slightly more flexible, but it still depends on the font, colour, theme, resolution and rendering quality. The problem is particularly apparent when tests run on a different computer from the author’s or when the application receives a visual change without a functional change.
This does not mean that visual comparison is wrong. It is appropriate when appearance is the subject of the check, for a chart, document preview or print layout, for example. As the main way to control forms, however, it usually creates an unnecessary amount of maintenance.
How a more stable approach works
Windows UI Automation provides a structured tree of application elements. A test can distinguish a window, text field, table and button and work with their properties. Instead of a position, it can look for a button with a particular identifier in a specified window.
When designing locators, we proceed from the most stable information:
- a unique
AutomationIdin the correct window or container, - the element type combined with a stable name or another property,
- the relationship to a parent element if the identifier alone is insufficient,
- order or coordinates only as a last, deliberately accepted option.
An AutomationId does not have to be unique across the entire application, so the search scope also matters. We centralise access to screens in shared test-suite components so that changing one element does not require dozens of scenarios to be modified. The test also waits for a particular application state rather than a fixed number of seconds.
The limits of UI Automation
Not every custom graphical element provides the system with sufficient information. The application therefore needs to be examined with an inspection tool before work begins. Sometimes adding names, roles or identifiers to the application code helps. In other cases, a combined approach is required.
A change of colour or position will then generally not affect the test, although a change to an element’s name or structure may. UI Automation does not remove maintenance. It ties maintenance to meaningful interface changes. Good exposure of elements can also help assistive technologies, but it does not replace accessibility testing on its own.
What to establish when first inspecting a screen
Choose one important scenario and inspect elements in its later states too. A button may exist as soon as a window opens but remain disabled until data is entered. A list may contain different items after filtering, and confirmation may appear in a separate dialog.
A practical record might look like this:
| Question | What to record |
|---|---|
| Where is the correct element? | Process, window and container to search |
| How do we distinguish it? | Identifier, type, name and number of matching elements |
| Which action does it support? | Available interface for invoking, changing a value or selecting |
| What confirms the outcome? | Changed data, a saved record or another expected state |
Microsoft’s property reference notes that AutomationId is optional and not guaranteed to remain stable between releases. Agree with developers which identifiers should remain stable and verify them after updates. A button name may also change with translation. A matching value alone is therefore insufficient without the correct window context.
Finding an element does not establish a usable action
UI Automation control patterns expose particular element capabilities, such as Invoke for invoking an action or Value for a value. Support depends on the element and can change with its state. Seeing a button in the tree alone does not establish that the selected library can operate it as required.
Before an action, wait for its prerequisites. Afterwards, wait for the outcome. pywinauto supports waiting for existence, visibility and other element states, as well as repeated condition evaluation. A ready button does not establish that data loaded correctly or that saving finished. Select the success condition for the scenario and bound the wait with a clear timeout.
If the condition is not met, retain diagnostics. Automatically continuing by clicking the old coordinate can trigger another action and hide the cause. Selecting the first matching button must likewise not replace a decision when the locator matches several elements.
How to handle a table or a custom-drawn control
In virtualised lists, not all items may be fully available in the tree simultaneously. A record absent from the first inspection may therefore still exist in the application. Depending on component support, the item needs to be found, realised or scrolled into view. Check the capabilities of the particular control and testing library.
For a custom canvas without a usable tree, compare adding support in the application code with a limited visual approach. An export or application API can sometimes verify part of the outcome. That does not replace checking user-interface behaviour when the interface itself is the test target.
If image recognition is needed, restrict the search to the correct window and document the theme, scaling, reference image and match acceptance rule. Stop the scenario on an ambiguous result. Isolate this approach in one helper component so that it need not be edited in every test. Treat coordinates as a deliberately accepted limitation, with outcome verification.
How to compare the old and new scenario
Use the same test data, expected outcome and prepared initial state. In a model form, the test must save an edited address for a particular customer. Both versions must therefore verify the address against the correct record. A disappearing window or a “done” message alone may not confirm that result.
Compare runs after moving the window, at supported scaling and after an allowed visual change. Separately try a controlled functional fault, such as the new address not being saved. The new test must detect it. A more stable locator would be a weak result if an important assertion had also been removed.
Distinguish programmatic invocation from a mouse or keyboard test. A successful action through a control pattern alone does not confirm that the button is available to the user or that focus order is correct. To test those properties, use the corresponding interaction method and separate verification.
What to retain on failure
Record the last completed step, searched properties, matching elements, window state and application version. Supplement a screenshot with the relevant tree section where available. Distinguish a missing element, ambiguous locator, unready action and unmet outcome. This record is more useful than “click failed” alone.
Use prepared test accounts and limit sensitive data captured in images and text dumps. Verify the pilot on the intended automated-run machine too. The article on desktop tests in CI addresses conditions for launching and controlling windows. Then decide which further scenarios to rewrite and where to retain a documented combined approach.
What you gain
With verified locators, tests can be less sensitive to resolution, theme and minor visual changes. On failure, compare application behaviour, element structure and run evidence to establish the cause.
Assess maintenance benefits from the pilot’s results. Stable locators and shared screen objects can shorten repairs after a redesign and make it easier to distinguish an application defect from a test defect.
Next step
On one important screen, compare what the user sees with what appears in the Windows UI Automation tree. If you find stable names, types and identifiers there, rewrite one frequently failing scenario and monitor its results across repeated runs. For WPF and WinForms, continue with how to assess the application’s technical suitability.