Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara choose("radio button") not working

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

like image 425
james Avatar asked Mar 22 '23 00:03

james


1 Answers

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.

like image 158
Jon Avatar answered Apr 01 '23 13:04

Jon