Testing Angularjs with Selenium without element id

If you are using Selenium WebDriver there are many ways to find elements on a page. XPATH is probably one of the better approaches as far as flexibility goes (though it can get ugly quickly) but WebDriver also allows you to use Class Names, Tag Names, Name, Link Text and CSS.

Selenium WebDriver - Locate UI Elements


We have encountered the same situation in our work now. As mentioned in this discussion, we compared the possible two approaches to overcome this issue:

  1. Try and locate the elements differently.
  2. Introduce new attributes where needed, even though they not required by Angular development.

We decided to go with (2) and add IDs where needed. Tests robustness in much better and the overhead is not significant at all.

What we did exactly? To the relevant elements, attribute named data-hook was added with the proper value that identify him. Works great.