Skip to main content

Locators in Test Automation: A Deep Dive

Locators in Test Automation
Posted On: 10 October 2024

Whether you're testing a simple form or an application, a strong understanding of locators will help you in creating reliable test scripts. Let's understand what locators are, how to identify them, and their various types.

What Are Locators in Test Automation?

In testing or test automation, locators carry out a core purpose: to allow the detection and interaction with web items during the testing process. Whether you're doing manual testing or utilizing traditional automation testing tools, locators are critical for ensuring that your tests appropriately imitate user behavior. They ensure that the application under test (AUT) performs as planned.

How to Find a Locator for Automation?

Finding the right locator for automation involves a few essential steps:

  1. Examine the Web Page: Most browsers have developer tools to explore the components of a web page. Access the browser’s developer tools to inspect the element’s HTML structure.
  2. Select a Locator Strategy: You can use various strategies, including the element’s ID, class name, tag name, or other characteristics such as ‘name’ or ‘title’.
  3. Use the Right Tool: Automation tools often come with features to help you identify locators. For example, ACCELQ provides two distinct methods for Element identification, Locator-Free, and Smart Locator, to allow you to specify the locator strategy. By employing a Locator-Free element identification method, ACCELQ simplifies automation, lowering maintenance requirements and ensuring that scripts are not impacted by modifications made to the HTML structure of the application. The Smart Locator method explicitly maintains an element within the Element Repository, with identification criteria based on the element’s DOM structure.
  4. Check the Locator: Once you’ve discovered a locator, run it through your automation tool to check if it successfully detects the element in question. This step is critical to avoiding flaky tests that will fail intermittently.

Types of Locators in Test Automation

There are several types of locators used in test automation, each with its advantages and use cases:

Locator Type Description Example Usage Pros Cons
ID Locator Uses the unique id attribute of an HTML element. driver.findElement(By.id(“submit-button”)) Highly reliable if IDs are unique on the page. Only works if the element has a unique id.
Class Name Locator Targets elements using the class attribute, often shared by multiple elements. driver.findElement(By.className(“input-field”)) Useful when class names are specific and consistent. May not be unique; multiple elements can share a class.
Name Locator Uses the name attribute, often used in form fields. driver.findElement(By.name(“username”)) Reliable for form elements with unique names. Limited to elements that have a name attribute.
Tag Name Locator Targets elements based on their HTML tag, such as div, input, or button. driver.findElement(By.tagName(“button”)) Useful for broad selections of elements by type. Not unique; usually needs further refinement.
Link Text Locator Selects anchor elements (“<a>”) by the exact text of the link. driver.findElement(By.linkText(“Click here”)) Simple and effective for identifying links by text. Only works for “<a>” elements; text must be exact.
Partial Link Text Similar to Link Text but matches a portion of the link’s text. driver.findElement(By.partialLinkText(“Click”)) Flexible when the full link text is variable. Can lead to ambiguous selections if multiple links share the partial text.
CSS Selector Uses CSS syntax to identify elements by class, ID, attributes, or hierarchy. driver.findElement(By.cssSelector(“.input[type=’text’]”)) Highly flexible and powerful for complex queries. Can be complex and harder to maintain if not simple.
XPath Locator Uses XPath expressions to navigate and identify elements in the DOM. driver.findElement(By.xpath(“//input[@id=’username’]”)) Extremely flexible and supports complex DOM navigation. Can be slow and brittle, especially with complex paths.

How to improve Locators' quality

Improving the quality of locators in test automation is essential for building robust and reliable tests. Here are some strategies to improve the quality of locators:

1. Use Unique Identifiers

  • ID Attributes: Whenever possible, use `id` attributes for locators, as they are usually unique within a webpage. If the application under test doesn’t have unique IDs, suggest adding them.
  • Custom Data Attributes: Consider using custom data attributes (e.g., `data-test-id`) specifically designed for testing. These attributes can be stable and less likely to change.

2. Avoid Fragile Locators

  • Dynamic XPaths: Avoid using dynamic or overly complex XPath expressions that rely on the document structure. These expressions are prone to breaking with minor changes in the DOM.
  • Class Names: Be cautious with class names if they are auto-generated or likely to change due to styling updates.

3. Use Descriptive Locators

  • Readable CSS Selectors: If using CSS selectors, ensure they are readable and descriptive.
  • Meaningful Names: If using name attributes or custom data attributes, ensure they are meaningful and reflect the purpose of the element.

4. Prioritize Stability Over Simplicity

  • Attribute Combinations: Sometimes, combining multiple attributes can create a more stable locator. For example, using both a class and an attribute like `data-test-id` can provide extra robustness.
  • Text Content: When applicable, use visible text to locate elements, but ensure that the text is stable and not subject to frequent changes or localization.

5. Regularly Review and Refactor Locators

  • Maintenance: Regularly review locators to ensure they are still relevant and optimal. Refactor locators as the application evolves to maintain test reliability.
  • Automated Checks: Implement automated checks to identify outdated or broken locators, which can be particularly useful in large test suites.

6. Collaborate with Development Teams

  • Early Involvement: Engage with development teams early in the design and development process to advocate for testability, including stable and unique locators.
  • Shared Understanding: Ensure there is a shared understanding of the importance of stable identifiers and encourage the use of best practices like semantic HTML and data attributes for test automation.

7. Monitor Locator Performance

  • Execution Time: Monitor locator performance during test execution. If certain locators consistently slow down tests, consider optimizing them.
  • Error Rates: Track the error rates associated with specific locators. High error rates might indicate that a locator is too fragile or not well-suited to the application’s structure.

Conclusion

Locators are key to ensuring that your testing process is robust and accurate. ACCELQ is our AI-powered test automation platform that simplifies automation by employing a Locator-Free method of element identification. Your team doesn’t need to worry about identifying the locators that lower maintenance requirements. This approach allows for faster and more intuitive script creation during sprints by increasing script robustness, reducing false positives, and improving readability. The platform’s codeless automation and record-and-playback features make it easy for non-technical users to create and manage locators. It also supports dynamic locator handling, allowing for flexibility with changing elements. To learn more about ACCELQ or for a free trial, click here.

Balbodh Jha

Associate Director Product Engineering

Balbodh is a passionate enthusiast of Test Automation, constantly seeking opportunities to tackle real-world challenges in this field. He possesses an insatiable curiosity for engaging in discussions on testing-related topics and crafting solutions to address them. He has a wealth of experience in establishing Test Centers of Excellence (TCoE) for a diverse range of clients he has collaborated with.

Discover More

Top continuous integration toolsBlogCI/CDTop 10 Continuous Integration Tools In 2024
11 July 2024

Top 10 Continuous Integration Tools In 2024

Discover the continuous integration tools tailored for DevOps teams. Compare the features and select the best CI/CD tool for your project.
Key CI/CD Pipeline TrendsBlogCI/CDKey CI/CD Pipeline Trends to Watch in 2024
29 March 2024

Key CI/CD Pipeline Trends to Watch in 2024

Learn about CI/CD Pipelines trends that are reshaping software delivery for faster, more secure, and efficient.

Get started on your Codeless Test Automation journey

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

Close Menu