1

I'm new to test automation with Selenium (Python) and just wondering if it makes sense to locate elements on webpages via a custom attribute?

In some places the IDs of the elements are generated and have a new ID the next time the page is opened.
Names and classes can sometimes change and are not unique.
The developer offered me to add an additional attribute to the elements that I can use for the test automation.

For example:

<a href="..." name="my-link" data-test-id="my-unique-test-id">Link Text</a>

I found the idea quite charming because it is completely detached from developer purposes. This attribute is used explicitly for locating purposes in test automation.
Unfortunately, I have currently found only the way to address custom attributes via XPath. And that should be much slower than identifying elements by ID or name.

Can you please share your experiences with me?
Is there any library in Python that I can use to find such a custom attribute more efficiently?
Have you found a better way to locate elements?

2
  • You don't need XPath, you can select by any attribute using CSS selectors: [data-test-id="my-unique-test-id"]. But I doubt it's substantially slow given all of the other overhead of E2E testing; have you actually had a problem?
    – jonrsharpe
    May 16, 2019 at 21:44
  • Hi @jonrsharpe, thank you for your answer. I have not found any problems yet, but I'm just starting to build my testing framework. And that's why I first wanted to define how I locate elements. I work in a small company and I am very close to development. At first, I found it very helpful that the developer offered me to implement my own identifying attribute wherever I need it. Unfortunately, I have found almost no reports from testers who have gone the same way. The only thing that I found is that searching for ID or NAME should be faster than searching via CSS selectors or XPath.
    – Matze
    May 17, 2019 at 12:19

1 Answer 1

1

When using selectors i try to assume nothing. By.cssSelector("form button") is a bad selector because the frontend might change the button to an input[type="submit"].

If possible, i try to mark all my test intends with specific classes. Like By.cssSelector(".e2e-contact-form .e2e-submit"). This only assumes that there are elements with these classes, independent of location or element type. Another advantage is, that when the frontend devs changes the design, your selector can stay the same. Frontend should use their own classes to change the design. Additionally, when frontend changes code surrounding your "e2e" classes, they are aware that this may impact e2e testing and can notify you in advance.

But i guess a custom attribute would work as well... though it bloats your css selectors to By.cssSelector("[data-test-id: \"my-unique-test-id\"]")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.