Skip to content

Commit 9292d11

Browse files
authored
fix: correctly open the devtools (#252)
* fix: correctly open the devtools The original debug url was deprecated apparently (I didn't confirm that; a call to `page.driver.debug` just opened a blank page). Instead there is an endpoint now that allows to parse out a remote devtools frontend path (although they incorrectly call it a url). So instead of generating a fixed debug_url now we make a request to said endpoint and parse the actual (dynamic; contains a uuid) devtools path and build a url from it. * fix: add remote allow origins so the browser allows to connect * chore: add test for remote-allow-origins option
1 parent ddc9cee commit 9292d11

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/capybara/cuprite/driver.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def initialize(app, options = {})
3434
@screen_size ||= DEFAULT_MAXIMIZE_SCREEN_SIZE
3535
@options[:save_path] ||= File.expand_path(Capybara.save_path) if Capybara.save_path
3636

37+
@options[:"remote-allow-origins"] = "*"
38+
3739
ENV["FERRUM_DEBUG"] = "true" if ENV["CUPRITE_DEBUG"]
3840

3941
super()
@@ -265,7 +267,12 @@ def basic_authorize(user, password)
265267
alias authorize basic_authorize
266268

267269
def debug_url
268-
"http://#{browser.process.host}:#{browser.process.port}"
270+
response = JSON.parse(Net::HTTP.get(URI(build_remote_debug_url(path: "/json"))))
271+
272+
devtools_frontend_path = response[0]&.[]("devtoolsFrontendUrl")
273+
raise "Could not generate debug url for remote debugging session" unless devtools_frontend_path
274+
275+
build_remote_debug_url path: devtools_frontend_path
269276
end
270277

271278
def debug(binding = nil)
@@ -363,6 +370,10 @@ def dismiss_modal(type, options = {})
363370

364371
private
365372

373+
def build_remote_debug_url(path:)
374+
"http://#{browser.process.host}:#{browser.process.port}#{path}"
375+
end
376+
366377
def default_domain
367378
if @started
368379
URI.parse(browser.current_url).host

spec/lib/driver_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# frozen_string_literal: true
22

33
describe Capybara::Cuprite::Driver do
4+
describe "options" do
5+
it "sets the remote-allow-origins option" do
6+
driver = described_class.new nil
7+
8+
expect(driver.browser.options.to_h).to include("remote-allow-origins": "*")
9+
end
10+
end
11+
412
describe "save_path configuration" do
513
it "defaults to the Capybara save path" do
614
driver = with_capybara_save_path("/tmp/capybara-save-path") do
@@ -21,6 +29,19 @@
2129
end
2230
end
2331

32+
describe "debug_url" do
33+
it "parses the devtools frontend url correctly" do
34+
driver = described_class.new nil, {port: 12345}
35+
driver.browser # initialize browser before stubbing Net::HTTP as it also calls it
36+
uri = instance_double URI
37+
38+
allow(driver).to receive(:URI).with("http://127.0.0.1:12345/json").and_return uri
39+
allow(Net::HTTP).to receive(:get).with(uri).and_return "[{\"devtoolsFrontendUrl\":\"/works\"}]"
40+
41+
expect(driver.debug_url).to eq "http://127.0.0.1:12345/works"
42+
end
43+
end
44+
2445
private
2546

2647
def with_capybara_save_path(path)

0 commit comments

Comments
 (0)