Skip to content

Commit

Permalink
feat: Add Ferrum::Browser#debug (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
route authored Feb 10, 2025
1 parent d0a3f43 commit dad31db
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- `Ferrum::Mouse#scroll_by` to be able to scroll by, as alternative to `scroll_to` [#514]
- `Ferrum::Network::Exchange#unknown` determines if the exchange is in an unknown state, meaning that browser might not return info about it [#426]
- `Ferrum::Network::Exchange#loader_id` returns loader id [#426]
- `Ferrum::Browser#debug` opens headless session in the browser devtools frontend [#519]

### Changed

Expand Down
31 changes: 31 additions & 0 deletions lib/ferrum/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,24 @@ def version
VersionInfo.new(command("Browser.getVersion"))
end

#
# Opens headless session in the browser devtools frontend.
#
# @return [void]
#
# @since 0.16
#
def debug(bind = nil)
::Process.spawn(process.path, debug_url)

bind ||= binding
if bind.respond_to?(:pry)
Pry.start(bind)
else
bind.irb
end
end

private

def start
Expand All @@ -262,5 +280,18 @@ def start
raise
end
end

def debug_url
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 build_remote_debug_url(path:)
"http://#{process.host}:#{process.port}#{path}"
end
end
end

0 comments on commit dad31db

Please sign in to comment.