Skip to content

Commit

Permalink
fix: correctly open the devtools (#252)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
phikes authored Dec 13, 2023
1 parent ddc9cee commit 9292d11
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/capybara/cuprite/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def initialize(app, options = {})
@screen_size ||= DEFAULT_MAXIMIZE_SCREEN_SIZE
@options[:save_path] ||= File.expand_path(Capybara.save_path) if Capybara.save_path

@options[:"remote-allow-origins"] = "*"

ENV["FERRUM_DEBUG"] = "true" if ENV["CUPRITE_DEBUG"]

super()
Expand Down Expand Up @@ -265,7 +267,12 @@ def basic_authorize(user, password)
alias authorize basic_authorize

def debug_url
"http://#{browser.process.host}:#{browser.process.port}"
response = JSON.parse(Net::HTTP.get(URI(build_remote_debug_url(path: "/json"))))

devtools_frontend_path = response[0]&.[]("devtoolsFrontendUrl")
raise "Could not generate debug url for remote debugging session" unless devtools_frontend_path

build_remote_debug_url path: devtools_frontend_path
end

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

private

def build_remote_debug_url(path:)
"http://#{browser.process.host}:#{browser.process.port}#{path}"
end

def default_domain
if @started
URI.parse(browser.current_url).host
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/driver_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# frozen_string_literal: true

describe Capybara::Cuprite::Driver do
describe "options" do
it "sets the remote-allow-origins option" do
driver = described_class.new nil

expect(driver.browser.options.to_h).to include("remote-allow-origins": "*")
end
end

describe "save_path configuration" do
it "defaults to the Capybara save path" do
driver = with_capybara_save_path("/tmp/capybara-save-path") do
Expand All @@ -21,6 +29,19 @@
end
end

describe "debug_url" do
it "parses the devtools frontend url correctly" do
driver = described_class.new nil, {port: 12345}
driver.browser # initialize browser before stubbing Net::HTTP as it also calls it
uri = instance_double URI

allow(driver).to receive(:URI).with("http://127.0.0.1:12345/json").and_return uri
allow(Net::HTTP).to receive(:get).with(uri).and_return "[{\"devtoolsFrontendUrl\":\"/works\"}]"

expect(driver.debug_url).to eq "http://127.0.0.1:12345/works"
end
end

private

def with_capybara_save_path(path)
Expand Down

0 comments on commit 9292d11

Please sign in to comment.