Class: Ferrum::Browser::Xvfb

Inherits:
Object
  • Object
show all
Defined in:
lib/ferrum/browser/xvfb.rb

Constant Summary collapse

NOT_FOUND =
"Could not find an executable for the Xvfb. Try to install " \
"it with your package manager".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Xvfb

Returns a new instance of Xvfb.

Raises:

  • (Cliver::Dependency::NotFound)


19
20
21
22
23
24
25
# File 'lib/ferrum/browser/xvfb.rb', line 19

def initialize(options)
  @path = self.class.xvfb_path
  raise Cliver::Dependency::NotFound.new(NOT_FOUND) unless @path

  @screen_size = options.fetch(:window_size, [1024, 768]).join("x") + "x24"
  @display_id = (Time.now.to_f * 1000).to_i % 100_000_000
end

Instance Attribute Details

#display_idObject (readonly)

Returns the value of attribute display_id.



17
18
19
# File 'lib/ferrum/browser/xvfb.rb', line 17

def display_id
  @display_id
end

#pidObject (readonly)

Returns the value of attribute pid.



17
18
19
# File 'lib/ferrum/browser/xvfb.rb', line 17

def pid
  @pid
end

#screen_sizeObject (readonly)

Returns the value of attribute screen_size.



17
18
19
# File 'lib/ferrum/browser/xvfb.rb', line 17

def screen_size
  @screen_size
end

Class Method Details

.start(*args) ⇒ Object



9
10
11
# File 'lib/ferrum/browser/xvfb.rb', line 9

def self.start(*args)
  new(*args).tap(&:start)
end

.xvfb_pathObject



13
14
15
# File 'lib/ferrum/browser/xvfb.rb', line 13

def self.xvfb_path
  Cliver.detect("Xvfb")
end

Instance Method Details

#startObject



27
28
29
30
# File 'lib/ferrum/browser/xvfb.rb', line 27

def start
  @pid = ::Process.spawn("#{@path} :#{display_id} -screen 0 #{screen_size}")
  ::Process.detach(@pid)
end

#to_envObject



32
33
34
# File 'lib/ferrum/browser/xvfb.rb', line 32

def to_env
  { "DISPLAY" => ":#{display_id}" }
end