debayan chakravarty
debayan chakravarty

Reputation: 115

ID's Required for UI automation in selenium in all html pages

There is a project where we are going to automate the UI but the Automation team is suggesting that we have to use ID's all over the page so that it will be easy to automate there script.

My Question here is why we will use ID's everywhere ? hampering the Html and Css structure.

The webpage can be automated without ID's in html yes or no ?

Upvotes: 2

Views: 6066

Answers (2)

Vladimir Efimov
Vladimir Efimov

Reputation: 799

Yes, a web page can be automated without ID's. For example, you can play with cssSelectors here https://www.w3schools.com/cssref/trysel.asp (note that example page has elements with and without ids)

Using ids for element's lookup in automation is generally considered as a best practice. If you use ids your automation tests will become independent of html structures which basically will make them more stable.

For example, in the first version of your app you may have some text implemented as

<p id="someTextId" class="someClass">Hello world</p>

but at some point may decide to rewrite it as (change the tag and even apply different class name)

<div id="someTextId" class="anotherClass">Hello world</div>

In case you rely on id #someTextId to locate an element your test will still be able to access necessary element and interact with it properly. If you use p or .someClass your automation test will fail to find an element even though from the ui perspective the same text will be displayed in a browser.

We faced several downsides of using id:

  1. Some frameworks do not recommend using them or generate them automatically. (Potential issues with ids described here https://www.javascriptstuff.com/use-refs-not-ids/, https://www.quora.com/Are-IDs-a-big-no-no-in-the-CSS-world, https://dev.to/claireparker/reasons-not-to-use-ids-in-css-4ni4, https://www.creativebloq.com/css3/avoid-css-mistakes-10135080, https://www.reddit.com/r/webdev/comments/3ge2ma/why_some_people_dont_use_ids_at_all/)
  2. Some other logic may rely on them, so changing/adding them for the need of automation may somehow affect other app logic unexpectedly. What you can use instead of id is some other attribute. For example in our projects, we have switched from id to a specific attribute named dataSeleniumId. It clearly shows that the attribute is selenium tests usage only. As a next step, you can add a rule in your team when someone changes or removed dataSeleniumId attribute he should inform automation testing team about it. As changing/removing this attribute will lead to test failures and to avoid any false failures it is better to fix it in advance.

Upvotes: 3

Infern0
Infern0

Reputation: 2814

For an automation developer its much easier to browse trough the html code and see the id of specific button/text field/etc.. to implement the relevant locator inside the automated test.

In most cases, the project start to receive duplication of classes or complicated nested elements. This make the life of automation dev harder, because of writing xpath or css selectors, verify that they work and this locator finds only 1 unique element.

Its up to the team and code style suggested from the team leader.

Back on the question, yes the website can be written without id's but if the goals is to automate large part of the website, id's would be great helper to the automation dev team.

Upvotes: 0

Related Questions