absessive
absessive

Reputation: 1171

Capybara::ElementNotFound: Unable to find xpath "/html"

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

Upvotes: 7

Views: 16160

Answers (5)

J.R. Bob Dobbs
J.R. Bob Dobbs

Reputation: 295

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.

Upvotes: 0

banyan
banyan

Reputation: 4190

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

Upvotes: 0

Brent
Brent

Reputation: 31

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.

Upvotes: 3

Coelhone
Coelhone

Reputation: 310

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

check this issue

Upvotes: 4

demental
demental

Reputation: 1484

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 ?

Upvotes: 4

Related Questions