Testing Chosen Selects With Capybara And Rails

Posted By Weston Ganger

I was having trouble using my chosen selects when starting out using Capybara. Turns out you need to add a couple helper methods to your test helper file.

<pre>
<code class="language-markup">

test_helper.rb

def select_first_from_chosen(from) # from is any selector find_field allows
field = find_field(from, visible: false)
find("##{field[:id]}_chosen").click
first("##{field[:id]}_chosen ul.chosen-results li").click
end

def select_from_chosen(item_text, options)
field = find_field(options[:from], visible: false)
find("##{field[:id]}_chosen").click
find("##{field[:id]}_chosen ul.chosen-results li", text: item_text).click
end


You can then use these methods as alternative to select method of Capybara.
<pre>
<code class="language-markup">
select_first_from_chosen('#site_id')

select_from_chosen('Vancouver', from: '#site_id')

Related External Links:

Article Topic:Software Development - Ruby / Rails

Date:December 08, 2016