We'd like to know what you view as best practices and what has worked for your projects
My organization is in the beginning phases of creating a Selenium test suite, and have had some early discussions about how to ensure that our tests are robust.
In one of the first tests, we modified the markup under test to facilitate testing:
The original markup was something along the lines of:
<div><a href="someLink"><i class="fa fa-someicon"></i>Some Text</a></div>
And the author of the test added an ID to the anchor tag:
<div><a id="SomeId" href="someLink"><i class="fa fa-someicon"></i>Some Text</a></div>
A few points of view emerged:
Adding anything to the markup solely for the purpose of facilitating testing is a bad thing.
a.) It clutters the markup, which hurts front-end developers (i.e. where is this id even being used?).
b.) It increases the payload being sent to the clients.
Adding an id to an element to facilitate testing is okay.
a.) It allows for more robust selectors (traversing the DOM / using a XPath can be much more brittle and difficult to read).
b.) Assuming that you use unique id's, there's no material downside to adding identifiers to elements on a page.
Adding another attribute (i.e. data-testing-id) is preferable to adding an id.
a.) While it clutters the markup and is also sent in the response to clients, the purpose of the attribute is very clear.
b.) Similar to point 2, you can write more robust selectors.