Ruby on rails Capybara::ElementNotFound:找不到xpath“/html";
我在学习RubyonRails教程时遇到了以下错误Ruby on rails Capybara::ElementNotFound:找不到xpath“/html";,ruby-on-rails,ruby,rspec,capybara,gemfile,Ruby On Rails,Ruby,Rspec,Capybara,Gemfile,我在学习RubyonRails教程时遇到了以下错误 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_p
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
您可能有一个错误,使页面无法正确呈现 使用一些调试工具获取帮助:
- 在请求规范中,使用
在规范期间打印页面内容put page.html
- 跟踪日志文件(log/test.log)
能否显示您的./spec/requests/static\u pages\u spec.rb源代码?可能是webrat gem+capybara gem的问题,请尝试从gem文件中删除webrat
我想问题很可能出在这一行:
访问“/static\u页面/主页”
运行“rake routes”找出路径名并使用其中一个。例如,如果您有一条名为“home”的路线,请使用:
参观家乐径
如果您想要的路径不存在,请将其添加到config/routes.rb。今天我遇到了相同的错误,但大小写不同。 我在
spec\u helper.rb
中包含了include Capybara::DSL
(rails\u helper.rb
)
然后,当我运行spec时,会出现一条警告,提示不要将Capybara::DSL包含在全局范围内代码>。但在一些测试中,我得到了Capybara::ElementNotFound:找不到xpath”/html“
我必须包含在RSpec.configure
块中
RSpec.configure do |config|
config.include Capybara::DSL
end
可能的副本请检查此规范是否在规范/控制器或规范/请求中