Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click radio button with capybara in ruby on rails app

I'm trying to select a radio button with capybara and it can't find the radio button. Here is my rspec test, view and error. Please note that I am using factories for user, skills, etc.

Rspec test

  scenario "user chooses a couple skills and moves on to bio" do
    user = create(:user)
    skill = create(:skill)
    skill_two = create(:skill)
    skill_three = create(:skill)
    sign_in(user)
    visit onboard_skills_path

    choose(skill.name)    
  end

View

  <%= form_for(:onboard_skill, url: onboard_skills_path) do |f| %>
    <ul>
    <% @skills.each do |skill| %>
      <li>
        <%= check_box_tag("skill_ids[]", skill.id, current_user.onboard_skill_ids.include?(skill.id)) %>
        <%= f.label(skill.name) %>
      </li>
    <% end %>
    </ul>
    <%= f.submit "Next >", class: "submit_skills" %>
  <% end %>

Error I am getting is:

Unable to find radio button "Development 1"
like image 361
LMo Avatar asked Dec 11 '14 18:12

LMo


1 Answers

You are using choose to test for a radio button but in your provided view code you have a check_box_tag. Try either changing check_box_tag to radio_button_tag or if you really want a check box, use check instead of choose.

Note that you can also select a radio button or check box by searching for the id using find. This helps when capybara does not find it by the label name. Try:

find(:css, "#skill_ids_[value='#{skill.id}']").set(true)
like image 76
nickwtf Avatar answered Nov 05 '22 02:11

nickwtf