Object
A class incapsulating the creation and usage of a headless X server
X Window System
Block mode:
require 'rubygems' require 'headless' require 'selenium-webdriver' Headless.ly do driver = Selenium::WebDriver.for :firefox driver.navigate.to 'http://google.com' puts driver.title end
Object mode:
require 'rubygems' require 'headless' require 'selenium-webdriver' headless = Headless.new headless.start driver = Selenium::WebDriver.for :firefox driver.navigate.to 'http://google.com' puts driver.title headless.destroy
Creates a new headless 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 is display number isn't explicitly set) - if Headless 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 (default true) - if a display is started but not stopped, should it be destroyed when the script finishes?
# File lib/headless.rb, line 65 def initialize(options = {}) CliUtil.ensure_application_exists!('Xvfb', 'Xvfb not found on your system') @display = options.fetch(:display, DEFAULT_DISPLAY_NUMBER).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, {}) @destroy_at_exit = options.fetch(:destroy_at_exit, true) attach_xvfb end
Block syntax:
Headless.run do # perform operations in headless mode end
See new for options
# File lib/headless.rb, line 102 def self.run(options={}, &block) headless = Headless.new(options) headless.start yield headless ensure headless.destroy end
Switches back from the headless server and terminates the headless session
# File lib/headless.rb, line 91 def destroy stop CliUtil.kill_process(pid_filename) end
Switches to the headless server
# File lib/headless.rb, line 79 def start @old_display = ENV['DISPLAY'] ENV['DISPLAY'] = ":#{display}" hook_at_exit end
Switches back from the headless server
# File lib/headless.rb, line 86 def stop ENV['DISPLAY'] = @old_display end
# File lib/headless.rb, line 115 def take_screenshot(file_path) CliUtil.ensure_application_exists!('import', "imagemagick not found on your system. Please install it using sudo apt-get install imagemagick") system "#{CliUtil.path_to('import')} -display localhost:#{display} -window root #{file_path}" end
Generated with the Darkfish Rdoc Generator 2.