How to check a checkbox in capybara? How to check a checkbox in capybara? ruby ruby

How to check a checkbox in capybara?


I found the following worked for me:

# Checkfind(:css, "#cityID[value='62']").set(true)# Uncheckfind(:css, "#cityID[value='62']").set(false)


It's better not to create multiple elements with the same id, so that (and not only for that) you can easily check/uncheck a checkbox with elegant

check 'cityID'uncheck 'cityID'

If one can not avoid multiple elements with the same id and still needs to check a checkbox with certain value, he can do so with

find(:css, "#cityID[value='62']").set(true)find(:css, "#cityID[value='62']").set(false)

More information on capybara input manipulations can be found here


When running capybara test, you got the page object. This you can use to check/uncheck any checkboxes. As @buruzaemon already mentioned:

to find and check a checkbox by name, id, or label text.

So lets assume you got a checkbox in your html like:

<label>    <input type="checkbox" value="myvalue" name="myname" id="myid">  MyLabel</label>

You could check this with:

page.check('myid')page.check('MyLabel')page.check('myname')

Uncheck is the same just use page.uncheck method.