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

ORACLE ERP Cloud Testing-ACCELQBlogEnterpise App Test Automation
27 June 2022

Best Practices to perform end-to-end Oracle ERP Cloud Testing

Oracle ERP Cloud is a True cloud SaaS and modularized ERP (Enterprise Resource Planning) application that enables continuous digital innovation without end-of-life and implementation disruption required for today’s business. Compared…
API Automation testing-ACCELQBlogTest Automation
1 July 2022

How to Use ACCELQ for API Automation Testing

Modern software applications typically consist of three main layers: the Data layer, the API (or service) layer, and the User Interface (UI) layer. The API layer contains the business logic…
Introduction to integration testing in salesforce-ACCELQBlogEnterpise App Test Automation
18 November 2022

Your Comprehensive Introduction to Integration Testing in Salesforce

Integration testing in Salesforce is critical to ensure that the Salesforce platform and all its modules are working properly, sharing data seamlessly, and offering a unified user experience.

Get started on your Codeless Test Automation journey

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

Close Menu