TIL: Working with Long Element Ids with Capybara
I'm often finding that I'm testing web interfaces which have elements with extremely long identifiers. Hey, I'm usually testing interfaces generated by Rails and with custom form generators.
An id of the form #generator_application_name_screen_name_field
is not unusual. Much of that id will alway be static and we can use Capybara's matchers to help us kill the cruft.
If I wanted to find all the elements that had an id with a prefix:
all("input[id^='generator']")
or I hunt an element which I know has a suffix:
find("input[id$='email']")
find("input[id$='email']").set('my@email.test')
If I'm searching on the name of the field, I can use the same tricks and also look for matches in the middle of the name, for example "contact form email address field":
find("input[name~='email address']")
or, if separated by dashes, for example "contact-form-email-address-field":
find("input[name|='email']")
Member discussion