2

I am finding it really hard to locate stuff I am testing when its deep nested, etc.

Is it a bad idea to use something

<h1 name="test-main-title">About Us</h1>

and just use

element(by.name('test-main-title'));?

Later, for the prod build, I can remove name="test-*" from my HTML files. Just want to knwo if that is a terrible idea before I start doing it.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
user1354934
  • 8,139
  • 15
  • 50
  • 80

1 Answers1

4

The idea itself is similar to modifying application or a specific application build for testing or staging to make the testing easier. The idea itself is quite common and has its pros and cons. I don't though particularly like the idea of changing existing attribute values for that. Instead, think about adding meaningful ids and data-oriented classes or other attributes.

ids specifically have multiple advantages - they are less likely to be changed, they are unique (at least they should in theory), they are data-oriented (meaning, you probably would not have an id equal to col-sm-6) and they are the fastest way to locate elements.

Also, here are the related threads with more ideas:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • thank you! I think I will keep using name (or should I use id? does it matter?) because I think it will be easier for me. – user1354934 Aug 09 '16 at 23:36
  • @user1354934 yeah, sorry for multiple edits - I should probably kill that perfectionist inside me always complaining smth is not good enough. – alecxe Aug 09 '16 at 23:38
  • may i ask one more question? so what exactly should I be testing with protractor? i'm trying to learn all this stuff. Is it mostly for where you have data, or for all kinds of stuff? sorry if its to general a question :( for example i wrote a test to make sure i am on the right url, that the h1 title is correct, and that the button that's part of a card my repeater renders reads "add" – user1354934 Aug 09 '16 at 23:39
  • @user1354934 sure. Well, this is difficult to answer cause there is no silver bullet and it is application-under-test specific. If you don't have any tests at this point I suggest focusing on the most critical parts of the application first, or things that are most likely to break, or where the "money" is :) – alecxe Aug 10 '16 at 00:06
  • ah i see. thanks! wasn't too sure from the documentation what exactly i am suppose to test. – user1354934 Aug 10 '16 at 00:08
  • ID are unique not only in theory: also in practice. As in: if ID is not unique, locating element by non-unique ID fails. That allows it to be so fast. – Peter M. - stands for Monica Jun 23 '17 at 16:59