Skip to main content

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. The 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 time 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 the 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 a 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; a 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 the 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.

This Might Also Interest You...

BlogSeleniumVideo
4 December 2020

ACCELQ and Selenium – A match made in heaven

As the software quality and testing processes are rapidly making the transition from manual to automated testing, many commercial tools are helping organizations to move towards automation. Selenium is one…
BlogCodeless Test AutomationTest Automation
22 April 2020

Perils of Record and Playback Test Automation

One prevalent practice since the early days of test automation has been the concept of record and playback. This process is marketed by automation tool vendors (against controlled demo environments)…
BlogSeleniumTesting
15 November 2020

The Selenium Story – Ingredients for a Sustainable Adaption

For the last 2 decades, Software quality assurance has been making sustained but slow progress from manual testing towards automated testing. During this cycle, multiple commercial tools and vendors have been…
Close Menu