User Observable Locator Controversy

User Observable Locator Controversy

Generally test automators prefer to identify elements using the attribute: id, because it is unique on the page but also because it is less likely to change than text that is visible on the page. But I was intrigued by a brief post by Mark Borcherding in which he complains that "The ID isn't a user-observable attribute; however the text in the label is".

I can see that even though the text is more likely to change than the ID, it is immediately observable by the user and should be an obvious change. Where as if the developer changes the ID and doesn't communicate it, then it is more likely to result in a failed test; which takes time and effort to track down and repair.

No alt text provided for this image

The other desirable trait of using text is that sometimes you don't have a nice ID to use to identify an element. Often the element is some kind of child of an array. If you identify it by the exact relationship to the parent element, then if the layout gets changed, your test will fail. With a library like Capybara or SitePrism (Ruby gems) it is very simple to use the observable text without regard to the layout. Of course if the same text is used elsewhere on the page, then you need to indicate which instance you intend to target.

I am interested in the opinion of those that have far more experience with test automation than I have. Please comment with your experience and advice as to which is better. Thank you.

UPDATE: Newer browser test automation frameworks embrace using visual components. Read my post at https://www.dhirubhai.net/pulse/semantic-locators-eric-thomson/

?

ID is not a good idea as it makes it a unit test not a UI test as you are testing the internals of the page rather than how the page interacts with the user. Many test automation developers don't understand the difference and end up writing two types of tests in one - which is never a good idea. Most use ID cause it's "easier" - which just translates to "I'm lazy". Having "element + field + label name" means that it is very close to how a user would reference the field, e.g. when I click on the field next to the words "email" then i enter my email address. HTML spec says that a page cannot contain elements with the same ID (although due to lazy web devs over the years browsers allow it). If you are creating a component-based page with IDs you end up with "Component1", "Component2" or "ParentX_ParentY_Component", "ParentA_ParentB_Component" - neither way is more maintainable nor readable. A user would say "the email box down the bottom of the page not the one at the top" - so your find logic could indicate page location as well. Also if the page is to be translated into different languages using ID will always pass - however that may not be correct if the replaced text is the wrong way around (e.g. if "email" actually reads "name" in a different language - test passes but functionality is incorrect.) Not using ID makes it a little trickier to write the test - but ensures that the test is actually testing user "interaction" (what UI testing should really stand for), not browser rendering.

回复
Zhimin Zhan

Author | Test Automation Coach | Solopreneur l Speaker | Trainer | Freelance Blogger | International Award-winning Software Engineer | I help teams succeed with Agile/DevOps by implementing real Continuous Testing

8 年

I like using ID better, a simple reason is maintainability. From my experience, test maintenance (if projects are serious about test automation) requires much more effort than writing one (and pass it first time). Using ID does not necessarily equal to "less readable" (yes, less user - observable). The code "element :email, :field" comes from the perspective of A Page contains a list of elements. I see a Page is a collection of related operations (not attributes) user can perform one a web page. For example, in my page class, I will use def enter_email(email) driver.find_element(:id, "email_address").send_keys(email) end This way, I have flexibility of choosing most stable locator, send tab keys, add minor delay, scroll down to the make the element viewable and execute javascript in the function. As it is standard function, some testing IDEs may support auto-complete, refactoring, ..., etc At test script level, login_page.enter_email("[email protected]")

回复

要查看或添加评论,请登录

Eric Thomson的更多文章

  • Semantic Locators

    Semantic Locators

    Back in 2016 I wrote a post titled User Observable Locator Controversy Which asked the question, is it better to use…

  • Firefox 48 Broke Selenium - 2 Step Solution

    Firefox 48 Broke Selenium - 2 Step Solution

    This is getting annoying, every time Firefox makes another update it breaks my Selenium tests. I don't want to keep…

  • 3 Steps to Introduce Automation to Manual Testers

    3 Steps to Introduce Automation to Manual Testers

    The topic of test automation can be overwhelming, so how can you get started? Last Friday I attended the IIST Webinar:…

    2 条评论
  • Update Firefox and Selenium

    Update Firefox and Selenium

    The other day, my selenium tests quit working with Firefox 47 and I posted a big work-around (link). which required an…

  • Firefox 47 Sink Your Selenium Scripts?

    Firefox 47 Sink Your Selenium Scripts?

    I have a habit that before starting to write any new test automation scripts, I run one that I know was working…

    2 条评论
  • Modern Testing @ TCQAA

    Modern Testing @ TCQAA

    Last night, Andy Tinkham gave his first talk about this topic, after after attending a Reinventing Testers workshop…

  • 4 Hurdles to Clear for Test Automation

    4 Hurdles to Clear for Test Automation

    The fear of implementing test automation prevents many top notch QA teams from implementing an automation practice to…

  • Smart Testing Ideas

    Smart Testing Ideas

    On the way to work this morning I listened to an interview with Christin Wiedemann on TestTalks (for the second time)…

    1 条评论
  • Three Amigos Collaboration

    Three Amigos Collaboration

    I recently read Bob Galen’s post, The "3 Amigos" in Agile Teams. He explains that this term has been around since 2009,…

  • Awards and Recognition

    Awards and Recognition

    After a couple weeks of high stress and long hours I just finished testing a high profile project. At our team meeting,…

社区洞察

其他会员也浏览了