How to check a checkbox in capybara?

capybara cheat sheet
capybara have_field
capybara commands
unable to find checkbox capybara
capybara submit form
capybara have_content visible
capybara check element exists
capybara fill out form

I'm using Rspec and Capybara.

How can I write a step to check a checkbox? I've tried check by value but it can't find my checkbox. I'm not sure what to do, as I have in fact same ID with different values

Here is the code:

 <input id="cityID" type="checkbox" style="text-align: center; opacity: 0;" value="61" name="cityID">
 <input id="cityID" type="checkbox" style="text-align: center; opacity: 0;" value="62" name="cityID">
 <input id="cityID" type="checkbox" style="text-align: center; opacity: 0;" value="63" name="cityID">

I found the following worked for me:

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

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

How to check a checkbox in capybara?, There are multiple ways depending on exactly what you're trying to do - if you've already found the element and just want to know whether or  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.

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

how to check whether the check box is checked or not capybara Rspec, Method: Capybara::Node::Actions#check. option (String) — Value of the checkbox to select. id (String, Regexp) — Match fields that match the id attribute. name (String) — Match fields that match the name attribute. class (String, Array<String>, Regexp) — allow_label_click (Boolean) — wait (false, true, Numeric) — Your first example would require a # in front of the id since you're using a :css selector find an id and page.check doesn't take a symbol as it's first parameter - to use page.check it would be page.check('YOUR_CHECKBOX_ID') - and calling .click on the checkbox is not the same as calling .check -- check will leave a checkbox selected if it already is - click would toggle it.

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.

Method: Capybara::Node::Actions#check, The test should run and when it finds the checkbox with a label of English, the checkbox should be checked. But, it doesn't work. After many  check “English” That’s it. The test should run and when it finds the checkbox with a label of English, the checkbox should be checked. But, it doesn’t work. After many attempts at making this work and more Google searches than I can remember..I ended up at the Capybara mailing list. The Solution

I think you may have to give unique ids to your form elements, first of all.

But with regards to Capybara and checkboxes, the Capybara::Node::Actions#check instance method will allow you to find and check a checkbox by name, id, or label text.

When You Can't Check a Checkbox using Capybara, Parent Note My Capybara note n350071 ・ Se. will check a descendant checkbox with a name, id, or label text matching 'German'  Go to Capybara::Node::Actions, and here it lists how you can attach a file, check a checkbox, choose a radio button, click a button, click a link, click a link or a button, fill in a text box field, select a dropdown, uncheck a checkbox or unselect a dropdown.

If the box is associated with text, e.g. 'Option 3', then as of capybara 3.0.3 you can just do

check 'Option 3'

checkbox in Capybara - DEV, check('A Checkbox') Correct method name is click_on , not click; default_selector in Capybara 2 is click_link/button; fill_in; check/uncheck, select, choose. Cucumber/Capybara steps to check checkbox value by corresponding label text - gist:2953103

capybara cheat sheet · GitHub, Meta Capybara Version: 3.1.1 Driver Information (and browser if for the checkbox and click on that, or tell check / uncheck that it's allowed to  The underlying Capybara testing code for automating the scenario. The visit (line 2) method navigates to the given URL and page.should have_content (line 3) will check if the text is visible on the page. This method waits for the element to be visible for the default wait time which is 2 seconds in Capybara.

Capybara can't find a checkbox that's on the page, and not disabled , to Capybara and checkboxes, the Capybara::Node::Actions#check instance method will allow you to find and check a checkbox by name, id, or label text. Note. The one-page guide to Capybara: usage, examples, links, snippets, and more.

ruby on rails How to check a checkbox in capybara?, Interacting with forms. attach_file 'Image', '/path/to/image.jpg' fill_in 'First Name', with: 'John' check 'A checkbox' uncheck 'A checkbox' choose 'A radio button' Using this step definition you can check if any form field (text field, checkbox, etc) or button is disabled: Then the "Name" field should be disabled And the "Save" button should be disabled But the "Locked" field should not be disabled Capybara This step part of Spreewald. Webrat Then /^"([^

Comments
  • Your inputs shouldn't have the same IDs - they should have the same names, but different IDs.
  • Glad to hear it! Perhaps you'd mark this answer as 'accepted' and maybe upvote it... :)
  • @Jon M I have some odd id with empty brackets so for the check example... find(:css, "#cityID[value='62']").set(true) will work but find(:css, "#cityID[][value='62']").set(true) will not be found and fail. How do I run the same function with an empty bracket id?
  • @TangibleDream just to clarify - are you saying the checkbox has an ID of '[]'?
  • I couldn't find a way to make this work with the CSS selector at all! There must be some way to escape the square bracket but I couldn't find it. I had to resort to an XPath finder: find(:xpath, ".//input[@id='Extrapainful[]'][@value='12345']").set(true)
  • Can I mark this answer down... I know it works, but its counter intuitive to mark up an answer that isn't part of the simple api available: check('name, id or text here') (see answer below)
  • It's also better to not create multiple elements with the same id because it's not valid HTML. That should not be an issue if using rails form helpers properly.
  • I would like to add that the checkbox/uncheck only accepts the following values: id, name or related label element. Here you can read more about it.
  • solid answer, seems cleaner than the accepted answer using css selectors (even if that's what the check method does underneath the covers)
  • Yep, this is the best answer. It is cleaner and it closely mimics the user action. It does not pollute the form with extra id's and it makes the tests easy to read.
  • I agree this is cleaner. Interestingly though, the accepted answer isn't much different from how the check method is implemented in Capybara. def check(locator, options={}) find(:checkbox, locator, options).set(true) end
  • Thanks, and this is the same answer according to the docs: rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/… "Find a check box and mark it as checked. The check box can be found via name, id or label text."
  • This response should be at the top, not the 8 years old syntax.
  • actually for the scenario described by the author, the answer from @samuel is the right own
  • (1) I do not think click on its own is a valid capybara command (or at least if it is, it doesn't seem to be on the docs), and (2) if it was, it would probably toggle the checkbox, not make sure it was on or off