Capybara choose(“radio button”) not working

2019-05-09 00:50发布

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...

2条回答
干净又极端
2楼-- · 2019-05-09 01:31

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.

查看更多
Animai°情兽
3楼-- · 2019-05-09 01:45

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.

查看更多
登录 后发表回答