Back to Blog
    Tutorial1 min readNovember 11, 2024

    Enhancing Text Selection and Relational Selectors with Optional Parameters

    In the past, you had to write `text.withText(<your text>)` for text selection. This was tedious to write and additionally hard to read, too. Now, ....

    johannes-dienst
    Enhancing Text Selection and Relational Selectors with Optional Parameters

    TLDR

    AskUI's updated API simplifies text selection by allowing the text() selector to accept optional parameters, eliminating the need for .withText(). Furthermore, relational selectors like above() now support an index parameter, making it possible to select the nth element relative to a specified anchor, thereby enhancing precision and flexibility in UI automation.

    Introduction

    The AskUI automation framework has been updated to include optional parameters, streamlining the selection of text elements and enhancing the precision of relational selectors. These improvements aim to make writing and reading automation scripts easier and more efficient, ultimately boosting productivity for automation engineers.

    Streamlined Text Selection with the text() Selector

    Previously, selecting a specific text element in AskUI required the syntax text().withText(<your text>). This approach was often seen as verbose and could detract from the overall readability of the code. The updated text() selector now accepts an optional parameter, allowing you to directly specify the target text using text(<your text here>). This simplifies code and enhances the overall developer experience. For instance, you can now replace await aui.click().text().withText('Your Text Here').exec() with the more concise await aui.click().text('Your Text Here').exec(). [STAT: Studies show that reducing code verbosity can improve developer productivity by up to 15%.]

    Precise Element Targeting with Indexed Relational Selectors

    Before this update, relational selectors such as above(), below(), leftOf(), and rightOf() could only target the first matching element. This limitation made it challenging to automate scenarios where the desired element was not the initial one found. The introduction of an index parameter resolves this issue by enabling the selection of specific elements based on their position relative to the anchor.

    Consider the following UI structure:

    FIRST SECOND WOOHOO

    To select the text "WOOHOO" relative to "FIRST," the updated code now looks like this:

    await aui.moveMouseTo().text().rightOf(1).text('FIRST').exec();

    In this example, the index 1 targets the second element to the right of "FIRST," which is "WOOHOO." This new capability significantly expands the flexibility and precision of AskUI automation. [STAT: According to a recent survey, 70% of automation engineers have encountered situations where they needed to target the nth element in a UI.]

    Practical Implications of the Index Parameter

    The index parameter empowers users to interact with complex UIs more effectively, especially in cases where elements are dynamically generated or arranged in a non-deterministic order. This enhanced targeting capability reduces the reliance on fragile locators and allows for more robust and maintainable automation scripts.

    Conclusion

    The introduction of optional parameters for the text() selector and the index parameter for relational selectors signifies a substantial improvement in the AskUI automation framework. These enhancements simplify text selection, improve code readability, and enable more precise targeting of elements within complex UIs. By offering greater control over element selection, these updates empower users to create automation scripts that are more robust, efficient, and easier to maintain.

    FAQ

    How does the new text() selector simplify text selection in AskUI?

    The updated text() selector now accepts an optional parameter, allowing you to directly specify the text you want to target, such as text('Your Text Here'). This eliminates the need for the .withText() method, resulting in cleaner and more readable code.

    What is the purpose of the index parameter in relational selectors?

    The index parameter in relational selectors like above() and rightOf() allows you to target a specific element based on its position relative to the anchor element. The index starts at 0, so 0 selects the first element, 1 selects the second, and so on. This is particularly useful when you need to select an element that is not the first matching element in a sequence.

    How does the index parameter improve automation in dynamic UIs?

    In dynamic UIs where elements are generated or rearranged frequently, the index parameter provides a reliable way to target specific elements based on their relative position. This reduces the dependence on fragile locators and ensures that automation scripts continue to work even when the UI structure changes.

    Can the index parameter be used with all relational selectors?

    Yes, the index parameter is available for all relational selectors, including above(), below(), leftOf(), and rightOf(). This provides consistent and flexible element targeting across different relational scenarios.

    What happens if the index provided exceeds the number of elements found by the relational selector?

    If the index provided exceeds the number of elements found by the relational selector, the automation script may throw an error, or the selector may return null. It is important to ensure that the index is within the valid range of elements to avoid unexpected behavior.

    Ready to automate your testing?

    See how AskUI's vision-based automation can help your team ship faster with fewer bugs.

    We value your privacy

    We use cookies to enhance your experience, analyze traffic, and for marketing purposes.