From 9292d11f8d649a008d69ee9cdcc305cde6bdd10d Mon Sep 17 00:00:00 2001 From: Phillip Kessels Date: Wed, 13 Dec 2023 10:39:55 +0100 Subject: [PATCH] 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 --- lib/capybara/cuprite/driver.rb | 13 ++++++++++++- spec/lib/driver_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/capybara/cuprite/driver.rb b/lib/capybara/cuprite/driver.rb index 3100df2..83b0cbc 100644 --- a/lib/capybara/cuprite/driver.rb +++ b/lib/capybara/cuprite/driver.rb @@ -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() @@ -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) @@ -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 diff --git a/spec/lib/driver_spec.rb b/spec/lib/driver_spec.rb index 349d41f..513532b 100644 --- a/spec/lib/driver_spec.rb +++ b/spec/lib/driver_spec.rb @@ -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 @@ -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)