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

醉酒当歌 提交于 2019-12-23 09:41:10

问题


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"

回答1:


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)


来源:https://stackoverflow.com/questions/27430074/how-to-click-radio-button-with-capybara-in-ruby-on-rails-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!