Reputation: 733
I'm building a list of user to send emails to, which is coded as (haml, though that's not the point)
- @users.each do |user|
.field<
= check_box_tag "user_ids[#{user.id}]",user.id
%nbsp
= user.name
The code works fine (though it can be probably refactored). However, I have problem writing Capybara tests because object ids change over time. For instance, I have the following cucumber test:
.....
Given user "User one" exists
And "User two" esists
......
When I check "user_ids_152"
And I check "user_ids_153"
.....
I get Capybara::ElementNotFound: cannot check field, no checkbox with id, name, or label 'user_ids_152' found
since user id is now 272. I searched and couldn't find a suitable solution here on SO.
Suggestions?
Upvotes: 1
Views: 938
Reputation: 3015
I'm not sure of your requirements, but adding an HTML label for your checkbox would make it easier to consistently check via Capybara:
%label{ :for => "user_ids_#{user.id}"}
= check_box_tag "user_ids[#{user.id}]",user.id, :id => "user_ids_#{user.id}"
= user.name
Then you could do:
When I check "User one"
I should add that the more you write well-formed, standards-compliant HTML, the easier it will be for you to use something like Capybara in the long run. The rails view helpers do a good job of helping you along if you use them, but just take note of any HTML you are generating outside of them.
I like Bullet Proof Web Design as a primer for that:
http://www.simplebits.com/publications/bulletproof/
Upvotes: 1