Skip to content

Commit

Permalink
feat: Support for headless=new
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed Sep 14, 2023
1 parent b46d456 commit 594dd17
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 112 deletions.
4 changes: 4 additions & 0 deletions lib/ferrum/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ def version
VersionInfo.new(command("Browser.getVersion"))
end

def headless_new?
process&.command&.headless_new?
end

private

def start
Expand Down
3 changes: 2 additions & 1 deletion lib/ferrum/browser/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def raise_browser_error(error)
case error["message"]
# Node has disappeared while we were trying to get it
when "No node with given id found",
"Could not find node with given id"
"Could not find node with given id",
"Inspected target navigated or closed"
raise NodeNotFoundError, error
# Context is lost, page is reloading
when "Cannot find context with specified id"
Expand Down
4 changes: 4 additions & 0 deletions lib/ferrum/browser/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def xvfb?
!!options.xvfb
end

def headless_new?
@flags["headless"] == "new"
end

def to_a
[path] + @flags.map { |k, v| v.nil? ? "--#{k}" : "--#{k}=#{v}" }
end
Expand Down
4 changes: 3 additions & 1 deletion lib/ferrum/browser/options/chrome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class Chrome < Base
"keep-alive-for-test" => nil,
"disable-popup-blocking" => nil,
"disable-extensions" => nil,
"disable-component-extensions-with-background-pages" => nil,
"disable-hang-monitor" => nil,
"disable-features" => "site-per-process,TranslateUI",
"disable-features" => "site-per-process,IsolateOrigins,TranslateUI",
"disable-translate" => nil,
"disable-background-networking" => nil,
"enable-features" => "NetworkService,NetworkServiceInProcess",
Expand All @@ -32,6 +33,7 @@ class Chrome < Base
"disable-ipc-flooding-protection" => nil,
"disable-prompt-on-repost" => nil,
"disable-renderer-backgrounding" => nil,
"disable-site-isolation-trials" => nil,
"force-color-profile" => "srgb",
"metrics-recording-only" => nil,
"safebrowsing-disable-auto-update" => nil,
Expand Down
3 changes: 2 additions & 1 deletion lib/ferrum/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ def prepare_page
resize(width: width, height: height)

response = command("Page.getNavigationHistory")
return unless response.dig("entries", 0, "transitionType") != "typed"
transition_type = response.dig("entries", 0, "transitionType")
return if transition_type == "auto_toplevel"

# If we create page by clicking links, submitting forms and so on it
# opens a new window for which `frameStoppedLoading` event never
Expand Down
23 changes: 21 additions & 2 deletions spec/browser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,15 @@
proxy: { host: proxy.host, port: proxy.port, user: "u1", password: "p1" }
)

browser.go_to("https://example.com")
if browser.headless_new?
expect { browser.go_to("https://example.com") }.to raise_error(
Ferrum::StatusError,
"Request to https://example.com failed (net::ERR_HTTP_RESPONSE_CODE_FAILURE)"
)
else
browser.go_to("https://example.com")
end

expect(browser.network.status).to eq(407)
ensure
browser&.quit
Expand Down Expand Up @@ -234,6 +242,9 @@
let(:save_path) { "/tmp/ferrum" }

it "saves an attachment" do
# Also https://github.com/puppeteer/puppeteer/issues/10161
skip "https://bugs.chromium.org/p/chromium/issues/detail?id=1444729" if browser.headless_new?

browser.go_to("/#{filename}")

expect(File.exist?("#{save_path}/#{filename}")).to be true
Expand Down Expand Up @@ -531,7 +542,15 @@
it "fails with wrong password" do
page = browser.create_page(proxy: { host: proxy.host, port: proxy.port,
user: options[:user], password: "$$" })
page.go_to("https://example.com")

if browser.headless_new?
expect { page.go_to("https://example.com") }.to raise_error(
Ferrum::StatusError,
"Request to https://example.com failed (net::ERR_HTTP_RESPONSE_CODE_FAILURE)"
)
else
page.go_to("https://example.com")
end

expect(page.network.status).to eq(407)
end
Expand Down
Loading

0 comments on commit 594dd17

Please sign in to comment.