7

I'm following the Ruby on Rails tutorial at http://ruby.railstutorial.org/chapters/static-pages and came across the following error

 StaticPages Home page should have the content 'Sample App'
 Failure/Error: page.should have_content('Sample App')
 Capybara::ElementNotFound:
   Unable to find xpath "/html"
 # (eval):2:in `text'
 # ./spec/requests/static_pages_spec.rb:7:in `(root)'

My Gem file is as follows

source 'http://rubygems.org'

gem 'rails', '3.0.10'

gem 'jruby-openssl'

gem 'activerecord-jdbcsqlite3-adapter'
group :development, :test do
   gem 'webrat'
   gem 'rspec-rails', ">= 2.10.0"
   gem 'capybara', ">= 1.1.2"
end

How do I get rid of this error and pass the rspec? The source file

require 'spec_helper'

describe "StaticPages" do

  describe "Home page" do

    it "should have the content 'Sample App'" do
      visit '/static_pages/home'
      # puts page.html
      page.should have_content('Sample App')
    end
  end
end
absessive
  • 1,121
  • 5
  • 14
  • 36
  • 1
    Possible duplicate of http://stackoverflow.com/questions/11382415/rails-3-2-capybara-capybaraelementnotfoundunable-to-find-xpath-html Please check if this spec is in spec/controllers or spec/requests – benz001 Mar 08 '13 at 21:08

5 Answers5

4

You may have an error that prevents the page from being rendered properly.

Use some debugging facilities for help :

  • Inside your request spec, use puts page.html to print the page content during your spec.
  • track the log files (log/test.log)

Could you show your ./spec/requests/static_pages_spec.rb source ?

demental
  • 1,444
  • 13
  • 25
4

Maybe the problem is with webrat gem + capybara gem, try and remove webrat from your gemfile.

check this issue

Coelhone
  • 310
  • 1
  • 11
3

I'd guess that the problem is most likely with this line:

visit '/static_pages/home'

Run 'rake routes' to find out the path names and use one of them. For example, if you have a route named 'home', use:

visit home_path

If the path you want doesn't exist, add it to config/routes.rb.

Brent
  • 31
  • 1
0

Today I had same error but with different case. I included include Capybara::DSL in spec_helper.rb (rails_helper.rb).

Then, when I run spec, silently a warning shows up including Capybara::DSL in the global scope is not recommended!. but in some test I got Capybara::ElementNotFound: Unable to find xpath "/html".

I had to include in RSpec.configure block.

RSpec.configure do |config|
  config.include Capybara::DSL
end
banyan
  • 3,837
  • 2
  • 31
  • 25
0

Adding my solution here, since I ran across this error, and the original question is maybe ambiguous about the actual cause of the Capybara error.

While completing the def create method in my controller, was testing for the submit button to re-direct back to the ("contextually relevant" controller) index page, though had not explicitly redirect_to the index within the create method.

Added redirect_to "/<your_index_page_path_here>" and got the test to pass/resolved the test failure in question.

J.R. Bob Dobbs
  • 237
  • 3
  • 10