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)
  first("##{field[:id]}_chosen ul.chosen-results li").click

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

You can then use these methods as alternative to select method of Capybara.


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