# Best Practice
# How to use hints
# Best Practices for Complex Element Selection in Test Automation
When automating tests for complex applications, it can be tempting to rely on XPaths for element selection, especially when Virtuoso QA's Natural Language Programming (NLP) commands seem to struggle. However, over-relying on XPaths often leads to unstable, flaky tests, particularly in more dynamic environments. Here’s how to create more robust element selectors with Virtuoso QA.
Understand How Virtuoso Identifies Elements
Virtuoso QA offers two primary methods for targeting elements: NLP hints and selectors like XPath. The "magic" of NLP hints lies in their adaptability. When a step is written with only an XPath, the execution depends entirely on the stability of the page and element. On the other hand, Virtuoso QA can fall back to NLP hints to probabilistically identify elements if the XPath fails, which makes tests more reliable.Crafting Good Hints
A good hint balances between being too exact (like XPath) and too ambiguous (e.g., using a common term like "Add" when there are multiple instances). Consider unique identifiers or contextually distinct labels. For instance, instead of using a customer name like "Virtuoso QA" (which might appear in multiple records), opt for a unique element ID or a combination of contextual identifiers.Handling Dynamic Elements
Dynamic elements, such as those that append a unique reference number, can be challenging to target with descriptive text alone. In such cases, combining contextual NLP hints with partial identifiers (e.g., using a partial element ID) can significantly improve stability.Managing Flaky Click Actions
Sometimes, Virtuoso QA can "see" an element but struggles to "click" it, due to differences in element definitions. If an element is not explicitly defined as clickable, Virtuoso QA may default to another option with a higher "clickable" score, leading to incorrect clicks. To mitigate this, using a "store element details" step to precisely identify the element before clicking can be a more reliable strategy.Addressing JavaScript Events in Input Fields
Some input fields require JavaScript events to trigger before their values are included in a form submission. If Virtuoso QA’s usual "write" or "press" commands don't seem to trigger the event, try clicking on a non-clickable element, such as a title, to force the JavaScript event to run.Using Input Field Titles vs. Element Hints
Titles can often be used to locate input fields, but some applications (like Salesforce) use complex labels, making this approach unstable. In such cases, inspect the element structure to find more reliable attributes, like unique IDs or class names, to use as hints.
By following these best practices, you can create more robust and reliable tests with Virtuoso QA. While testing complex applications may require a bit more effort, these approaches can significantly reduce flaky test behavior and improve long-term stability.
# Read more
For further details, check out this article in our Knowledge abd Support Portal: