3

我遇到了 Capybara Webkit 的问题,当我尝试运行功能规范时,它会引发以下异常。

Failure/Error: visit root_path
Capybara::Webkit::InvalidResponseError:
  Unable to load URL: http://test.unbound.com:4040/ because of error loading http://test.unbound.com:4040/: Unknown error

失败的规范如下所示:

it "should render a page", js: true do
  visit root_path
  current_path.should == root_path
end

这是我使用:webkit_debug驱动程序时的输出:

Run options: include {:line_numbers=>[72]}
Finished "EnableLogging" with response "Success()"
Wrote response true ""
Received "Visit"
Started "Visit"
Load started
"Visit" started page load
Started request to "http://test.unbound.com:4040/"
Finished "Visit" with response "Success()"
Received 0 from "http://test.unbound.com:4040/"
Page finished with false
Load finished
Page load from command finished
Wrote response false "{"class":"InvalidResponseError","message":"Unable to load URL: http://test.unbound.com:4040/ because of error loading http://test.unbound.com:4040/: Unknown error"}"
Received "Reset"
Started "Reset"
undefined|0|SECURITY_ERR: DOM Exception 18: An attempt was made to break through the security policy of the user agent.
Finished "Reset" with response "Success()"
Wrote response true ""

非 JS 等效项将通过,不会引发异常。此版本使用常规 Capybara 而不是 Capybara webkit。

it "should render a page" do
  visit root_path
  current_path.should == root_path
end

我在每次测试之前强制主机可能是相关的,如下所示:

# default_host_and_port will equal: "test.unbound.com:4040"
default_url_options[:host] = Rails.configuration.default_host_and_port
Capybara.app_host = "http://#{Rails.configuration.default_host_and_port}"

如果我不这样做,那么我会遇到使用常规 Capybara 运行功能规范的问题。

我尝试过的一些事情:

  • 在测试环境中运行服务器并root_path在浏览器中访问(如this SO answer中所建议)。它加载良好。将出现 JS 控制台错误,因为mixpanel找不到,但我认为这不太可能导致问题。
  • 用于Rack::ShowExceptions尝试获取更详细的异常。仍然得到相同的异常和回溯。
  • -b使用标志运行 rspec 。没有不同。
  • 添加test.unbound.com到我的/etc/hosts. 没有不同。
  • 告诉 Capybara 忽略这样的 SSL 错误。

一条线索:

  • 当我跟踪测试日志时,我从来没有看到通过的请求GET /
4

2 回答 2

1

唯一的解决方法是删除主机设置。那是错误的根源,我认为您没有理由需要它。

如果您的测试在 CI 服务器或同行的计算机上运行怎么办?他们对本地测试数据库进行了更改,但需要访问远程服务器以查看更改?

于 2013-11-06T16:53:02.690 回答
0

你有没有试过在你的全局 rspec before(:each) 块中加入这样的东西?那为我修好了。

RSpec.configure do |config|
  config.before(:each) do
    if Capybara.javascript_driver == :webkit
      # Allow loading of all external URLs
      page.driver.allow_url("*")
      # Ignore SSL errors
      page.driver.browser.ignore_ssl_errors
    end
  end
end
于 2015-02-16T22:39:33.330 回答