What is the proper way to uncheck a checkbox in Capybara integration tests? I have a list of checkboxes that I need to uncheck. I have selected them all using all:
checkboxes = all("input[type='checkbox']")
In order uncheck each one I use each. According to the information I have found on the StackOverflow there should be three ways to uncheck a checkbox:
uncheck(field)
field.click
field.set(false)
In order to test whether these different methods I created the following tests (each of these was in a separate scenario on the same page):
Using uncheck it succeeded:
checkboxes = all("input[type='checkbox']")
checkboxes.each { |field| expect(field.checked?).to eq("checked") }
checkboxes.each { |field| uncheck(field[:id]) }
checkboxes.each { |field| expect(field.checked?).to eq(nil) }
puts page.body
save_and_open_page
Using click it failed, so I assume this is an improper way of unchecking the field:
checkboxes = all("input[type='checkbox']")
checkboxes.each { |field| expect(field.checked?).to eq("checked") }
checkboxes.each { |field| field.click }
checkboxes.each { |field| expect(field.checked?).to eq(nil) }
puts page.body
save_and_open_page
Using set(false) it succeeded:
checkboxes = all("input[type='checkbox']")
checkboxes.each { |field| expect(field.checked?).to eq("checked") }
checkboxes.each { |field| field.set(false) }
checkboxes.each { |field| expect(field.checked?).to eq(nil) }
puts page.body
save_and_open_page
I figured I would just use uncheck, but then I noticed something strange: the page.body still showed the checkbox as checked, even though I had asserted in the test that checked? was nil. save_and_open_page showed the same thing. It appeared as if the checkboxes had not changed since the page was loaded.
Questions:
- Why aren't
uncheckandset(false)altering the form HTML? In order form my test to pass some element associated withfieldhad to be unchecked. Is there some disconnect betweenpage.bodyand whatuncheckdeals with? - What is the canonical way of unchecking a checkbox? Since
uncheckis part of the API and is easy to use it seems to be the proper function to call for this sort of thing, but the fact that it's not changingpage.bodyconcerns me.
Related questions:
Thanks in advance!