diff --git a/lib/capybara/cuprite/driver.rb b/lib/capybara/cuprite/driver.rb index 3100df25..83b0cbc1 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 349d41f9..513532bf 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)