Capybara Webkit引发InvalidResponseError“无法加载URL”

时间:2013-11-06 16:40:24

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

我遇到了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错误like this

一条线索:

  • 当我拖尾测试日志时,我从未看到GET /的请求通过。

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

你有没有尝试在(=)每个块之前在你的全局rspec中放置这样的东西?这为我解决了。

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

答案 2 :(得分:-1)

Traceback (most recent call last):
  File "/usr/share/virt-manager/virt-manager.py", line 399, in <module>
    main()
  File "/usr/share/virt-manager/virt-manager.py", line 256, in main
    from virtManager import cli
  File "/usr/share/virt-manager/virtManager/cli.py", line 29, in <module>
    import libvirt
ImportError: No module named libvirt