Capybara webkit允许未设置未知的URL

时间:2015-07-09 22:32:47

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

我在rails应用程序中使用capybara webkit进行一些我的rspec测试。我想要allow_unknown_urls并根据全局配置指南https://github.com/thoughtbot/capybara-webkit/在spec_helper.rb文件中设置它 - 这是我当前的spec_helper.rb文件:

# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
ENV["RAILS_ENV"] ||= "test"
ENV['SERVER_NAME'] = "user.myapp.com"

require File.expand_path("../../config/environment", __FILE__)
require "rspec/rails"


Capybara::Webkit.configure do |config|
  # Enable debug mode. Prints a log of everything the driver is doing.
  config.debug = false

  config.allow_unknown_urls
  # Allow pages to make requests to any URL without issuing a warning.

  # Allow a specifc domain without issuing a warning.
  config.allow_url("checkout.stripe.com")



  # Timeout if requests take longer than 5 seconds
  config.timeout = 10

  # Don't raise errors when SSL certificates can't be validated
  config.ignore_ssl_errors

end

Capybara.javascript_driver = :webkit

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.use_transactional_fixtures = false
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do |example|
    DatabaseCleaner.strategy= example.metadata[:js] ? :truncation : :transaction
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

  config.include SignInHelpers, type: :feature
  config.mock_with :rspec

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  # rspec-mocks config goes here. You can use an alternate test double
  # library (such as bogus or mocha) by changing the `mock_with` option here.
  config.mock_with :rspec do |mocks|
    # Prevents you from mocking or stubbing a method that does not exist on
    # a real object. This is generally recommended, and will default to
    # `true` in RSpec 4.
    mocks.verify_partial_doubles = true
  end
end

正如您在webkit配置块中看到的那样,我正在设置config.allow_unknown_urls并且还明确允许checkout.stripe.com

当我运行使用webkit的测试时,我收到以下警告:

WARNING: The next major version of capybara-webkit will require at least version 5.0 of Qt. You're using version 4.8.7.
Request to unknown URL: https://checkout.stripe.com/v3/data/languages/en.json
To block requests to unknown URLs:
  page.driver.block_unknown_urls
To allow just this URL:
  page.driver.allow_url("https://checkout.stripe.com/v3/data/languages/en.json")
To allow requests to URLs from this host:
  page.driver.allow_url("checkout.stripe.com")
  Buyer creates a new event

这让我感到困惑,因为我的webkit已配置为允许所有未知网址并明确checkout.stripe.com。我在这里配置错误了什么?

4 个答案:

答案 0 :(得分:0)

我遇到了类似的问题并通过删除它来解决它 js: true  从我的测试。

我认为js: true指定了一个capybara-webkits js驱动程序而不是默认的selenium,所以如果你可以使用默认值来解决这个问题。 (有人请编辑,如果这不正确)

那就是说,我一直很难复制它。

答案 1 :(得分:0)

你必须使用

config.allow_urls("checkout.stripe.com") 

使警告静音的最佳方法是阻止未知网址,如消息中所述:page.driver.block_unknown_urls。如果您的应用程序依赖外部资源,API等,您需要通过page.drive.allow_url允许这些网址。如果您不关心警告,可以通过page.driver.allow_unknown_hosts将其静音。

答案 2 :(得分:0)

只需在helperenv.rb

上使用此功能即可
    $webkit_server = Capybara::Webkit::Server.new 
    $webkit_connection = Capybara::Webkit::Connection.new(server: $webkit_server) 
    $webkit_browser = Capybara::Webkit::Browser.new($webkit_connection) 

    $webkit_browser.allow_unknown_urls

Capybara.register_driver :webkit do |app|
            Capybara::Webkit::Driver.new(app, :browser => $webkit_browser)
        end

我还在其他地方定义了这个:

Capybara.default_driver = :webkit

Capybara.javascript_driver = :webkit

答案 3 :(得分:0)

我现在就这么做了。似乎问题是我的工作进度功能底部有一些空的“占位符”场景。正是这些“空洞”的场景给我带来了它。

Scenario: Write paper
Scenario: Shred paper
Scenario: Realize shredding a mistake
Scenario: Have a good cry
Scenario: Write paper again

似乎Capybara :: Webkit仅在运行实际场景时调用config.allow_url部分。当存在空场景时,就像这里一样,即使没有运行,它也会抱怨URL