Skip to content

Commit a981af2

Browse files
committed
fix: Don't modify ws_url
1 parent 7df5fae commit a981af2

File tree

6 files changed

+24
-16
lines changed

6 files changed

+24
-16
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: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +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 = options.ws_url
68-
return
69-
elsif options.url
70-
response = parse_json_version(options.url)
71-
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")
7270
return
7371
end
7472

@@ -185,19 +183,18 @@ def close_io(*ios)
185183
end
186184

187185
def parse_json_version(url)
188-
uri = Addressable::URI.parse(url)
189-
uri.path = "/json/version"
186+
url = URI.join(url, "/json/version")
190187

191-
if %w[wss ws].include?(uri.scheme)
192-
uri.scheme = case uri.scheme
188+
if %w[wss ws].include?(url.scheme)
189+
url.scheme = case url.scheme
193190
when "ws"
194191
"http"
195192
when "wss"
196193
"https"
197194
end
198195
end
199196

200-
response = JSON.parse(::Net::HTTP.get(URI(uri.to_s)))
197+
response = JSON.parse(::Net::HTTP.get(URI(url.to_s)))
201198

202199
@v8_version = response["V8-Version"]
203200
@browser_version = response["Browser"]
@@ -206,7 +203,7 @@ def parse_json_version(url)
206203
@protocol_version = response["Protocol-Version"]
207204

208205
response
209-
rescue StandardError
206+
rescue JSON::ParserError
210207
# nop
211208
end
212209
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)