diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c016e9d..942d0430 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Deprecated - `current_url` and `current_title` on pages and frames, will be removed in 0.22. Use `url` and `title`, or `frame.page.url` for the top window from a frame, `frame.url` is the frame's own +- `:js_errors`, will be removed in 0.22. Subscribe with `page.on("Runtime.exceptionThrown")` instead ### Fixed diff --git a/docs/2-customization.md b/docs/2-customization.md index e21b5e07..d65a2a94 100644 --- a/docs/2-customization.md +++ b/docs/2-customization.md @@ -29,7 +29,8 @@ Ferrum::Browser.new(options) Useful companion of headless option, so that you have time to see changes. * `:timeout` (Numeric) - The number of seconds we'll wait for a response when communicating with browser. Default is 5. - * `:js_errors` (Boolean) - When true, JavaScript errors get re-raised in Ruby. + * `:js_errors` (Boolean) - Deprecated, will be removed in 0.22. When true, JavaScript errors get re-raised in + Ruby. Subscribe with `page.on("Runtime.exceptionThrown") { |params| ... }` instead. * `:pending_connection_errors` (Boolean) - Raise `PendingConnectionsError` when main frame is still waiting for slow responses and timeout is reached. Default is false. * `:browser_name` (Symbol) - `:chrome` by default, `:opera` and `:edge` are @@ -176,9 +177,6 @@ Ferrum::Browser.new(headless: false, window_size: [1920, 1080]) # Connect to an existing Chrome instance Ferrum::Browser.new(url: "http://localhost:9222") -# Enable JavaScript error raising -Ferrum::Browser.new(js_errors: true) - # Set custom timeout and slowmo for debugging Ferrum::Browser.new(timeout: 10, slowmo: 0.5) diff --git a/lib/ferrum/browser.rb b/lib/ferrum/browser.rb index 361b5cd7..05470c58 100644 --- a/lib/ferrum/browser.rb +++ b/lib/ferrum/browser.rb @@ -97,7 +97,9 @@ class Browser # `Target.attachToTarget`. These normally resolve in milliseconds. # # @option options [Boolean] :js_errors - # When true, JavaScript errors get re-raised in Ruby. + # Deprecated, will be removed in Ferrum 0.22. When true, JavaScript + # errors get re-raised in Ruby. Subscribe with + # `page.on("Runtime.exceptionThrown")` instead. # # @option options [Boolean] :pending_connection_errors (true) # When main frame is still waiting for slow responses while timeout is diff --git a/lib/ferrum/browser/options.rb b/lib/ferrum/browser/options.rb index 3a1c0fff..7468d0bd 100644 --- a/lib/ferrum/browser/options.rb +++ b/lib/ferrum/browser/options.rb @@ -36,7 +36,7 @@ def initialize(options = nil) @timeout = @options.fetch(:timeout, DEFAULT_TIMEOUT) @protocol_timeout = @options.fetch(:protocol_timeout, DEFAULT_PROTOCOL_TIMEOUT) @window_size = @options.fetch(:window_size, WINDOW_SIZE) - @js_errors = @options.fetch(:js_errors, false) + @js_errors = parse_js_errors @headless = @options.fetch(:headless, true) @incognito = @options.fetch(:incognito, true) @dockerize = @options.fetch(:dockerize, false) @@ -132,6 +132,17 @@ def to_h private + def parse_js_errors + return false unless @options.key?(:js_errors) + + Utils::Deprecate.warn( + ":js_errors will be removed in Ferrum 0.22. Subscribe with " \ + "page.on(\"Runtime.exceptionThrown\") { |params| ... } to handle JavaScript errors.", + user_code: true + ) + @options[:js_errors] + end + def read_extension(extension) (extension.is_a?(Hash) && extension[:source]) || File.read(extension) end diff --git a/sig/ferrum/browser/options.rbs b/sig/ferrum/browser/options.rbs index 1775c2f7..24eb243c 100644 --- a/sig/ferrum/browser/options.rbs +++ b/sig/ferrum/browser/options.rbs @@ -28,7 +28,7 @@ module Ferrum attr_reader ws_max_receive_size: ::Integer? - attr_reader js_errors: bool + %a{deprecated} attr_reader js_errors: bool attr_reader base_url: ::Addressable::URI? @@ -112,6 +112,8 @@ module Ferrum private + def parse_js_errors: () -> bool + def read_extension: (String | Hash[Symbol, String] extension) -> String def parse_logger: (untyped logger) -> [untyped, Log?] diff --git a/spec/frame/runtime_spec.rb b/spec/frame/runtime_spec.rb index 8df87c3a..35a8948e 100644 --- a/spec/frame/runtime_spec.rb +++ b/spec/frame/runtime_spec.rb @@ -27,8 +27,19 @@ context "with javascript errors" do let(:browser) { Ferrum::Browser.new(base_url: base_url, js_errors: true) } + before { allow(Ferrum::Utils::Deprecate).to receive(:warn) } + after { browser.quit } + it "warns that :js_errors is deprecated", :launch do + Ferrum::Utils::Deprecate.reset! + allow(Ferrum::Utils::Deprecate).to receive(:warn).and_call_original + + expect { browser }.to output(/:js_errors will be removed in Ferrum 0\.22.*called from #{__FILE__}/m).to_stderr + ensure + Ferrum::Utils::Deprecate.reset! + end + it "propagates a Javascript error to a ruby exception", :launch do expect do browser.execute(%(throw new Error("zomg")))