Skip to content

Commit 8ba95cd

Browse files
committed
Merge branch 'Aubermean-main'
2 parents d851b23 + a981af2 commit 8ba95cd

File tree

6 files changed

+20
-13
lines changed

6 files changed

+20
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
### Fixed
1313

14+
- `:ws_url` option is now used without modifications WYSIWYG.
15+
1416
### Removed
1517

1618

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Ferrum::Browser.new(options)
188188
* `:url` (String) - URL for a running instance of Chrome. If this is set, a
189189
browser process will not be spawned.
190190
* `:ws_url` (String) - Websocket url for a running instance of Chrome. If this is set, a
191-
browser process will not be spawned.
191+
browser process will not be spawned. It's higher priority than `:url`, setting both doesn't make sense.
192192
* `:process_timeout` (Integer) - How long to wait for the Chrome process to
193193
respond on startup.
194194
* `:ws_max_receive_size` (Integer) - How big messages to accept from Chrome

lib/ferrum/browser/process.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,11 @@ def self.directory_remover(path)
6262
def initialize(options)
6363
@pid = @xvfb = @user_data_dir = nil
6464

65-
if options.ws_url
66-
response = parse_json_version(options.ws_url)
67-
self.ws_url = response&.[]("webSocketDebuggerUrl") || options.ws_url
68-
return
69-
end
70-
71-
if options.url
72-
response = parse_json_version(options.url)
73-
self.ws_url = response&.[]("webSocketDebuggerUrl")
65+
if options.ws_url || options.url
66+
# `:ws_url` option is higher priority than `:url`, parse versions
67+
# and use it as a ws_url, otherwise use what has been parsed.
68+
response = parse_json_version(options.ws_url || options.url)
69+
self.ws_url = options.ws_url || response&.[]("webSocketDebuggerUrl")
7470
return
7571
end
7672

@@ -207,7 +203,7 @@ def parse_json_version(url)
207203
@protocol_version = response["Protocol-Version"]
208204

209205
response
210-
rescue StandardError
206+
rescue JSON::ParserError
211207
# nop
212208
end
213209
end

spec/browser_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@
135135

136136
it "supports :ws_url argument" do
137137
with_external_browser do |url, process|
138-
uri = Addressable::URI.parse(url)
139-
browser = Ferrum::Browser.new(ws_url: "ws://#{uri.host}:#{uri.port}")
138+
browser = Ferrum::Browser.new(ws_url: web_socket_debugger_url(url))
140139
expect(process.v8_version).not_to be_nil
141140
expect(process.browser_version).not_to be_nil
142141
expect(process.webkit_version).not_to be_nil

spec/support/global_helpers.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,12 @@ def with_external_browser(host: "127.0.0.1", port: 32_001)
5858
process.stop
5959
end
6060
end
61+
62+
def web_socket_debugger_url(url)
63+
uri = Addressable::URI.parse(url)
64+
url = uri.join("/json/version").to_s
65+
JSON.parse(Net::HTTP.get(URI(url)))["webSocketDebuggerUrl"]
66+
rescue JSON::ParserError
67+
# nop
68+
end
6169
end

spec/unit/process_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
allow(Ferrum::Client).to receive(:new).and_return(double.as_null_object)
1111

1212
allow_any_instance_of(Ferrum::Browser::Process).to receive(:parse_ws_url)
13+
allow_any_instance_of(Ferrum::Browser::Process).to receive(:parse_json_version)
1314

1415
subject.send(:start)
1516

@@ -30,6 +31,7 @@
3031
allow(Process).to receive(:spawn).with({ "LD_PRELOAD" => "some.so" }, any_args).and_return(123_456_789)
3132

3233
allow_any_instance_of(Ferrum::Browser::Process).to receive(:parse_ws_url)
34+
allow_any_instance_of(Ferrum::Browser::Process).to receive(:parse_json_version)
3335

3436
subject.send(:start)
3537
subject.quit

0 commit comments

Comments
 (0)