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...

Standlone api vs integrated api tests-ACCELQBlogTesting
28 March 2023

Standalone API Tests vs. Integrated API Tests

Standalone and integrated API testing both play a crucial role in improving the overall quality of any software product.
Agile Testing Trends to watch in 2023-ACCELQBlogTesting
14 November 2022

Agile Testing Has Evolved! Trends To Watch In 2023

By applying agile testing principles, enterprises can build a reliable foundation for their digital products. Here are the trends to watch!
Mobile Testing- ACCELQBlogTesting
27 March 2022

What is Mobile testing? The perfect approach

Mobile testing ensures the intended working of mobile applications by manual or test automation tools using various testing frameworks.

Get started on your Codeless Test Automation journey

Talk to ACCELQ Team and see how you can get started.

Close Menu