Challenges in User Interface Automation: New Approaches
Tags: testing, programming
Published: 2022-10-17 07:45:16

In the previous blog we saw, that automation with selector-based approaches is a proven way to automate UIs that are based on a structure like HTML. If you played around with our [Git repository](https://bit.ly/3rDP9PC) you noticed how fast the test execution runs.

In this blog, we will discuss the problems that come with selector-based approaches and how current tools try to remedy them with Smart Selectors and Self-healing Tests.

## Problems With Selector-Based Approaches
When you look closely at the structure of the code under test and the test code you notice, that these two are intertwined. That leads to problems with large test suites.

> Every change in the code under test can lead to changes in the structure (HTML) under test causing tests to fail. That is why large UI test suites usually become flaky.

You can combat this problem with a strong engineering culture that puts constraints on how the resulting HTML is structured. Putting ids in every element is also a valid solution. But the main issue remains: You are coupling the code under test with your test code!

## Smart Selectors
Smart Selectors promise a solution to the tight coupling of test code with the code under test.

They help to implement Self-healing tests that can succeed, even when the element can not be detected due to wrong labeling like in the picture below (The button should have the id _signIn_ but it changed to _anmelden_):

![The id-selector signIn does not find the element, because it was accidentally renamed to anmelden.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7y83rql2trzwytwc0lv.png)<figcaption>The id-selector _signIn_ does not find the element, because it was accidentally renamed to _anmelden_.</figcaption>

The following three approaches are followed, which often are then combined with classic selectors in practice to reliably recognize elements.

## Optical Character Recognition (OCR)
OCR is the recognition of text from images. The technology has been used for a long time for various use cases and has now also established itself in UI automation tools.

![Different wrong spellings of Apple get recognized.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70d7r2xbxe5yj5gi3muz.png)<figcaption>Different wrong spellings of _Apple_ get recognized.</figcaption>

## Picture in Picture Search
Picture-in-Picture search refers to the search of cut-out elements on the UI by pixel matching. The element is usually cut out and stored as an image. If you want to select this element in a test, the picture runs over the entire UI and tests for a match between the cut-out picture and the actual UI.

![Detect Login button with a cut-out picture of it.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yep7h1lvetjmkz19i7r9.png)<figcaption>Detect Login button with a cut-out picture of it.</figcaption>

## Relations
A new approach is relative locators, which were also introduced in Selenium 4. The functionality is simple: Instead of selecting an element directly, it is addressed via the relation to other elements.

![Type test in textfield below email.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dc46si2eose6pgrls2ul.png)

For example, a text field for _Username_ can be found by its relation to the _Login_-Button as in the following example:

![Login form with label username, right of it a text field and a button.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gkax01svamz002x5vojl.png)

The Open Source library [Taiko](https://taiko.dev/) can detect elements based on relation.

```javascript
await write("TaikoUser", into(textBox(toLeftOf('Login'))));
```

You can find the full example in our [Git Repository](https://bit.ly/3rDP9PC).

### Are Self-healing Tests a Good Idea?
Self-healing Tests sound great in theory. However, it is a two-edged sword in our opinion. They will ease the pain of flaky tests, but come with two caveats:

- You need to implement and maintain the backup steps
- A test that might fail remains green and can mask a real error

💡 So be conscious and deliberate about deploying this technique. 

## Conclusion
Smart Selectors can help you write more robust tests by employing techniques that do not rely solely on the structure of the code (HTML) under test.

OCR, Picture-in-Picture search, and relational selectors are available in tools today and in recent years get combined with Machine Learning/AI to ease the pain of maintaining large test suites.

In the last installment of the series, we will look at currently _untestable_ User Interfaces like 2D Planes and canvases and show how cutting-edge and future products will enable UI automation for these use cases.

[Do you want to know more about UI automation's future with askui? Join our Discord community!](https://discord.gg/Gu35zMGxbx)