Reputation: 1171
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
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
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
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
Reputation: 310
Maybe the problem is with webrat gem + capybara gem, try and remove webrat from your gemfile.
Upvotes: 4
Reputation: 1484
You may have an error that prevents the page from being rendered properly.
Use some debugging facilities for help :
puts page.html
to print the page content during your spec.Could you show your ./spec/requests/static_pages_spec.rb source ?
Upvotes: 4