Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for headless=new #379

Merged
merged 4 commits into from
Sep 14, 2023
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ Ferrum::Browser.new(options)
```

* options `Hash`
* `:headless` (Boolean) - Set browser as headless or not, `true` by default.
* `:headless` (String | Boolean) - Set browser as headless or not, `true` by default. You can set `"new"` to support
[new headless mode](https://developer.chrome.com/articles/new-headless/).
* `:xvfb` (Boolean) - Run browser in a virtual framebuffer, `false` by default.
* `:window_size` (Array) - The dimensions of the browser window in which to
test, expressed as a 2-element array, e.g. [1024, 768]. Default: [1024, 768]
Expand Down
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
11 changes: 9 additions & 2 deletions 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 Expand Up @@ -74,7 +76,12 @@ def merge_required(flags, options, user_data_dir)
end

def merge_default(flags, options)
defaults = except("headless", "disable-gpu") unless options.headless
defaults = case options.headless
when false
except("headless", "disable-gpu")
when "new"
except("headless").merge("headless" => "new")
end

defaults ||= DEFAULT_OPTIONS
defaults.merge(flags)
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