Class: Xvfb

Inherits:
Object
  • Object
show all
Defined in:
lib/xvfb.rb,
lib/xvfb/cli_util.rb,
lib/xvfb/video/video_recorder.rb

Overview

A class incapsulating the creation and usage of a xvfb X server

Prerequisites

  • X Window System

  • Xvfb

Usage

require 'rubygems'
require 'xvfb'
require 'selenium-webdriver'

xvfb = Xvfb.new
xvfb.start

driver = Selenium::WebDriver.for :firefox
driver.navigate.to 'http://google.com'
puts driver.title

xvfb.destroy

– TODO test that reuse actually works with an existing xvfb session ++

Defined Under Namespace

Classes: CliUtil, Exception, VideoRecorder

Constant Summary collapse

DEFAULT_DISPLAY_NUMBER =
99
MAX_DISPLAY_NUMBER =
10_000
DEFAULT_DISPLAY_DIMENSIONS =
'1280x1024x24'
DEFAULT_XVFB_LAUNCH_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Xvfb

Creates a new xvfb server, but does NOT switch to it immediately. Call #start for that

List of available options:

  • display (default 99) - what display number to listen to;

  • reuse (default true) - if given display server already exists, should we use it or try another?

  • autopick (default true if display number isn’t explicitly set) - if Xvfb should automatically pick a display, or fail if the given one is not available.

  • dimensions (default 1280x1024x24) - display dimensions and depth. Not all combinations are possible, refer to man Xvfb.

  • destroy_at_exit - if a display is started but not stopped, should it be destroyed when the script finishes? (default true unless reuse is true and a server is already running)

  • xvfb_launch_timeout - how long should we wait for Xvfb to open a display, before assuming that it is frozen (in seconds, default is 10)

  • video - options to be passed to the ffmpeg video recorder. See Xvfb::VideoRecorder#initialize for

  • browser - options to be passed to the browser chromium. See Xvfb::Browser#initialize for documentation



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/xvfb.rb', line 64

def initialize(options = {})
  CliUtil.ensure_application_exists!('Xvfb', 'Xvfb not found on your system')

  @display = options.fetch(:display, DEFAULT_DISPLAY_NUMBER).to_i
  @xvfb_launch_timeout = options.fetch(:xvfb_launch_timeout, DEFAULT_XVFB_LAUNCH_TIMEOUT).to_i
  @autopick_display = options.fetch(:autopick, !options.key?(:display))
  @reuse_display = options.fetch(:reuse, true)
  @dimensions = options.fetch(:dimensions, DEFAULT_DISPLAY_DIMENSIONS)
  @video_capture_options = options.fetch(:video, {})

  already_running = xvfb_running? rescue false
  @destroy_at_exit = options.fetch(:destroy_at_exit, !(@reuse_display && already_running))

  @pid = nil # the pid of the running Xvfb process

  # FIXME Xvfb launch should not happen inside the constructor
  attach_xvfb
end

Instance Attribute Details

#dimensionsObject (readonly)

The display dimensions



42
43
44
# File 'lib/xvfb.rb', line 42

def dimensions
  @dimensions
end

#displayObject (readonly)

The display number



39
40
41
# File 'lib/xvfb.rb', line 39

def display
  @display
end

#xvfb_launch_timeoutObject (readonly)

Returns the value of attribute xvfb_launch_timeout.



43
44
45
# File 'lib/xvfb.rb', line 43

def xvfb_launch_timeout
  @xvfb_launch_timeout
end

Instance Method Details

#destroyObject

Switches back from the xvfb server and terminates the xvfb session while waiting for Xvfb process to terminate.



97
98
99
100
# File 'lib/xvfb.rb', line 97

def destroy
  stop
  CliUtil.kill_process(pid_filename, preserve_pid_file: true, wait: true)
end

#destroy_at_exit?Boolean

Whether the xvfb display will be destroyed when the script finishes.

Returns:

  • (Boolean)


117
118
119
# File 'lib/xvfb.rb', line 117

def destroy_at_exit?
  @destroy_at_exit
end

#destroy_syncObject

Deprecated. Same as destroy. Kept for backward compatibility in June 2015.



105
106
107
# File 'lib/xvfb.rb', line 105

def destroy_sync
  destroy
end

#destroy_without_syncObject

Same as the old destroy function – doesn’t wait for Xvfb to die. Can cause zombies: stackoverflow.com/a/31003621/1651458



111
112
113
114
# File 'lib/xvfb.rb', line 111

def destroy_without_sync
  stop
  CliUtil.kill_process(pid_filename, preserve_pid_file: true)
end

#startObject

Switches to the xvfb server



84
85
86
87
88
# File 'lib/xvfb.rb', line 84

def start
  @old_display = ENV['DISPLAY']
  ENV['DISPLAY'] = ":#{display}"
  hook_at_exit
end

#stopObject

Switches back from the xvfb server



91
92
93
# File 'lib/xvfb.rb', line 91

def stop
  ENV['DISPLAY'] = @old_display
end

#videoObject



121
122
123
# File 'lib/xvfb.rb', line 121

def video
  @video_recorder ||= VideoRecorder.new(display, dimensions, @video_capture_options)
end