Nightwatch, select the second item with the same input type

I am writing tests to run in nightwatch using javascript. I have two password fields in the same form I fill out to register for a new account. I select them using the id of the form concatenated with input[type=password]

and then using .setValue()

to insert some kind of string into them.

The problem is the difference between these two password fields. I dont want to use id or class to select them because I dont want it to be css dependent this way. This is because the test is for a live website that developers are constantly working on, and every time the css name changes, the test has to be changed if it depends on the id of the elements and not their type. I have no control over many developers and the names that they change and do not change. My only option is to write my tests independently of the css as I can. If I can select the password fields using their type, I will only depend on the form name used to select the password field, username field, and login button.

Is there a way to do this? Thanks in advance.

+3


source to share


2 answers


I'm not sure if you can do this intelligently without relying on "any" css form. HTML is a representation of the DOM, so you at least have to work in that aspect.

However, you can detach from using specific ids, attributes and only work on DOM class elements.

For example, if you have the following:

<form>
    <input>a</input>
    <input>b</input>
</form>

      



You can just do "form > input:nth-of-type(2)"

as your selector. Therefore it will always grab the second element input

below the direct childform

http://www.w3schools.com/cssref/sel_nth-of-type.asp

This assumes that the structure is locked and does not always change.

Hope it helps!

+4


source


you can think of <input>

as arrays that count from 1 so if you want to get the second input just do



.setValue('//form[@id="yourformid"]/input[2]','blablabla')

      

0


source







All Articles