5 tips for a stable element ID in Selenium automation

Element Identification is a crucial step in achieving automated test interaction in Selenium against browser based applications. Poor element identification design results in failure to identify elements at run time thus leading to stability issues. Entire test execution could come to a halt with just the username field not being identified at runtime.

This consideration is even more significant for today’s day and age applications with rich user interactions. Layout of webpages is extremely complex and often dynamic in nature. No more are the days you could recognize an element with just its ID or name and assume it stay static.

It is very much a recognized fact in the Selenium community that test automation coding should follow proper design and programming practices. But when it comes to element identification, design effort is often overlooked. Most of the times element ID is setup based on ad-hoc considerations without a rigorous thought. Here are some best practices to consider while setting up element identification in Selenium.

 

  1. Use minimal number of attributes possible
    It is always a good practice to use least possible number of attributes to identify an element. Pick the minimum set of attributes, thoroughly considering their long-term stability. Don’t be tempted to use more number of attributes, just because they all appear meaningful and reliable. Unintended (non-functional) changes to the application are not avoidable; and you are increasing the chances of failure with greater number of attributes.
  2. Good neighbors may not be all that uncommon
    As you analyze the element for identification properties, sometimes the neighborhood may offer much better stable attributes than the element itself. Be sure to check-out the surroundings before deciding on the identification criterion. Ancestors, descendants or siblings could be involved in an element ID.
  3. Be wary of false uniqueness
    Thoroughly consider the attribute value before picking an attribute for element ID. Analyze possibilities of variations across test runs. Try out with multiple instances of the web page, if in doubt. Sometimes, the variations may not be all that obvious. Application functional knowledge may also be helpful to decipher the attribute values and decide on the stability. ID values with special characters or class names related to the UI framework such as “ng-“, may not be suitable for identification.
  4. Get a handle on patterns
    Regular expression patterns come in very handy to define attribute values. With dynamic data and workflows presented on the applications, it is a very common use case for pattern based matching. Review and practice common regex patterns and syntax; lot of times, a simple regex is all that you need to build stable identification.
  5. Avoid path based reliance
    Unless there is no other way to ensure uniqueness of an element, do not use xpath type of path based identification. DOM structure is very much an application page design concern; avoid hardwiring to the exact path of an element in the DOM. Use CSS or JQuery based selectors as much as possible.

With a small upfront effort and discipline while designing element identification, you can achieve a substantial improvement in your Selenium test execution reliability and thus avoid a lot of rework.

Leave a Reply

Your email address will not be published. Required fields are marked *