Last Updated: July 13, 2016
·
2.869K
· tmartin314

Capybara select chosen field for rspec testing

  1. Create a new file in spec/support/
# spec/support/select_from_chosen.rb
module SelectFromChosen
  # select_from_chosen('Option', from: 'id_of_field')
  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
end
  1. Include the module in the Rspec configuration for features:
# spec/rails_helper.rb
RSpec.configure do |config|
  config.include SelectFromChosen, type: :feature
end
  1. Usage in scenario:
select_from_chosen('Option', from: 'id_of_field')