From 92b5dfe6db3fee8638977d9258321e37bdb12174 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Tue, 1 Dec 2026 16:16:58 +0300 Subject: [PATCH] ref(api)!: remove what 0.22 deprecated Remove the :extensions option, browser.add_extension and browser.extensions, use :init_scripts, add_init_script and init_scripts. Remove network.clear, use clear_traffic or clear_cache, and browser.reset, use contexts.reset. Utils::Deprecate goes too, nothing uses it anymore. --- CHANGELOG.md | 16 +++++++ docs/13-javascript.md | 3 +- docs/2-customization.md | 1 - lib/ferrum.rb | 1 - lib/ferrum/browser.rb | 22 +--------- lib/ferrum/browser/options.rb | 36 +--------------- lib/ferrum/network.rb | 22 ---------- lib/ferrum/utils/deprecate.rb | 76 ---------------------------------- sig/ferrum/browser.rbs | 8 ---- sig/ferrum/browser/options.rbs | 10 ----- sig/ferrum/network.rbs | 4 -- sig/ferrum/utils/deprecate.rbs | 13 ------ spec/browser_spec.rb | 63 ---------------------------- spec/network_spec.rb | 23 ---------- 14 files changed, 19 insertions(+), 279 deletions(-) delete mode 100644 lib/ferrum/utils/deprecate.rb delete mode 100644 sig/ferrum/utils/deprecate.rbs diff --git a/CHANGELOG.md b/CHANGELOG.md index fde06f5a..6b81a8b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,19 @@ +## [Unreleased](https://github.com/rubycdp/ferrum/compare/v0.22.0...main) ## + +### Added + +### Changed + +### Deprecated + +### Fixed + +### Removed +- `:extensions`, `browser.add_extension` and `browser.extensions`, use `:init_scripts`, `add_init_script` and + `init_scripts` +- `network.clear`, use `network.clear_traffic` or `network.clear_cache` +- `browser.reset`, use `browser.contexts.reset` + ## [0.22.0](https://github.com/rubycdp/ferrum/compare/v0.21.0...v0.22.0) (December 1, 2026) ## ### Added diff --git a/docs/13-javascript.md b/docs/13-javascript.md index d758ff3a..d3f36790 100644 --- a/docs/13-javascript.md +++ b/docs/13-javascript.md @@ -133,8 +133,7 @@ page.remove_script_on_new_document(id) Adds an init script, see the `:init_scripts` option. It runs in every page and iframe, popups and out-of-process iframes included, attached after the call, before their own scripts. Pages already open keep the scripts they had. -`browser.init_scripts` returns the source of every init script. `add_extension` and `extensions` are deprecated -aliases. +`browser.init_scripts` returns the source of every init script. * script `String | Hash` a path to a JS file or `{ source: "..." }` diff --git a/docs/2-customization.md b/docs/2-customization.md index b4b4f93f..ddc1866a 100644 --- a/docs/2-customization.md +++ b/docs/2-customization.md @@ -23,7 +23,6 @@ Ferrum::Browser.new(options) * `:init_scripts` (Array[String | Hash]) - Paths to JS files or JS source code to run in every page and iframe before their own scripts, e.g. `["/path/to/script.js", { source: "window.secret = 'top'" }]`. Files are read in `Browser.new`. See `add_init_script` in [JavaScript](/docs/ferrum/javascript). - * `:extensions` - Deprecated, use `:init_scripts`. * `:logger` (Object responding to `puts`, or a `Logger`) - When present, debug output is written to it, see [Logging](#logging). * `:slowmo` (Integer | Float) - Set a delay in seconds to wait before sending command. diff --git a/lib/ferrum.rb b/lib/ferrum.rb index cabbd84d..802914a7 100644 --- a/lib/ferrum.rb +++ b/lib/ferrum.rb @@ -11,7 +11,6 @@ require "ferrum/concurrency/dispatcher" require "ferrum/utils/attempt" require "ferrum/utils/poll" -require "ferrum/utils/deprecate" require "ferrum/errors" require "ferrum/browser" require "ferrum/remote_object" diff --git a/lib/ferrum/browser.rb b/lib/ferrum/browser.rb index 60b8a0ab..c6efab58 100644 --- a/lib/ferrum/browser.rb +++ b/lib/ferrum/browser.rb @@ -26,8 +26,6 @@ module Ferrum class Browser extend Forwardable - RESET_DEPRECATED = "browser.reset will be removed in Ferrum 1.0. Use browser.contexts.reset instead." - delegate %i[default_context] => :contexts delegate %i[targets create_target page pages await_page await_page! workers service_workers attach_target close_target cookies downloads] => :default_context @@ -50,7 +48,7 @@ class Browser attr_reader :client, :process, :contexts, :options delegate %i[timeout timeout= base_url base_url= default_user_agent default_user_agent= init_scripts - add_init_script extensions add_extension on_page] => :options + add_init_script on_page] => :options # # Initializes the browser. @@ -79,9 +77,6 @@ class Browser # Paths to JS files or JS source code to run in every page and iframe before their own scripts, e.g. # `["/path/to/script.js", { source: "window.secret = 'top'" }]`. # - # @option options [Array] :extensions - # Deprecated, use `:init_scripts`. - # # @option options [#puts, Logger] :logger # When present, debug output is written to this object, see {Log}. # @@ -195,21 +190,6 @@ def create_page(new_context: false, new_window: false, proxy: nil) end end - # - # Closes browser tabs opened by the `Browser` instance. - # - # @deprecated Use {Contexts#reset} via `browser.contexts.reset`. - # - # @return [nil] - # - # @raise [TimeoutError] - # - def reset - Utils::Deprecate.warn(RESET_DEPRECATED, user_code: true) - contexts.reset - nil - end - # # Restarts the browser process, keeping the same options. # diff --git a/lib/ferrum/browser/options.rb b/lib/ferrum/browser/options.rb index 51b4dbf5..05535538 100644 --- a/lib/ferrum/browser/options.rb +++ b/lib/ferrum/browser/options.rb @@ -17,9 +17,6 @@ class Options BASE_URL_SCHEMES = %w[http https].freeze DEFAULT_TIMEOUT = ENV.fetch("FERRUM_DEFAULT_TIMEOUT", 5).to_f PROCESS_TIMEOUT = ENV.fetch("FERRUM_PROCESS_TIMEOUT", 10).to_i - EXTENSIONS_DEPRECATED = "The :extensions option will be removed in Ferrum 1.0. Use :init_scripts instead." - EXTENSIONS_READER_DEPRECATED = "extensions will be removed in Ferrum 1.0. Use init_scripts instead." - ADD_EXTENSION_DEPRECATED = "add_extension will be removed in Ferrum 1.0. Use add_init_script instead." attr_reader :window_size, :logger, :log, :ws_max_receive_size, :base_url, :slowmo, @@ -94,36 +91,6 @@ def add_init_script(script) @init_scripts = [*@init_scripts, read_init_script(script)].freeze end - # - # Adds a script to run in every new document. - # - # @deprecated Use {#add_init_script}. - # - # @param [String, Hash] extension - # A path to a JS file, or `{ source: "..." }`. - # - # @return [Array] - # - # @raise [Errno::ENOENT] - # If the file doesn't exist. - # - def add_extension(extension) - Utils::Deprecate.warn(ADD_EXTENSION_DEPRECATED, user_code: true) - add_init_script(extension) - end - - # - # JS source code of every init script. - # - # @deprecated Use {#init_scripts}. - # - # @return [Array] - # - def extensions - Utils::Deprecate.warn(EXTENSIONS_READER_DEPRECATED, user_code: true) - init_scripts - end - # # Calls the block with every new page before Ferrum returns it, so the page can be set up before anything runs # on it. That covers pages you create and pages other pages open, like popups. Pages already open when you call @@ -191,8 +158,7 @@ def to_h private def parse_init_scripts - Utils::Deprecate.warn(EXTENSIONS_DEPRECATED, user_code: true) if @options.key?(:extensions) - [*@options[:extensions], *@options[:init_scripts]].map { |s| read_init_script(s) }.freeze + Array(@options[:init_scripts]).map { |s| read_init_script(s) }.freeze end def read_init_script(script) diff --git a/lib/ferrum/network.rb b/lib/ferrum/network.rb index bdc4f9c6..8790e2b4 100644 --- a/lib/ferrum/network.rb +++ b/lib/ferrum/network.rb @@ -18,9 +18,6 @@ module Ferrum # class Network WAIT_INTERVAL = 0.05 - CLEAR_TYPE = %i[traffic cache].freeze - CLEAR_DEPRECATED = "network.clear will be removed in Ferrum 1.0. Use network.clear_traffic or " \ - "network.clear_cache instead." EMULATE_KEYS = %i[user_agent accept_language platform metadata].freeze METADATA_REQUIRED = %i[platform platform_version architecture model mobile].freeze METADATA_OPTIONAL = %i[brands full_version_list full_version bitness wow64 form_factors].freeze @@ -179,25 +176,6 @@ def status response&.status end - # - # Clear browser's cache or collected traffic. - # - # @deprecated Use {#clear_traffic} or {#clear_cache}. - # - # @param [:traffic, :cache] type - # The type of traffic to clear. - # - # @return [nil] - # - # @raise [TimeoutError] - # - def clear(type) - raise ArgumentError, ":type should be #{CLEAR_TYPE.map(&:inspect).join(' | ')}" unless CLEAR_TYPE.include?(type) - - Utils::Deprecate.warn(CLEAR_DEPRECATED, user_code: true) - type == :traffic ? clear_traffic : clear_cache - end - # # Forgets the collected traffic, including the main frame's {#request} and # {#response}. diff --git a/lib/ferrum/utils/deprecate.rb b/lib/ferrum/utils/deprecate.rb deleted file mode 100644 index b231e487..00000000 --- a/lib/ferrum/utils/deprecate.rb +++ /dev/null @@ -1,76 +0,0 @@ -# frozen_string_literal: true - -module Ferrum - module Utils - # - # Emits deprecation warnings for APIs removed in a later release. - # - # Warnings are printed once per unique message and call site so that a - # deprecated call inside a loop doesn't flood the output. Set - # `FERRUM_DEPRECATION_WARNINGS=0` to silence them entirely, or - # `FERRUM_DEPRECATION_WARNINGS=raise` to turn them into errors while - # migrating a suite. - # - module Deprecate - @seen = Concurrent::Set.new - - module_function - - # - # Warns about a deprecation, naming where it comes from. - # - # @param [String] message - # What changes, and what to do instead. - # - # @param [Boolean] user_code - # Whether to name the first caller outside any installed gem, which is - # where a script passed through a library like Capybara was written, - # rather than the direct caller. - # - # @return [void] - # - # @raise [Ferrum::Error] - # When `FERRUM_DEPRECATION_WARNINGS=raise`. - # - def warn(message, user_code: false) - mode = ENV.fetch("FERRUM_DEPRECATION_WARNINGS", "warn") - return if %w[0 false].include?(mode) - - location = caller_locations(1)&.find { |frame| !skip?(frame.path.to_s, user_code) } - message = "[Ferrum] DEPRECATION: #{message}" - message += "\n called from #{location.path}:#{location.lineno}" if location - - raise Ferrum::Error, message if mode == "raise" - return unless @seen.add?(message) - - Kernel.warn(message) - end - - # - # Forgets which warnings have already been printed. Only useful in tests. - # - # @return [void] - # - def reset! - @seen.clear - end - - # - # Whether a caller's path is left out when naming where a deprecated - # call comes from. - # - # @param [String] path - # - # @param [Boolean] user_code - # Whether to leave out installed gems as well as Ferrum itself. - # - # @return [Boolean] - # - def skip?(path, user_code) - return true if path.include?("/lib/ferrum/") || path.end_with?("forwardable.rb") || path.start_with?("<") - - user_code && Gem.path.any? { |gem_path| path.start_with?(File.join(gem_path, "")) } - end - end - end -end diff --git a/sig/ferrum/browser.rbs b/sig/ferrum/browser.rbs index e49ebedf..c2827415 100644 --- a/sig/ferrum/browser.rbs +++ b/sig/ferrum/browser.rbs @@ -2,8 +2,6 @@ module Ferrum class Browser extend Forwardable - RESET_DEPRECATED: String - attr_reader client: ::Ferrum::Client attr_reader process: Browser::Process attr_reader contexts: Contexts @@ -35,14 +33,8 @@ module Ferrum def add_init_script: (String | Hash[Symbol, String] script) -> ::Array[String] - %a{deprecated} def extensions: () -> ::Array[String] - - %a{deprecated} def add_extension: (String | Hash[Symbol, String] extension) -> ::Array[String] - def command: (String method, ?timeout: (::Numeric | Deadline)?, **untyped params) -> Hash[String, untyped] - %a{deprecated} def reset: () -> nil - def restart: () -> void def quit: (?wait: bool) -> Thread? diff --git a/sig/ferrum/browser/options.rbs b/sig/ferrum/browser/options.rbs index a2958ac0..10a5fb9d 100644 --- a/sig/ferrum/browser/options.rbs +++ b/sig/ferrum/browser/options.rbs @@ -13,12 +13,6 @@ module Ferrum PROCESS_TIMEOUT: ::Integer - EXTENSIONS_DEPRECATED: String - - EXTENSIONS_READER_DEPRECATED: String - - ADD_EXTENSION_DEPRECATED: String - attr_reader window_size: ::Array[::Integer] attr_accessor timeout: ::Numeric @@ -99,10 +93,6 @@ module Ferrum def add_init_script: (String | Hash[Symbol, String] script) -> ::Array[String] - %a{deprecated} def add_extension: (String | Hash[Symbol, String] extension) -> ::Array[String] - - %a{deprecated} def extensions: () -> ::Array[String] - def on_page: () { (Page page) -> void } -> nil attr_reader page_handlers: ::Array[^(Page) -> void] diff --git a/sig/ferrum/network.rbs b/sig/ferrum/network.rbs index 9fee3064..4ceb0050 100644 --- a/sig/ferrum/network.rbs +++ b/sig/ferrum/network.rbs @@ -1,8 +1,6 @@ module Ferrum class Network WAIT_INTERVAL: ::Float - CLEAR_TYPE: ::Array[:traffic | :cache] - CLEAR_DEPRECATED: String EMULATE_KEYS: ::Array[:user_agent | :accept_language | :platform | :metadata] METADATA_REQUIRED: ::Array[Symbol] METADATA_OPTIONAL: ::Array[Symbol] @@ -37,8 +35,6 @@ module Ferrum def status: () -> untyped - %a{deprecated} def clear: (:traffic | :cache `type`) -> nil - def clear_traffic: () -> nil def clear_cache: () -> nil diff --git a/sig/ferrum/utils/deprecate.rbs b/sig/ferrum/utils/deprecate.rbs deleted file mode 100644 index e58cf030..00000000 --- a/sig/ferrum/utils/deprecate.rbs +++ /dev/null @@ -1,13 +0,0 @@ -module Ferrum - module Utils - module Deprecate - self.@seen: Concurrent::Set[::String] - - def self?.warn: (::String message, ?user_code: bool) -> void - - def self?.reset!: () -> void - - def self?.skip?: (::String path, bool user_code) -> bool - end - end -end diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index 432f3988..d1614273 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -406,23 +406,6 @@ def broken.puts(*) = raise(IOError, "closed stream") end.to raise_error(Errno::ENOENT) end - it "warns that :extensions is deprecated and runs them before :init_scripts", :launch do - Ferrum::Utils::Deprecate.reset! - browser = nil - - expect do - browser = Ferrum::Browser.new(base_url: base_url, - extensions: [{ source: "window.order = ['extension']" }], - init_scripts: [{ source: "window.order.push('init script')" }]) - end.to output(/The :extensions option will be removed in Ferrum 1\.0.*called from #{__FILE__}/m).to_stderr - browser.go_to - - expect(browser.evaluate("window.order")).to eq(["extension", "init script"]) - ensure - browser&.quit - Ferrum::Utils::Deprecate.reset! - end - it "supports :port argument", :launch do browser = Ferrum::Browser.new(port: 12_345) browser.go_to(base_url) @@ -1079,39 +1062,6 @@ def closed_by_peer?(socket) end end - describe "#add_extension" do - it "warns that add_extension is deprecated and adds an init script", :launch do - browser = Ferrum::Browser.new(base_url: base_url) - Ferrum::Utils::Deprecate.reset! - - expect { browser.add_extension(source: "window.secret = 'top'") } - .to output(/add_extension will be removed in Ferrum 1\.0.*called from #{__FILE__}/m).to_stderr - page = browser.create_page - page.go_to("/requiring_custom_extension") - - expect(page.evaluate("window.secret")).to eq("top") - ensure - browser&.quit - Ferrum::Utils::Deprecate.reset! - end - end - - describe "#extensions" do - it "warns that extensions is deprecated and returns the init scripts", :launch do - browser = Ferrum::Browser.new(init_scripts: [{ source: "window.secret = 'top'" }]) - Ferrum::Utils::Deprecate.reset! - - scripts = nil - - expect { scripts = browser.extensions } - .to output(/extensions will be removed in Ferrum 1\.0.*called from #{__FILE__}/m).to_stderr - expect(scripts).to eq(["window.secret = 'top'"]) - ensure - browser&.quit - Ferrum::Utils::Deprecate.reset! - end - end - describe "#crash" do it "works after crash with explicit restart" do expect { browser.crash }.to raise_error(Ferrum::DeadBrowserError) @@ -1412,19 +1362,6 @@ def expect_cleanup_when_close_raises(error) end end - describe "#reset" do - it "warns that reset is deprecated and disposes the contexts" do - context = browser.contexts.create - Ferrum::Utils::Deprecate.reset! - - expect { browser.reset } - .to output(/browser\.reset will be removed in Ferrum 1\.0.*called from #{__FILE__}/m).to_stderr - expect(browser.contexts[context.id]).to be_nil - ensure - Ferrum::Utils::Deprecate.reset! - end - end - describe "#create_page" do it "supports calling without block" do expect(browser.contexts.size).to eq(0) diff --git a/spec/network_spec.rb b/spec/network_spec.rb index 6b436f86..31aef66d 100644 --- a/spec/network_spec.rb +++ b/spec/network_spec.rb @@ -202,29 +202,6 @@ end end - describe "#clear" do - it "raises error when type is not in the list" do - page.go_to("/with_js") - expect(traffic.length).to eq(4) - - expect { network.clear(:something) }.to raise_error(ArgumentError, ":type should be :traffic | :cache") - - expect(traffic.length).to eq(4) - end - - it "warns that clear is deprecated and clears all the traffic" do - page.go_to("/with_js") - expect(traffic.length).to eq(4) - Ferrum::Utils::Deprecate.reset! - - expect { page.network.clear(:traffic) } - .to output(/network\.clear will be removed in Ferrum 1\.0.*called from #{__FILE__}/m).to_stderr - expect(traffic.length).to eq(0) - ensure - Ferrum::Utils::Deprecate.reset! - end - end - describe "#clear_traffic" do it "clears the traffic and the main frame's request and response" do page.go_to("/with_js")