diff --git a/CHANGELOG.md b/CHANGELOG.md index b862db3b..96256d47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ ### Fixed +- `:ws_url` option is now used without modifications WYSIWYG. + ### Removed diff --git a/README.md b/README.md index 350df06e..9af47332 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,7 @@ Ferrum::Browser.new(options) * `:url` (String) - URL for a running instance of Chrome. If this is set, a browser process will not be spawned. * `:ws_url` (String) - Websocket url for a running instance of Chrome. If this is set, a - browser process will not be spawned. + browser process will not be spawned. It's higher priority than `:url`, setting both doesn't make sense. * `:process_timeout` (Integer) - How long to wait for the Chrome process to respond on startup. * `:ws_max_receive_size` (Integer) - How big messages to accept from Chrome diff --git a/lib/ferrum/browser/process.rb b/lib/ferrum/browser/process.rb index 006f975a..163cccfe 100644 --- a/lib/ferrum/browser/process.rb +++ b/lib/ferrum/browser/process.rb @@ -62,15 +62,11 @@ def self.directory_remover(path) def initialize(options) @pid = @xvfb = @user_data_dir = nil - if options.ws_url - response = parse_json_version(options.ws_url) - self.ws_url = response&.[]("webSocketDebuggerUrl") || options.ws_url - return - end - - if options.url - response = parse_json_version(options.url) - self.ws_url = response&.[]("webSocketDebuggerUrl") + if options.ws_url || options.url + # `:ws_url` option is higher priority than `:url`, parse versions + # and use it as a ws_url, otherwise use what has been parsed. + response = parse_json_version(options.ws_url || options.url) + self.ws_url = options.ws_url || response&.[]("webSocketDebuggerUrl") return end @@ -207,7 +203,7 @@ def parse_json_version(url) @protocol_version = response["Protocol-Version"] response - rescue StandardError + rescue JSON::ParserError # nop end end diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index da3b1f88..6856f5c3 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -135,8 +135,7 @@ it "supports :ws_url argument" do with_external_browser do |url, process| - uri = Addressable::URI.parse(url) - browser = Ferrum::Browser.new(ws_url: "ws://#{uri.host}:#{uri.port}") + browser = Ferrum::Browser.new(ws_url: web_socket_debugger_url(url)) expect(process.v8_version).not_to be_nil expect(process.browser_version).not_to be_nil expect(process.webkit_version).not_to be_nil diff --git a/spec/support/global_helpers.rb b/spec/support/global_helpers.rb index 59266934..08f3853d 100644 --- a/spec/support/global_helpers.rb +++ b/spec/support/global_helpers.rb @@ -58,4 +58,12 @@ def with_external_browser(host: "127.0.0.1", port: 32_001) process.stop end end + + def web_socket_debugger_url(url) + uri = Addressable::URI.parse(url) + url = uri.join("/json/version").to_s + JSON.parse(Net::HTTP.get(URI(url)))["webSocketDebuggerUrl"] + rescue JSON::ParserError + # nop + end end diff --git a/spec/unit/process_spec.rb b/spec/unit/process_spec.rb index 519a2ee7..5ad6c435 100644 --- a/spec/unit/process_spec.rb +++ b/spec/unit/process_spec.rb @@ -10,6 +10,7 @@ allow(Ferrum::Client).to receive(:new).and_return(double.as_null_object) allow_any_instance_of(Ferrum::Browser::Process).to receive(:parse_ws_url) + allow_any_instance_of(Ferrum::Browser::Process).to receive(:parse_json_version) subject.send(:start) @@ -30,6 +31,7 @@ allow(Process).to receive(:spawn).with({ "LD_PRELOAD" => "some.so" }, any_args).and_return(123_456_789) allow_any_instance_of(Ferrum::Browser::Process).to receive(:parse_ws_url) + allow_any_instance_of(Ferrum::Browser::Process).to receive(:parse_json_version) subject.send(:start) subject.quit