11
votes

In a Rails 3.2.14 app with Ruby 2, using rspec-rails 2.14.0 and capybara 2.1.0 the following feature spec is causing a failure:

require 'spec_helper'

feature 'View the homepage' do
  scenario 'user sees relevant page title' do
    visit root_path
    expect(page).to have_css('title', text: "Todo")
  end
end

The failure message is:

 1) View the homepage user sees relevant page title
 Failure/Error: expect(page).to have_css('title', text: "Todo")
 Capybara::ExpectationNotMet:
   expected to find css "title" with text "Todo" but there were no matches. Also
   found "", which matched the selector but not all filters.

The title element and correct text are on the rendered page

But when I change this line in the feature spec:

expect(page).to have_css('title', text: "Todo")

to this:

page.has_css?('title', text: "Todo")

then the test passes. [Edit - but see response below from @JustinKo that this test is not a good test as it will always pass]

How do I get the have_css(...) form to work? Is it a configuration issue?

Here's the relevant part of my Gemfile:

group :development, :test do
  gem 'rspec-rails' 
  gem 'capybara'
end

And my spec/spec_helper.rb is set up like this:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails'
require 'capybara/rspec'

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|

  # out of the box rspec config code ommitted

  config.include Capybara::DSL
end

Anyone know what I might be doing wrong?

1
please go ahead and read this post, especially the capybara part nofail.de/2013/10/debugging-rails-applications-in-developmentphoet
If you do just page.has_css?('title', text: "Todo"), the test will always pass. has_css? simply returns true or false, which is not sufficient to fail a test. If you output it, you will likely see that it is false. Are you sure the page has a title element with text?Justin Ko
@JustinKo Thanks for pointing out the newbie error. Yes, the title is showing up with the appropriate text - checked in the browser.Tim Jones
Are you sure it is the 'title' element not 'H2' or 'H3'?Backnol

1 Answers

17
votes

By default, Capybara only looks for "visible" elements. The head element (and its title element) are not really considered visible. This results in the title element being ignored in have_css.

You can force Capybara to also consider non-visible elements by the :visible => false option.

expect(page).to have_css('title', :text => 'Todo', :visible => false)

However, it would be easier to use the have_title method:

expect(page).to have_title('Todo')