DEV Community

Cover image for Tutorial: Relational Selectors in askui
Johannes Dienst
Johannes Dienst

Posted on • Originally published at askui.com

Tutorial: Relational Selectors in askui

In this tutorial, we will explain how all the different relational selectors work in askui, so you get a thorough understanding of them.



Relational selectors can be difficult and sometimes confusing to work with at first. Sometimes it is even not clear why a specific element will not be selected or why the selector does not seem to work.



After reading this tutorial you will know how to use the full power of all the relational selectors. Additionally, you learn what pitfalls you can fall into and how to avoid them in the future.

In this article we’ll discuss the following relational selectors:

Prerequisites

above()

When you want to click on a textfield and it is above an element, like for example a button with the text Submit. You can do it with above(). The following code snippet moves the mouse to the textfield above the Submit-button:

await aui
  .click()
  .textfield()
  .above()
  .button()
  .withText('Submit')
  .exec();
Enter fullscreen mode Exit fullscreen mode

Gif showcasing above().

below()

When you want to select a textfield you can do so by finding the correct label, which is often above the textfield. The following code snippet moves the mouse to the textfield below the text Mobile Number:

await aui
  .moveMouseTo()
  .textfield()
  .below()
  .text()
  .withText('Mobile Number')
  .exec();
Enter fullscreen mode Exit fullscreen mode

Gif showcasing below().

contains()

For selecting an element, that contains another element, contains() is the right candidate. It is especially useful if you want to select a textfield with a placeholder text inside it. The text inside the textfield is annotated as an element itself.

If you have problems with selecting a specific element, always run annotate() to create a screenshot of all the annotations or use annotateInteractively() to see if you need to use contains().

The following snippet moves the mouse to a textfield based on its placeholder text First Crush which is contained in the textfield:

await aui
  .moveMouseTo()
  .textfield()
  .contains()
  .text()
  .withText('First Crush')
  .exec();
Enter fullscreen mode Exit fullscreen mode

Gif showcasing contains().

in()

When you want to target an element that is inside another element you can use in().

--------------------
|     outerEl      |
|  --------------  |
|  |  innerEl   |  |
|  --------------  |
|                  |
--------------------
Enter fullscreen mode Exit fullscreen mode

The following code snippet moves the mouse pointer to the text of the first textfield askui found:

await aui
  .moveMouseTo()
  .text()
  .in()
  .textfield()
  .exec();
Enter fullscreen mode Exit fullscreen mode

Gif showcasing in().

leftOf() and rightOf()

If you want to select an element based on its location left or right of another element you have to use leftOf() or rightOf() respectively.

💡 If you do not specify another filter like withText() then you will get the nearest element. Otherwise, askui retrieves the nearest element that matches the filter!

await aui
  .moveMouseTo()
  .text()
  .leftOf()
  .text()
  .withText('Denmark')
  .exec();
Enter fullscreen mode Exit fullscreen mode

Gif showcasing leftOf().

await aui
  .moveMouseTo()
  .text()
  .rightOf()
  .text()
  .withExactText('Joe Root')
  .exec();
Enter fullscreen mode Exit fullscreen mode

Gif showcasing rightOf()

nearestTo()

Filtering with the nearestTo() filter will return the element nearest to another element. This is useful when the direction is not clear on where to search. Especially responsive designs are prone to wrap elements into a new line where leftOf() and rightOf() would fail.

await aui
  .moveMouseTo()
  .textfield()
  .nearestTo()
  .text()
  .withTextRegex('User Em*')
  .exec();
Enter fullscreen mode Exit fullscreen mode

Gif showcasing nearestTo().

Additional Considerations About the Distance Metric

The distance is not entirely based on physical distance. If you use an additional filter like withText() it optimizes to find the best match. It also considers special cases, for example, modal dialogs. Therefore the element selected by askui might sometimes be wrong from a user's point of view.

You can use moveMouseTo() like in the following example to see what element a command targets.

await aui
  .moveMouseTo()
  .textfield()
  .above()
  .button()
  .withText('Submit')
  .exec()
Enter fullscreen mode Exit fullscreen mode

Conclusion

Relational selectors are a powerful tool to create robust automation. However, sometimes they are tricky to master.

This tutorial equipped you with the knowledge to use their full power in your automation.

If you have a recurring or persisting issue, don’t hesitate to ask the Discord community for help!

Top comments (0)