Ways to avoid brittle element locators

Chris ColosimoChris Colosimo Posts: 100 admin
edited September 2019 in Selenic

Website recorders do their very best to select the right element locator as you navigate through the site but in many cases the locators that get recorded are non-ideal and contain dynamic information that would otherwise be too specific for example a brittle xpath.
Common examples

  • IDs that change every time you visit the page
    o Often these are seen an Salesforce and SAP applications
    o <div id="gwt-uid-198">Click here for more info</div>

  • Text that differs based on the user that is loged in
    o Username as a log out button
    o <a href="/logout.jsp">Logout user (bob)</a>

  • Locators that are not resilient to page change
    o Positional locators Pictures on a shopping app

What often happens is that the locator chosen made sense for that particular state but then on playback things might have changed or the locator is picking up a different object.

Here is how to make that less painful

There are many solutions out there that are striving to solve this exact challenge. The key is in capturing the most intelligent locator possible when initially building the test script. I recently wrote a blog outlining 10 great tools for UI testing, many of these tools have record and playback capability. Additionally, there are plug-ins for Chrome that can help during script creation to extract just the right element locators right out of the page. Two specific tools that come to mind are SeleniumIDE and TruePath.

Sign In or Register to comment.