Capybara :: ElementNotFound:无法找到xpath“/ html”
我正在http://ruby.railstutorial.org/chapters/static-pages上关注Ruby on Rails教程并遇到以下错误
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)'
我的Gem文件如下
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
如何摆脱这个错误并通过rspec? 源文件
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
也许问题是webrat gem + capybara gem,尝试从你的gemfile中删除webrat。
检查这个问题
您可能有一个错误,阻止页面正确呈现。
使用一些调试工具寻求帮助:
- 在您的请求规范中,使用
puts page.html
在您的规范中打印页面内容。 - 跟踪日志文件(log / test.log)
你能展示你的./spec/requests/static_pages_spec.rb来源吗?
我猜这个问题最有可能出现在这条线上:
visit '/static_pages/home'
运行’rake routes’找出路径名并使用其中一个。 例如,如果您有一个名为“home”的路线,请使用:
访问home_path
如果您想要的路径不存在,请将其添加到config / routes.rb。
今天我有同样的错误但是情况不同。 我在spec_helper.rb
( rails_helper.rb
)中包含了spec_helper.rb
include Capybara::DSL
。
然后,当我运行规范时, including Capybara::DSL in the global scope is not recommended!
显示including Capybara::DSL in the global scope is not recommended!
的警告including Capybara::DSL in the global scope is not recommended!
。 但是在一些测试中我得到了Capybara::ElementNotFound: Unable to find xpath "/html"
。
我必须包含在RSpec.configure
块中。
RSpec.configure do |config| config.include Capybara::DSL end