Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking checkboxes with Capybara

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").

like image 554
Ryan Arneson Avatar asked Dec 15 '22 21:12

Ryan Arneson


1 Answers

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.

like image 94
Lukasz Muzyka Avatar answered Jan 13 '23 11:01

Lukasz Muzyka