samedi 3 juin 2017

Rspec testing for radio buttons

I have been having problems setting up a test which involves selecting a radio button using Capybara in Rspec. So, I've tried to isolate the problem out as much as possible.

I created a very simple page of 3 radio buttons, that I would like to test by selecting the first button.

However, I keep getting the error

Failure/Error: choose('items_1', visible: false)

     Capybara::ElementNotFound:
       Unable to find radio button "items_1"

The HTML for the page I'm testing is very simple


      <body>  
        <h1>Choose</h1>  
          <input type="radio" name="items" id="items_1" value="1" /> first item<br/>  
          <input type="radio" name="items" id="items_2" value="2" /> second item<br/>  
          <input type="radio" name="items" id="items_3" value="3" /> third item<br/>
      </body>

My spec is


    require 'rails_helper'

    describe 'Radio button testing' do

      it 'selects the first radio button', :feature do
        visit '/choose'
        choose('items_1', visible: false)
        expect(page).to have_content('Choose')
      end

    end

I have tried using choose since the documentation says it's for radio buttons (both with and without the css id marker)


    choose('items_1', visible: false)
    choose('#items_1', visible: false)

but neither work.

Maybe something very simple is wrong?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire