Capybara website allows you to set unknown urls
I am using capybara webkit for some of my rspec tests in a rails app. I want allow_unknown_urls and set this in the spec_helper.rb file according to the guide for global config here https://github.com/thoughtbot/capybara-webkit/ - this is my current spec_helper.rb file:
# 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
As you can see in the configure webkit block I am setting config.allow_unknown_urls and also explicitly allowing checkout.stripe.com
When I run my test which uses webkit I get the following warnings:
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
This confuses me as my web kit is configured to resolve all unknown urls and explicitly checkout.stripe.com. What have I configured incorrectly here?
source to share
I had a similar problem and resolved it by removing it
js: true
from my test.
I think js: true
the js driver is specifying capybara-webkits instead of the standard selenium, so if you can get around with the default that might fix this. (Someone please edit if this is not the case)
However, I have had many problems reproducing them consistently.
source to share
You must use
config.allow_urls("checkout.stripe.com")
The best way to disable the warning - block unknown URL-addresses, as described in the report: page.driver.block_unknown_urls
. If your application depends on external resources, APIs, etc., you need to resolve these urls through page.drive.allow_url
. If you don't care about alerts, you can turn them off via page.driver.allow_unknown_hosts
.
source to share
Just use this on one of your helper
orenv.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
Elsewhere I also defined this:
Capybara.default_driver = :webkit
Capybara.javascript_driver = :webkit
source to share
I just had it now. The problem seems to be that I had some empty "placeholder" scripts at the bottom of my work-in-progress. It was these "empty" scripts that were causing this for me.
Scenario: Write paper
Scenario: Shred paper
Scenario: Realize shredding a mistake
Scenario: Have a good cry
Scenario: Write paper again
It seems Capybara :: Webkit only calls parts of config.allow_url when the real script runs. When an empty script is present like here, it will complain about the URL even if it fails
source to share