Reputation: 27553
I have a list of checkboxes, created using collection_check_boxes
.
When testing this in a feature/integration test, using Capybara, and want to "normalize" the page by unchecking them all, then checking the ones I want checked:
within_fieldset('Product') do
# Reset all checkboxes for a level playingfield.
# What to do?
# Mark checkboxes for products enabled
products.each do |product|
check products
end
end
This is in a so-called PageObject, hence I want to somewhat generic: were this in the actual test, I would know which fields were checked and uncheck them. But this more generic helper has no such knowledge.
I've tried something along the lines of find('input[type=checkbox]').all {|checkbox| uncheck(checkbox) }
, which should work but seems rather convoluted for the task at hand, not?
Is there not some uncheck_all()?
that I missed, in Capybara? Is it a common pattern to "Reset" a form in capybara to a blank state before starting to fill_in forms?
Upvotes: 6
Views: 3637
Reputation: 351
This is almost identical to your solution, but might be slightly more legible, and I think also works with custom JS checkboxes. As far as I know there isn't anything like an uncheck_all
method.
all("input[type='checkbox']").each{|box| box.set('false')}
Upvotes: 3
Reputation: 4178
Try this:
all('input[type=checkbox]').each do |checkbox|
if checkbox.checked? then
checkbox.click
end
end
Upvotes: 10