Skip to content

Commit 07b9354

Browse files
authored
Support for headless=new (#379)
* feat: Support for headless=new * chore: comments * fix: linux position spec * fix: README.md
1 parent b46d456 commit 07b9354

File tree

11 files changed

+176
-116
lines changed

11 files changed

+176
-116
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ Ferrum::Browser.new(options)
145145
```
146146

147147
* options `Hash`
148-
* `:headless` (Boolean) - Set browser as headless or not, `true` by default.
148+
* `:headless` (String | Boolean) - Set browser as headless or not, `true` by default. You can set `"new"` to support
149+
[new headless mode](https://developer.chrome.com/articles/new-headless/).
149150
* `:xvfb` (Boolean) - Run browser in a virtual framebuffer, `false` by default.
150151
* `:window_size` (Array) - The dimensions of the browser window in which to
151152
test, expressed as a 2-element array, e.g. [1024, 768]. Default: [1024, 768]

lib/ferrum/browser.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ def version
264264
VersionInfo.new(command("Browser.getVersion"))
265265
end
266266

267+
def headless_new?
268+
process&.command&.headless_new?
269+
end
270+
267271
private
268272

269273
def start

lib/ferrum/browser/client.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ def raise_browser_error(error)
8484
case error["message"]
8585
# Node has disappeared while we were trying to get it
8686
when "No node with given id found",
87-
"Could not find node with given id"
87+
"Could not find node with given id",
88+
"Inspected target navigated or closed"
8889
raise NodeNotFoundError, error
8990
# Context is lost, page is reloading
9091
when "Cannot find context with specified id"

lib/ferrum/browser/command.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def xvfb?
3939
!!options.xvfb
4040
end
4141

42+
def headless_new?
43+
@flags["headless"] == "new"
44+
end
45+
4246
def to_a
4347
[path] + @flags.map { |k, v| v.nil? ? "--#{k}" : "--#{k}=#{v}" }
4448
end

lib/ferrum/browser/options/chrome.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ class Chrome < Base
1919
"keep-alive-for-test" => nil,
2020
"disable-popup-blocking" => nil,
2121
"disable-extensions" => nil,
22+
"disable-component-extensions-with-background-pages" => nil,
2223
"disable-hang-monitor" => nil,
23-
"disable-features" => "site-per-process,TranslateUI",
24+
"disable-features" => "site-per-process,IsolateOrigins,TranslateUI",
2425
"disable-translate" => nil,
2526
"disable-background-networking" => nil,
2627
"enable-features" => "NetworkService,NetworkServiceInProcess",
@@ -32,6 +33,7 @@ class Chrome < Base
3233
"disable-ipc-flooding-protection" => nil,
3334
"disable-prompt-on-repost" => nil,
3435
"disable-renderer-backgrounding" => nil,
36+
"disable-site-isolation-trials" => nil,
3537
"force-color-profile" => "srgb",
3638
"metrics-recording-only" => nil,
3739
"safebrowsing-disable-auto-update" => nil,
@@ -74,7 +76,12 @@ def merge_required(flags, options, user_data_dir)
7476
end
7577

7678
def merge_default(flags, options)
77-
defaults = except("headless", "disable-gpu") unless options.headless
79+
defaults = case options.headless
80+
when false
81+
except("headless", "disable-gpu")
82+
when "new"
83+
except("headless").merge("headless" => "new")
84+
end
7885

7986
defaults ||= DEFAULT_OPTIONS
8087
defaults.merge(flags)

lib/ferrum/page.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ def prepare_page
390390
resize(width: width, height: height)
391391

392392
response = command("Page.getNavigationHistory")
393-
return unless response.dig("entries", 0, "transitionType") != "typed"
393+
transition_type = response.dig("entries", 0, "transitionType")
394+
return if transition_type == "auto_toplevel"
394395

395396
# If we create page by clicking links, submitting forms and so on it
396397
# opens a new window for which `frameStoppedLoading` event never

spec/browser_spec.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,15 @@
181181
proxy: { host: proxy.host, port: proxy.port, user: "u1", password: "p1" }
182182
)
183183

184-
browser.go_to("https://example.com")
184+
if browser.headless_new?
185+
expect { browser.go_to("https://example.com") }.to raise_error(
186+
Ferrum::StatusError,
187+
"Request to https://example.com failed (net::ERR_HTTP_RESPONSE_CODE_FAILURE)"
188+
)
189+
else
190+
browser.go_to("https://example.com")
191+
end
192+
185193
expect(browser.network.status).to eq(407)
186194
ensure
187195
browser&.quit
@@ -234,6 +242,9 @@
234242
let(:save_path) { "/tmp/ferrum" }
235243

236244
it "saves an attachment" do
245+
# Also https://github.com/puppeteer/puppeteer/issues/10161
246+
skip "https://bugs.chromium.org/p/chromium/issues/detail?id=1444729" if browser.headless_new?
247+
237248
browser.go_to("/#{filename}")
238249

239250
expect(File.exist?("#{save_path}/#{filename}")).to be true
@@ -531,7 +542,15 @@
531542
it "fails with wrong password" do
532543
page = browser.create_page(proxy: { host: proxy.host, port: proxy.port,
533544
user: options[:user], password: "$$" })
534-
page.go_to("https://example.com")
545+
546+
if browser.headless_new?
547+
expect { page.go_to("https://example.com") }.to raise_error(
548+
Ferrum::StatusError,
549+
"Request to https://example.com failed (net::ERR_HTTP_RESPONSE_CODE_FAILURE)"
550+
)
551+
else
552+
page.go_to("https://example.com")
553+
end
535554

536555
expect(page.network.status).to eq(407)
537556
end

0 commit comments

Comments
 (0)