Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easily uncheck all checkboxes with Capybara

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?

like image 329
berkes Avatar asked Dec 20 '22 10:12

berkes


2 Answers

Try this:

all('input[type=checkbox]').each do |checkbox|
 if checkbox.checked? then 
  checkbox.click
 end
end
like image 185
Vishal Aggarwal Avatar answered Jan 01 '23 18:01

Vishal Aggarwal


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')}

like image 38
kaimonster Avatar answered Jan 01 '23 19:01

kaimonster