4

Using Capybara I cannot for the life of me select a checkbox on my form.

In my request spec I've tried:

check("First Name")
page.check("First Name")
page.check("pickem_option_ids_10")
find(:css, "#pickem_option_ids_11[value='11']").set(true)
find(:css, "#pickem_option_ids_11").set(true)

Snippet from my form:

<div class="control-group check_boxes optional">
   <label class="check_boxes optional control-label">Options:</label>
   <div class="controls">
    <label class="checkbox">
     <input class="check_boxes optional" id="pickem_option_ids_10" name="pickem[option_ids][]" type="checkbox" value="10" />First Name
    </label>
    <label class="checkbox">
     <input class="check_boxes optional" id="pickem_option_ids_11" name="pickem[option_ids][]" type="checkbox" value="11" />Middle Name
    </label>
   </div>
</div>

I got some of the find() ideas from this SO thread.

I've had some success in other specs where I have a single checkbox with a label of Active and I just say check("Active").

Community
  • 1
  • 1
Ryan Arneson
  • 1,323
  • 3
  • 14
  • 25
  • This solution worked with me http://stackoverflow.com/questions/8297624/how-to-check-a-checkbox-in-capybara –  Mar 01 '13 at 14:56
  • Or more precisely this: http://stackoverflow.com/a/14813623/1612469 – Aleks May 25 '15 at 12:48

2 Answers2

10

Had the same problem today, I looked around and this seemed to work:

find(:xpath, "//input[@value='10']").set(true)

of course you can replace '10' with anything you want - just check your HTML and use your value.

Hope that helps.

Lukasz Muzyka
  • 2,783
  • 1
  • 30
  • 41
2

Capybara can't find checkbox "First Name" because your html is wrong. Your html should look like

<label class="checkbox" for="pickem_option_ids_10">First Name</label>
 <input class="check_boxes optional" id="pickem_option_ids_10" name="pickem[option_ids][]" type="checkbox" value="10" />

In your view code

= label_tag "pickem_option_ids_10", "First Name"
= check_box_tag "pickem_option_ids_10", 10

Then check("First Name") should work.

Otherwise you can find("#pickem_option_ids_10").check

Art Shayderov
  • 5,002
  • 1
  • 26
  • 33
  • Good point. I'm using the association helper from simple_form, which is where that markup is coming from. Unfortunately I don't know how to achieve your suggestion with it. However now that you mention it, the generated markup is kind of strange. – Ryan Arneson Jul 19 '12 at 01:51
  • Sorry, never used simple_form. Does `find("#pickem_option_ids_10").check` work? – Art Shayderov Jul 19 '12 at 05:57
  • 1
    Failure/Error: find("#pickem_option_ids_10").check Capybara::ElementNotFound: Unable to find css "#pickem_option_ids_10" – Ryan Arneson Jul 19 '12 at 15:02
  • Well, you got some other page probably. Can be Rails error page. – Art Shayderov Jul 19 '12 at 15:30
  • I put in a save_and_open_page and when it opens the checkboxes aren't even visible. That would certainly be an issue. – Ryan Arneson Jul 23 '12 at 18:24
  • Alright my newbness caught up to me. My seed data was never loaded into the test environment, therefore Options didn't exist to create checkboxes from. – Ryan Arneson Jul 23 '12 at 20:25