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?
Try this:
all('input[type=checkbox]').each do |checkbox|
if checkbox.checked? then
checkbox.click
end
end
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')}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With