Hassen
Hassen

Reputation: 7644

Rails: Capybara doesn't find a div element

I'm using Capybara with RSpec to check my Rails project.

I'm testing errors when form fields are not correctly filled. Here is the form (using haml):

= form_tag '/objects', :class => 'objects-form' do
    %ul
        %li= select_tag 'object', options_for_select([['Select an object', ''], ['Car', 'CAR'], ['Keys', 'KEYS'], ['Ambrella', 'AMBRELLA']]), :id => 'select-object'
        %li= text_field_tag :quantity, nil
        %li= submit_tag 'Buy', :id => 'object-submit'

When an error occurs (in this case, not choosing an object but only a quantity), a flash message is displayed (with a .flash-error class name):

- flash.each do |type, msg|
    = content_tag :div, msg, :class => "flash-#{type}", :id => 'flash-msg'

So, here is my Capybara test:

it 'Should return error when no object selected' do
    within 'form.objects-form' do
        fill_in 'quantity', :with => '1'
        click_button 'object-submit'
        current_path.should == objects_path
        save_and_open_page
        page.should have_css 'div.flash-error'
    end
end

But I get the following Capybara error:

Failure/Error: page.should have_css 'div.flash-error'
   expected #has_css?("div.flash-error") to return true, got false

The opened page (using save_and_open_page) shows the error message with the appropriate div:

div#flash-msg.flash-error

I'm using the latest version of Capybara (since many similar problems on Stackoverflow are relative to old versions of Capybara) using gem 'capybara', :git => 'https://github.com/jnicklas/capybara.git'

Any idea? Thank in advance.

EDIT:

Corrected typing mistake on Capybara code, as detected by Alex.

Upvotes: 2

Views: 874

Answers (1)

Alex Peachey
Alex Peachey

Reputation: 4676

The problem is your selector you are using in capybara is div#flash-error but your div is div#flash-msg.flash-error or more simply div.flash-error.

Upvotes: 1

Related Questions