0
votes

Inside a capybara test, I need to fill in a text field that doesn't have a unique id or class attribute. The text field (the field called title in this case) can appear anywhere on the page. The only thing we know is that the text field is wrapped in a div and this div sits immediately after an h3 tag which has the content "Title"

<h3>Title</h3>
<div class="input-row clear">
  <input id="ember5046" class="ember-view ember-text-field" type="text">
</div>
<h3>Work Location</h3>
<div class="input-row clear">
  <input id="ember5048" class="ember-view ember-text-field" type="text">
</div>

How can I do it?

We are not allowed to use xpath (company policy)

We are not allowed to do index based selectors like all("input")[0]

1
"We are not allowed to use xpath" why? Also - why don't you ask whomever made this silly rule to explain how you can do it ;)Taryn East
Secondly - is there no way you can alter the html? if you can put a unique id/class in a surrounding div, then you'd be able to do itTaryn East
If you decide that XPath is allowed, there was another question today with the exact same problem. That question includes an XPath solution.Justin Ko
find('#id').set 'text' assuming your ids are not uniquely generated on loadbcar

1 Answers

0
votes

I think it should be possible with the css adjacent siblings combinator selector. Something like:

h3[text=Title] + div

This gives you the next element matching the thing after the +. If you rather want everything that matches you could use the ~ operator instead. These siblings selector can only help you to find things after a certain node. Going backwards is not possible as far as I know.