Skip to content
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
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