From a88428a1a0aceb6a1191443cecfe1aa7cffc62f2 Mon Sep 17 00:00:00 2001 From: Gregory Myers Date: Thu, 5 Nov 2026 11:20:53 +0000 Subject: [PATCH] Allow url_blocklist and url_allowlist in driver options Browser already aliases url_blocklist/url_allowlist, but the options hash only accepted the legacy keys. Map the new keys to url_blacklist and url_whitelist once in Driver#initialize, so Browser#initialize and Driver#reset! read the same options and the lists survive a reset. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + README.md | 4 +-- lib/capybara/cuprite/driver.rb | 2 ++ spec/features/driver_spec.rb | 52 ++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d025e21..5d0fff55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## [Unreleased](https://github.com/rubycdp/ferrum/compare/v0.18...main) ## ### Added +- `:url_blocklist` and `:url_allowlist` driver options as aliases for `:url_blacklist` and `:url_whitelist` [#330] ### Changed diff --git a/README.md b/README.md index 5b906a41..ea273984 100644 --- a/README.md +++ b/README.md @@ -82,8 +82,8 @@ end that triggered the dialog (a click, `visit`, `evaluate_script`, ...), since a JS dialog blocks the page until answered, so that action's own command is what was waiting. A dialog fired with nothing in flight (e.g. a bare JS timer) only surfaces on whatever command runs next. - * `:url_blacklist` (Array) - array of regexes to match against requested URLs - * `:url_whitelist` (Array) - array of regexes to match against requested URLs + * `:url_blocklist` (Array) - array of regexes to match against requested URLs, alias `:url_blacklist` + * `:url_allowlist` (Array) - array of regexes to match against requested URLs, alias `:url_whitelist` ## Debugging diff --git a/lib/capybara/cuprite/driver.rb b/lib/capybara/cuprite/driver.rb index 22143832..2f12470b 100644 --- a/lib/capybara/cuprite/driver.rb +++ b/lib/capybara/cuprite/driver.rb @@ -36,6 +36,8 @@ def initialize(app, options = {}) @options[:save_path] ||= File.expand_path(Capybara.save_path) if Capybara.save_path @options[:pending_connection_errors] = true unless @options.key?(:pending_connection_errors) + @options[:url_blacklist] = @options.delete(:url_blocklist) if @options.key?(:url_blocklist) + @options[:url_whitelist] = @options.delete(:url_allowlist) if @options.key?(:url_allowlist) # It's set for debug() to make devtools tab open correctly. @options[:browser_options] ||= {} diff --git a/spec/features/driver_spec.rb b/spec/features/driver_spec.rb index aad29295..e15b03c7 100644 --- a/spec/features/driver_spec.rb +++ b/spec/features/driver_spec.rb @@ -1170,6 +1170,28 @@ def create_screenshot(file, *args) expect(session.html).not_to include("We shouldn't see this.") end end + + it "can be configured in the driver with url_blocklist and survive reset" do + Capybara.register_driver :cuprite_blocklist do |app| + Capybara::Cuprite::Driver.new(app, @driver.options.except(:url_blacklist).merge(url_blocklist: /unwanted/)) + end + + session = Capybara::Session.new(:cuprite_blocklist, @session.app) + + session.visit "/cuprite/url_blacklist" + expect(session).to have_content("We are loading some unwanted action here") + session.within_frame "framename" do + expect(session.html).not_to include("We shouldn't see this.") + end + + session.reset! + + session.visit "/cuprite/url_blacklist" + expect(session).to have_content("We are loading some unwanted action here") + session.within_frame "framename" do + expect(session.html).not_to include("We shouldn't see this.") + end + end end context "whitelisting urls for resource requests" do @@ -1252,6 +1274,36 @@ def create_screenshot(file, *args) expect(session).not_to have_content("We shouldn't see this.") end end + + it "can be configured in the driver with url_allowlist and survive reset" do + Capybara.register_driver :cuprite_allowlist do |app| + Capybara::Cuprite::Driver.new(app, + @driver.options.except(:url_whitelist) + .merge(url_allowlist: [/url_whitelist/, %r{/cuprite/wanted}])) + end + + session = Capybara::Session.new(:cuprite_allowlist, @session.app) + + session.visit "/cuprite/url_whitelist" + expect(session).to have_content("We are loading some wanted action here") + session.within_frame "framename" do + expect(session).to have_content("We should see this.") + end + session.within_frame "unwantedframe" do + expect(session).not_to have_content("We shouldn't see this.") + end + + session.reset! + + session.visit "/cuprite/url_whitelist" + expect(session).to have_content("We are loading some wanted action here") + session.within_frame "framename" do + expect(session).to have_content("We should see this.") + end + session.within_frame "unwantedframe" do + expect(session).not_to have_content("We shouldn't see this.") + end + end end context "has ability to send keys" do