Skip to content

Commit dad31db

Browse files
authored
feat: Add Ferrum::Browser#debug (#519)
1 parent d0a3f43 commit dad31db

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- `Ferrum::Mouse#scroll_by` to be able to scroll by, as alternative to `scroll_to` [#514]
99
- `Ferrum::Network::Exchange#unknown` determines if the exchange is in an unknown state, meaning that browser might not return info about it [#426]
1010
- `Ferrum::Network::Exchange#loader_id` returns loader id [#426]
11+
- `Ferrum::Browser#debug` opens headless session in the browser devtools frontend [#519]
1112

1213
### Changed
1314

lib/ferrum/browser.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,24 @@ def version
245245
VersionInfo.new(command("Browser.getVersion"))
246246
end
247247

248+
#
249+
# Opens headless session in the browser devtools frontend.
250+
#
251+
# @return [void]
252+
#
253+
# @since 0.16
254+
#
255+
def debug(bind = nil)
256+
::Process.spawn(process.path, debug_url)
257+
258+
bind ||= binding
259+
if bind.respond_to?(:pry)
260+
Pry.start(bind)
261+
else
262+
bind.irb
263+
end
264+
end
265+
248266
private
249267

250268
def start
@@ -262,5 +280,18 @@ def start
262280
raise
263281
end
264282
end
283+
284+
def debug_url
285+
response = JSON.parse(Net::HTTP.get(URI(build_remote_debug_url(path: "/json"))))
286+
287+
devtools_frontend_path = response[0]&.[]("devtoolsFrontendUrl")
288+
raise "Could not generate debug url for remote debugging session" unless devtools_frontend_path
289+
290+
build_remote_debug_url(path: devtools_frontend_path)
291+
end
292+
293+
def build_remote_debug_url(path:)
294+
"http://#{process.host}:#{process.port}#{path}"
295+
end
265296
end
266297
end

0 commit comments

Comments
 (0)