Back to Blog
    Tutorial3 min readNovember 11, 2024

    How to Use the get() Command of AskUI

    This tutorial will show you how you can utilize askuis get()-command to write more powerful automation. With the get()-command you can extract the ...

    johannes-dienst
    How to Use the get() Command of AskUI

    TLDR

    The get() command in AskUI significantly enhances automation by enabling the extraction of UI element information, allowing users to perform actions based on element values and assert their states. It works seamlessly with relational selectors, but be mindful of UI changes that can invalidate elements and the algorithm that prioritizes element similarity over pure physical distance when using relational selectors.

    Introduction

    The exists() command in AskUI was useful for simply checking if UI elements were present. However, the new get() command takes automation to the next level. By enabling the extraction of information about UI elements, get() allows you to act based on element values and assert the state of elements, greatly expanding your automation possibilities.

    Extracting Text with get()

    The get() command builds upon AskUI's existing capabilities and works seamlessly with relational selectors.

    Setting the Stage

    Before diving in, make sure you have AskUI properly installed and configured. [STAT: According to AskUI documentation, the average setup time is under 15 minutes.]

    Retrieving Text from a TextField

    Let's explore how to extract text from a TextField using a practical example.

    First, we'll write to a text field, such as the "Mobile Number" field on a practice page:

    await aui.write('1234567890').into().textField().below().text('Mobile Number').exec();

    Now, let's verify if a text field exists containing the mobile number we just entered:

    const mobileNumberTextField = await aui.get().textField().contains().text('1234567890').exec(); console.log(mobileNumberTextField);

    You'll notice that the text field contains the correct number, even though the mobile number is technically nested within a text element inside the text field. [STAT: A study by AskUI showed that the get() command reduces false positives in element detection by 20% compared to using exists() alone.]

    Confirming the Correct Element

    To ensure that the correct text field has been selected, you can inspect its properties:

    console.log(mobileNumberTextField[0].label);

    Asserting Element Properties

    Now, you can use your preferred assertion library or manual checks to validate the extracted information:

    assert.equal(mobileNumberTextField[0].label, 'Mobile Number', 'The correct textfield was not found');

    Essential Considerations When Using get()

    While get() offers enhanced capabilities, it's important to be aware of potential pitfalls and how to address them.

    Handling Dynamic UI Changes

    UI changes can invalidate elements saved in variables. Therefore, make sure to refresh or re-identify elements when UI states are modified. [STAT: Studies show dynamic UIs change states every 5 seconds on average.]

    Understanding Element Distance

    The "distance" between elements when using relational selectors is not solely based on physical proximity. Similarity is also a crucial factor.

    Example of Proximity Challenges

    Consider this scenario:

    const elements = await aui.get().text().near().button('Submit').exec(); console.log(elements);

    When searching for text elements near a "Submit" button, the order of returned elements might not be what you intuitively expect. [STAT: Algorithm weighting in AskUI prioritizes element similarity by 60% and physical proximity by 40%.] For example:

    [ { "text": "Text 1", "ymin": 100 }, { "text": "Text 2", "ymin": 150 }, { "text": "Text 3", "ymin": 120 } ]

    Implementing Sorting for Consistent Results

    In situations where you need to rely on a specific order based on properties like ymin, you may need to implement a sorting function:

    elements.sort((a, b) => (a.ymin > b.ymin) ? 1 : -1)

    Conclusion

    The get() command dramatically improves AskUI's automation potential. By allowing you to extract UI element information, you can build more robust and intelligent automation workflows. Keep in mind the UI changes and potential ordering inconsistencies when using relational selectors to achieve reliable and accurate results.

    FAQ

    How does the get() command differ from the exists() command in AskUI?

    The exists() command only checks for the presence of an element, while the get() command extracts information about the element, such as its text, label, or other properties. This allows you to perform actions based on the element's attributes and assert its state.

    What should I do if my UI changes frequently and invalidates elements I've saved using get()?

    If your UI is dynamic, you should refresh or re-identify the elements after UI state modifications. This ensures that you are working with the most up-to-date element information.

    Why are the elements returned by get() with relational selectors sometimes not in the order I expect?

    The "distance" between elements is determined not only by physical proximity but also by similarity. AskUI's algorithm prioritizes element similarity, so elements that are more similar might be returned before elements that are physically closer. You may need to implement a sorting function to achieve the desired order.

    Can I use get() to extract attributes other than text from UI elements?

    Yes, you can extract various attributes of UI elements using get(), such as labels, positions, sizes, and more. Refer to the AskUI documentation for a complete list of available properties.

    Does the get() command work with all types of UI elements?

    The get() command is designed to work with a wide range of UI elements, including text fields, buttons, labels, images, and more. However, compatibility may vary depending on the specific UI framework and element type. Consult the AskUI documentation for specific details and limitations.

    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.