Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capybara: find(element) using selector to target a complex attribute name

Using cucumber and capybara to test a rails app. Assuming I cannot change the markup, can I use capybara to select the following select in a page full of similar tds and selects?

<td>
  <select name="attributes[ruby][category]">
    <option value="2" selected="selected">Languages</option>
    <option value="3">Communication</option>
  </select>
</td>

This seems to fail (I assume because of the nested "[" and "]").

find("select[name=attributes[ruby][category]]")

Escaping doesn't work either. Thoughts?

like image 863
Pierre Avatar asked Oct 05 '11 20:10

Pierre


3 Answers

You can try find('select', :name => 'attributes[ruby][category]') or find_field('attributes[ruby][category]').

like image 100
maro Avatar answered Nov 01 '22 11:11

maro


I think you need to quote the attribute value:

find("select[name='attributes[ruby][category]']")

but maro's suggestion of using find_field is a cleaner approach.

like image 42
Andy Waite Avatar answered Nov 01 '22 09:11

Andy Waite


More generally you can use XPath

find(:xpath, "//select[@name='attributes[ruby][category]'")

This approach has the advantage that it can be used for any attribute.

like image 2
Baruch Avatar answered Nov 01 '22 09:11

Baruch