Checking flags with Capybara

Using Capybara, I can’t choose me a checkbox in my form for my whole life.

In my request specification I 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) 

A 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 have some find () ideas from this SO thread .

I had some success in other specifications where I have one checkbox with an Active label and I just say check("Active") .

+4
source share
2 answers

Today I had the same problem, I looked around and it seemed to work:

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

Of course, you can replace "10" with anything - just check your HTML and use your value.

Hope this helps.

+10
source

Capybara cannot find the "First Name" checkbox because your html is incorrect. 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

+2
source

All Articles