Capybara choose(“radio button”) not working

拈花ヽ惹草 提交于 2019-12-10 06:06:29

问题


A snapshot of my view:

<%= form_for @request do |f| %>
  <div class="form-group">
    <%= f.radio_button(:item, "Snow/waterproof shell (upper)") %>
    <%= f.label(:item, "Snow/waterproof shell (upper)") %>
    </br>
    <%= f.radio_button(:item, "Headlamp") %>
    <%= f.label(:item, "Headlamp") %>
  </div>

Yet on my Rspec integration test file (spec/requests/requests_spec.rb), when I write (note the choose radio button is a part of the form where the user requests an item from a list, and the test is for the resulting page after submission, which should indicate the item that the user requested). I'm using gem 'rspec-rails', '2.13.1'

describe "Requests" do
  subject { page }

  describe "new request" do
    before { visit root_path }

    describe "with valid information" do
      before do 
        choose("Snow/waterproof shell (upper)")
        click_button submit
      end

      it { should have_content("Snow/waterproof shell (upper)")
    end
  end
end

I always get the error:

←[31mFailure/Error:←[0m ←[31mchoose("Snow/waterproof shell (upper)")←[0m
←[31mCapybara::ElementNotFound←[0m:
←[31mUnable to find radio button "Snow/waterproof shell (upper)"←[0m
←[36m # ./spec/requests/requests_spec.rb:24:in `block (4 levels) in <top (required)>'←[0m

Same if I try with choose("Headlamp") or any other option. Any thoughts smart people? This seemed like something that would be so easy...


回答1:


I've had this issue a number of times. If you choose form elements based on their ID in the dom it's far more reliable:

before do 
  choose 'request_item_headlamp'
  click_button submit
end

I can't tell without looking what ID rails would come up with for the other radio button. Just right click it in chrome, inspect element, and cut and paste the element ID into your test.




回答2:


I suspect sometimes when choose doesn't reliably check a radio button, it may be because an animation is in progress.

If you suspect this is causing your choose calls to be unreliable, try disabling animations, say by setting their execution time to 0 seconds in your test environment. Alternatively build in a wait period in your test for the animation to finish.



来源:https://stackoverflow.com/questions/21085057/capybara-chooseradio-button-not-working

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