Selecting multiple checkboxes with Capybara, weird behaviour - ruby-on-rails

I am making an acceptance test on Ruby on Rails 3.0.11 with Capybara 1.1.2, and trying to select two checkboxes in a form.
Here is the view code for the checkboxes (using haml 3.1.3 and simple_form 2.0.4):
= f.input :countries,
:collection => MusicTeaser.teaser_countries,
:as => :check_boxes
The collection looks like this:
[["AT", "at"], ["CH", "ch"], ["DE", "de"], ["ZA", "za"]]
And the HTML generated:
<div class="input check_boxes optional">
<label class="check_boxes optional control-label">Countries</label>
<label class="checkbox">
<input class="check_boxes optional" id="music_teaser_countries_at" name="music_teaser[countries][]" type="checkbox" value="at">AT
</label>
<label class="checkbox">
<input class="check_boxes optional" id="music_teaser_countries_ch" name="music_teaser[countries][]" type="checkbox" value="ch">CH
</label>
<label class="checkbox">
<input class="check_boxes optional" id="music_teaser_countries_de" name="music_teaser[countries][]" type="checkbox" value="de" checked="">DE
</label>
<label class="checkbox">
<input class="check_boxes optional" id="music_teaser_countries_za" name="music_teaser[countries][]" type="checkbox" value="za">ZA
</label>
<input name="music_teaser[countries][]" type="hidden" value="">
</div>
The code to select them looks like this (pretty straightforward):
check 'music_teaser_countries_de'
check 'music_teaser_countries_za'
I expected that, on submit, the countries param would look like this:
params[:music_teaser][:countries] = ["de", "za"]
But it appears as this:
params[:music_teaser][:countries] = ["deza"]
So it seems like Capybara is doing something wrong when selecting more than one checkbox. Ideas?

Related

Customize simple form radio buttons to match the theme markup

I’m using simple forms. The following line is what I have
=f.input :live_mode, as: :radio_buttons, label: false
which produces the following markup
<div class="form-group radio_buttons optional facility_live_mode"> .
<input type="hidden" name="facility[live_mode]" value="">
<label class="radio">
<label for="facility_live_mode_true">
<input class="radio_buttons optional" type="radio" value="true" name="facility[live_mode]" id="facility_live_mode_true">
Yes
</label>
</label>
<label class="radio">
<label for="facility_live_mode_false">
<input class="radio_buttons optional" readonly="readonly" type="radio" value="false" checked="checked" name="facility[live_mode]" id="facility_live_mode_false">
No
</label>
</label>
</div>
The wrapper label is the result of setting config.item_wrapper_tag in simple_form.rb to :label which matches the markup of the custom theme I’m using.
What I want to achieve is this
<input type="hidden" name="facility[live_mode]" value="">
<label class="custom-control custom-radio custom-control-inline">
<input class="custom-control-input" type="radio" value="true" name="facility[live_mode]" id="facility_live_mode_true">
<span class="custom-control-label">Yes</span>
</label>
<label class="custom-control custom-radio custom-control-inline">
<input class="custom-control-input" readonly="readonly" type="radio" value="false" checked="checked" name="facility[live_mode]" id="facility_live_mode_false">
<span class="custom-control-label">No</span>
</label>
but I have no idea how to go about doing this. Simple form config file provides a few options for customization but nothing that can produce the output that I need.
Is there something I can do to make this happen?

Rails: Simple Form checkbox and material design

I have been trying to figure out how to make simple form generate the correct code for a checkbox.
The code im trying to create is:
<div class="checkbox m-b-15">
<input value="0" type="hidden" name="group[open]">
<label>
<input class="boolean optional" type="checkbox" value="1" checked="checked" name="group[open]" id="group_open">
<i class="input-helper"></i>
Label text
</label>
</div>
So far im managed to generate
<div class="checkbox"><input value="0" type="hidden" name="group[open]">
<label class="boolean optional" for="group_open">
<input class="boolean optional" type="checkbox" value="1" checked="checked" name="group[open]" id="group_open">
Label text
</label>
</div>
With the following code
b.wrapper tag: :label do |ba|
ba.use :input
# <i class="input-helper"></i> tag should go here
ba.use :label_text
end
So how do I insert then i tag?
<i class="input-helper"></i>

How to delete the last element from ruby association input

How to delete the last element from ruby association input
this is my input form field, which select all sizes and lists
= simple_nested_form_for [:admin, #product], wrapper: :horizontal_small_form do |f|
= f.association :standard_sizes, as: :check_boxes
This is what iam getting when iam inspecting it.
How to remove last option (custom size) from this.
<label class="check_boxes optional control-label">Standard sizes</label><span class="checkbox"><label for="product_standard_size_ids_1">
<input class="check_boxes optional" type="checkbox" value="1" name="product[standard_size_ids][]" id="product_standard_size_ids_1">S</label></span><span class="checkbox"><label for="product_standard_size_ids_2">
<input class="check_boxes optional" type="checkbox" value="2" name="product[standard_size_ids][]" id="product_standard_size_ids_2">M</label></span><span class="checkbox"><label for="product_standard_size_ids_3">
<input class="check_boxes optional" type="checkbox" value="3" name="product[standard_size_ids][]" id="product_standard_size_ids_3">L</label></span><span class="checkbox"><label for="product_standard_size_ids_4">
<input class="check_boxes optional" type="checkbox" value="4" name="product[standard_size_ids][]" id="product_standard_size_ids_4">XL</label></span><span class="checkbox"><label for="product_standard_size_ids_5">
<input class="check_boxes optional" type="checkbox" value="10" name="product[standard_size_ids][]" id="product_standard_size_ids_10">Custom Size</label></span><input type="hidden" name="product[standard_size_ids][]" value=""></div>
You can select the collection you wish to show with:
#rails 4
f.association :standard_sizes, collection: StandardSize.where.not(id: 10)
#rails 3
f.association :standard_sizes, collection: StandardSize.where('standard_sizes.id != 10')
But you should probably wonder why you need the field Custom Size in your database...

Choose Bootstrap option with capybara

I'm having problems selecting an option with Capybara in a form made with Bootstrap and I don't know why.
This is how the form is actually written:
<div class="control-group radio_buttons optional application_read">
<label class="radio_buttons optional control-label">Read</label>
<div class="controls">
<span class="radio">
<label for="application_read_true">
<input class="radio_buttons optional" type="radio" value="true" name="application[read]" id="application_read_true">Yes
</label>
</span>
<span class="radio">
<label for="application_read_false">
<input class="radio_buttons optional" type="radio" value="false" checked="checked" name="application[read]" id="application_read_false">No
</label>
</span>
</div>
</div>
What I'm attempting to do is select the first Radio button with "Yes" label (so Read: Yes) this way:
find("#application_read_true").click
but it doesn't affect the form in any way.
How can I select the True option under the 'Read' label? Thanks in advance.
Try this:
choose("application_read_true")
But I believe that this should work too if you have only one radio button with "Yes" caption
choose("Yes")

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

Resources