From c13f11829f3dca4a86349618a094839f67948b68 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Fri, 27 Nov 2026 14:23:49 +0300 Subject: [PATCH] feat(driver): keep the moved settings on driver.browser with a deprecation warning until 1.0 --- CHANGELOG.md | 12 ++++--- lib/capybara/cuprite/deprecation.rb | 20 +++++++++++ lib/capybara/cuprite/driver.rb | 2 ++ lib/capybara/cuprite/driver/deprecations.rb | 36 ++++++++++++++++++++ sig/capybara/cuprite/deprecation.rbs | 7 ++++ sig/capybara/cuprite/driver/deprecations.rbs | 11 ++++++ spec/driver/deprecations_spec.rb | 33 ++++++++++++++++++ 7 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 lib/capybara/cuprite/deprecation.rb create mode 100644 lib/capybara/cuprite/driver/deprecations.rb create mode 100644 sig/capybara/cuprite/deprecation.rbs create mode 100644 sig/capybara/cuprite/driver/deprecations.rbs create mode 100644 spec/driver/deprecations_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 82c4811..4f2c32c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,10 @@ ### Added ### Changed -- `driver.browser` is a plain `Ferrum::Browser`, whose `page` is the first window. `driver.current_page` is the window - Capybara switched to. Cuprite sets every page up through Ferrum's `on_page` [#399] -- `browser.go_to` no longer raises `PendingConnectionsError`, `visit` does, and `browser.resize` and +- Breaking: `driver.browser` is a plain `Ferrum::Browser`, whose `page` is the first window. `driver.current_page` is + the window Capybara switched to. Cuprite sets every page up through Ferrum's `on_page` [#399] +- Breaking: `browser.go_to` no longer raises `PendingConnectionsError`, `visit` does, and `browser.resize` and `browser.set_viewport` don't keep `:mobile` and `:scale_factor` [#399] -- `url_blocklist=`, `url_allowlist=`, their aliases and `raise_on_unhandled_modal=` moved from `driver.browser` to - `driver` [#399] - A browser that died is restarted by `reset!` before the next test, not by the command that found it dead, so the test it died in fails with `Ferrum::DeadBrowserError` [#399] - `UnhandledModalError` is raised from Capybara's calls (`visit`, finders, node actions, `evaluate_script`, @@ -16,6 +14,10 @@ - Cuprite reaches Ferrum through its public API only, Ferrum's `new_window:`, `close_target` and `on_page` replace the internals it patched or called [#397, #398, #399] +### Deprecated +- `url_blocklist=`, `url_allowlist=`, their aliases, their readers and `raise_on_unhandled_modal=` on `driver.browser`, + will be removed in 1.0. They moved to `driver` and warn on the browser [#399] + ### Fixed ### Removed diff --git a/lib/capybara/cuprite/deprecation.rb b/lib/capybara/cuprite/deprecation.rb new file mode 100644 index 0000000..db6c1e4 --- /dev/null +++ b/lib/capybara/cuprite/deprecation.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Capybara + module Cuprite + # The release that removes what's deprecated now. + REMOVE_DEPRECATED_IN = "1.0" + + # Warns that a method is deprecated, pointing at the code that called it. + # + # @api private + # @param method [String] the deprecated call, e.g. `"page.driver.browser.url_blocklist="` + # @param advice [String, nil] what to use instead + # @return [void] + def self.deprecation_warning(method, advice = nil) + message = "#{method} is deprecated and will be removed in #{REMOVE_DEPRECATED_IN}" + message += ", #{advice} instead" if advice + warn message, uplevel: 2 + end + end +end diff --git a/lib/capybara/cuprite/driver.rb b/lib/capybara/cuprite/driver.rb index aeac974..3d9389c 100644 --- a/lib/capybara/cuprite/driver.rb +++ b/lib/capybara/cuprite/driver.rb @@ -3,6 +3,7 @@ require "forwardable" require "rack" require "capybara/cuprite/finder" +require "capybara/cuprite/driver/deprecations" require "capybara/cuprite/driver/extensions" require "capybara/cuprite/driver/settings" require "capybara/cuprite/driver/window" @@ -447,6 +448,7 @@ def start_browser browser = Ferrum::Browser.new(options.except(*CUPRITE_OPTIONS)) @settings = Settings.new(options, browser.options.window_size) browser.on_page { |page| prepare_page(page) } + Deprecations.extend_browser(browser, self) browser rescue StandardError browser&.quit diff --git a/lib/capybara/cuprite/driver/deprecations.rb b/lib/capybara/cuprite/driver/deprecations.rb new file mode 100644 index 0000000..a04be6b --- /dev/null +++ b/lib/capybara/cuprite/driver/deprecations.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require "capybara/cuprite/deprecation" + +module Capybara + module Cuprite + class Driver < Capybara::Driver::Base + # The settings that moved from `page.driver.browser` to `page.driver`. They stay on the browser until 1.0 and + # forward to the driver with a warning. + # @api private + module Deprecations + # The settings the browser keeps until 1.0. + BROWSER_METHODS = %i[raise_on_unhandled_modal raise_on_unhandled_modal= + url_whitelist url_whitelist= url_allowlist url_allowlist= + url_blacklist url_blacklist= url_blocklist url_blocklist=].freeze + + module_function + + # Adds the moved settings to the browser, forwarding them to the driver with a warning. Only this browser + # object gets them, `Ferrum::Browser` stays as it is. + # + # @param browser [Ferrum::Browser] + # @param driver [Driver] + # @return [void] + def extend_browser(browser, driver) + BROWSER_METHODS.each do |name| + browser.define_singleton_method(name) do |*args| + Cuprite.deprecation_warning("page.driver.browser.#{name}", "use page.driver.#{name}") + driver.public_send(name, *args) + end + end + end + end + end + end +end diff --git a/sig/capybara/cuprite/deprecation.rbs b/sig/capybara/cuprite/deprecation.rbs new file mode 100644 index 0000000..7c4a6a2 --- /dev/null +++ b/sig/capybara/cuprite/deprecation.rbs @@ -0,0 +1,7 @@ +module Capybara + module Cuprite + REMOVE_DEPRECATED_IN: String + + def self.deprecation_warning: (String method, ?String? advice) -> void + end +end diff --git a/sig/capybara/cuprite/driver/deprecations.rbs b/sig/capybara/cuprite/driver/deprecations.rbs new file mode 100644 index 0000000..8338ab5 --- /dev/null +++ b/sig/capybara/cuprite/driver/deprecations.rbs @@ -0,0 +1,11 @@ +module Capybara + module Cuprite + class Driver < Capybara::Driver::Base + module Deprecations + BROWSER_METHODS: Array[Symbol] + + def self?.extend_browser: (Ferrum::Browser browser, Driver driver) -> void + end + end + end +end diff --git a/spec/driver/deprecations_spec.rb b/spec/driver/deprecations_spec.rb new file mode 100644 index 0000000..7adf7a3 --- /dev/null +++ b/spec/driver/deprecations_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +describe Capybara::Cuprite::Driver::Deprecations do + around do |example| + @driver = Capybara::Cuprite::Driver.new(nil) + example.run + ensure + @driver.quit + end + + describe ".extend_browser" do + def expect_deprecated(name, &) + message = "page.driver.browser.#{name} is deprecated and will be removed in 1.0, use page.driver.#{name} instead" + expect(&).to output(/#{Regexp.escape(message)}/).to_stderr + end + + it "forwards url_blocklist= on the browser to the driver with a warning" do + expect_deprecated("url_blocklist=") { @driver.browser.url_blocklist = /unwanted/ } + expect(@driver.url_blocklist).to eq([/unwanted/]) + end + + it "forwards url_allowlist on the browser to the driver with a warning" do + @driver.url_allowlist = /wanted/ + + expect_deprecated("url_allowlist") { expect(@driver.browser.url_allowlist).to eq([/wanted/]) } + end + + it "forwards raise_on_unhandled_modal= on the browser to the driver with a warning" do + expect_deprecated("raise_on_unhandled_modal=") { @driver.browser.raise_on_unhandled_modal = true } + expect(@driver.raise_on_unhandled_modal).to be(true) + end + end +end