diff --git a/CHANGELOG.md b/CHANGELOG.md index 97ea75ec..31e0fd1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/ferrum/browser.rb b/lib/ferrum/browser.rb index 0d3950d7..4d0dc74a 100644 --- a/lib/ferrum/browser.rb +++ b/lib/ferrum/browser.rb @@ -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 @@ -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