Capybara::ElementNotFound: Unable to find xpath “/html”

谁说胖子不能爱 提交于 2020-01-14 10:13:08

问题


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

回答1:


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

check this issue




回答2:


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 ?




回答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.




回答4:


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


来源:https://stackoverflow.com/questions/10606150/capybaraelementnotfound-unable-to-find-xpath-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!