Rails

Testing Chosen Selects With Capybara And Rails

Posted by Weston Ganger on December 08, 2016

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.


# 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.


select_first_from_chosen('#site_id')

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


Related External Links:


Posted in Rails and Tagged with capybara chosen rails testing 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts