diff --git a/CHANGELOG.md b/CHANGELOG.md index a34294c..8b5d291 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## [Unreleased](https://github.com/rubycdp/cuprite/compare/v0.20...main) ## ### Added +- `:url_blocklist` and `:url_allowlist` driver options as aliases for `:url_blacklist` and `:url_whitelist` [#330] ### Changed - `Driver#current_url` and `Driver#title` read Ferrum's `url` and `title` of the window, not `current_url` and diff --git a/README.md b/README.md index 50e2668..f8097ab 100644 --- a/README.md +++ b/README.md @@ -92,8 +92,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` * `:emulate` (Symbol) - device from Chrome DevTools' list to emulate, e.g. `:iphone_16`, `:pixel_9`, `:ipad_mini` * `:mobile` (Boolean) - emulate a mobile device: viewport meta tag and touch events * `:scale_factor` (Numeric) - device scale factor to emulate diff --git a/lib/capybara/cuprite/driver.rb b/lib/capybara/cuprite/driver.rb index 279d151..ab3777b 100644 --- a/lib/capybara/cuprite/driver.rb +++ b/lib/capybara/cuprite/driver.rb @@ -47,6 +47,9 @@ def initialize(app, options = {}) @screen_size ||= DEFAULT_MAXIMIZE_SCREEN_SIZE @options[:save_path] ||= File.expand_path(Capybara.save_path) if Capybara.save_path + @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] ||= {} unless @options[:browser_options][:"remote-allow-origins"] diff --git a/spec/browser/extensions_spec.rb b/spec/browser/extensions_spec.rb index 0dfe877..dd6b24b 100644 --- a/spec/browser/extensions_spec.rb +++ b/spec/browser/extensions_spec.rb @@ -60,6 +60,28 @@ expect(session.html).not_to include("We shouldn't see this.") end end + + it "keeps blocking urls from the url_blocklist driver option after reset!" do + Capybara.register_driver :cuprite_blocklist do |app| + Capybara::Cuprite::Driver.new(app, @driver.options.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 describe "#url_whitelist=" do @@ -142,6 +164,35 @@ expect(session).not_to have_content("We shouldn't see this.") end end + + it "keeps allowing urls from the url_allowlist driver option after reset!" do + Capybara.register_driver :cuprite_allowlist do |app| + Capybara::Cuprite::Driver.new(app, + @driver.options.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 describe "#raise_on_unhandled_modal=" do