Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly open the devtools #252

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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