【问题标题】:Capybara::ElementNotFound: Unable to find xpath "/html"Capybara::ElementNotFound: 找不到 xpath "/html"
【发布时间】:2012-05-15 17:50:12
【问题描述】:

我在 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

【问题讨论】:

标签: ruby-on-rails ruby rspec capybara gemfile


【解决方案1】:

也许问题出在 webrat gem + capybara gem,尝试从您的 gemfile 中删除 webrat。

check this issue

【讨论】:

    【解决方案2】:

    您可能遇到错误,导致页面无法正确呈现。

    使用一些调试工具来寻求帮助:

    • 在您的请求规范中,使用 puts page.html 在您的规范期间打印页面内容。
    • 跟踪日志文件 (log/test.log)

    您能否展示您的 ./spec/requests/static_pages_spec.rb 源代码?

    【讨论】:

      【解决方案3】:

      我猜这个问题很可能出在这一行:

      visit '/static_pages/home'

      运行“rake routes”以找出路径名并使用其中之一。例如,如果您有一条名为“家”的路线,请使用:

      访问 home_path

      如果您想要的路径不存在,请将其添加到 config/routes.rb。

      【讨论】:

        【解决方案4】:

        今天我遇到了同样的错误,但情况不同。 我将include Capybara::DSL 包含在spec_helper.rb (rails_helper.rb) 中。

        然后,当我运行规范时,会默默地显示一个警告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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-06-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-11
          • 2018-05-07
          • 2012-12-01
          • 1970-01-01
          相关资源
          最近更新 更多