ruby-on-railsformsdrop-down-menucapybara

Make capybara click a dropdown option


I'm trying to use Capybara to select a drop down option, but it won't let me. The form is as follows:

  <%= simple_form_for @building do |f| %>
    <%= f.input :address  %>
    <%= f.input :city  %>
    <%= f.input :state, collection: ['AL', 'AK', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL',
      'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS',
      'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'] %>
    <%= f.input :zip  %>
    <%= f.input :description  %>
    <%= f.button :submit, "Add Building"  %>
  <% end %>

allowing you to select a from a dropdown of all the states in the USA. The code I am using in Capybara is as follows:

    fill_in 'Address', with: valid_attrs.address
    fill_in 'City', with: valid_attrs.city
    click_on 'building_state'
    click_on 'CT'
    fill_in 'Zip', with: valid_attrs.zip

but the "click on 'building_state' through to click_on 'CT' doesn't work.


Solution

  • To select an option from dropdown, you need to use select method and not click_on:

    For example:

    select 'CT', from: "building_state"