Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
- `FERRUM_CLICK_WAIT` and `FERRUM_GOTO_WAIT`, will be removed in 0.22. Use `FERRUM_NAVIGATION_WAIT` instead

### Fixed
- A headful browser with `xvfb: true` sometimes started before Xvfb was ready and exited at once with
`ProcessTimeoutError`, Ferrum waits for the display now
- `page.command(method, wait:)` ran a subclass override of `command`, like Cuprite's, twice
- `evaluate` of a result nested about 100 levels deep raised an opaque `BrowserError` about Chrome's CBOR stack limit,
it raises `ResultTooDeepError` now, a `BrowserError` that says to return less or use `evaluate_handle`
Expand Down
24 changes: 22 additions & 2 deletions lib/ferrum/browser/xvfb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,27 @@ def initialize(options)

@screen_size = "#{options.window_size.join('x')}x24"
@display_id = (Time.now.to_f * 1000).to_i % 100_000_000
@timeout = options.process_timeout
end

#
# Spawns the Xvfb process on the configured display.
# Spawns the Xvfb process on the configured display and waits until it
# accepts connections, so a browser started right after finds the display.
#
# @return [void]
#
# @raise [Error]
# When Xvfb exits or isn't ready within `:process_timeout`.
#
def start
@pid = ::Process.spawn("#{@path} :#{display_id} -screen 0 #{screen_size}")
reader, writer = IO.pipe
@pid = ::Process.spawn(@path, ":#{display_id}", "-screen", "0", screen_size, "-displayfd", "1", out: writer)
::Process.detach(@pid)
writer.close
wait_for_display(reader)
ensure
reader&.close
writer&.close
end

#
Expand All @@ -52,6 +63,15 @@ def start
def to_env
{ "DISPLAY" => ":#{display_id}" }
end

private

def wait_for_display(io)
return if io.wait_readable(@timeout) && io.gets

Process::Killer.kill(@pid)
raise Error, "Xvfb exited or wasn't ready on display :#{display_id} within #{@timeout} seconds"
end
end
end
end
4 changes: 4 additions & 0 deletions sig/ferrum/browser/xvfb.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ module Ferrum
def start: () -> untyped

def to_env: () -> ::Hash[::String, ::String]

private

def wait_for_display: (::IO io) -> void
end
end
end
16 changes: 16 additions & 0 deletions spec/browser/xvfb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@
expect(process_alive?(pid)).to be(false) if pid
end
end

it "waits for an Xvfb that is slow to start before launching a headful browser" do
Dir.mktmpdir do |dir|
slow_xvfb = File.join(dir, "Xvfb")
File.write(slow_xvfb, "#!/bin/sh\nsleep 1\nexec #{Ferrum::Browser::Binary.find('Xvfb')} \"$@\"\n", perm: 0o755)
allow(Ferrum::Browser::Binary).to receive(:find).and_call_original
allow(Ferrum::Browser::Binary).to receive(:find).with("Xvfb").and_return(slow_xvfb)

browser = Ferrum::Browser.new(xvfb: true, headless: false)
browser.go_to(base_url)

expect(browser.body).to include("Hello world!")
ensure
browser&.quit
end
end
end

def process_alive?(pid)
Expand Down
Loading