• Stars
    star
    960
  • Rank 47,640 (Top 1.0 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 14 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Create a virtual X screen from Ruby, record videos and take screenshots.

Headless Travis CI status

Buy Me a Coffee at ko-fi.com

Headless is the Ruby interface for Xvfb. It allows you to create a headless display straight from Ruby code, hiding the low-level action. It can also capture images and video from the virtual framebuffer. For example, you can record screenshots and screencasts of your failing integration specs.

I created it so I can run Selenium tests in Cucumber without any shell scripting. Even more, you can go headless only when you run tests against Selenium. Other possible uses include pdf generation with wkhtmltopdf, or screenshotting.

Documentation is available at rubydoc.info

Changelog

Note: Headless will NOT hide most applications on OS X. Here is a detailed explanation

Installation

On Debian/Ubuntu:

sudo apt-get install xvfb
gem install headless

Usage

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

Cucumber

Running cucumber headless is now as simple as adding a before and after hook in features/support/env.rb:

# change the condition to fit your setup
if Capybara.current_driver == :selenium
  require 'headless'

  headless = Headless.new
  headless.start
end

Running tests in parallel

If you have multiple threads running acceptance tests in parallel, you want to spawn Headless before forking, and then reuse that instance with destroy_at_exit: false. You can even spawn a Headless instance in one ruby script, and then reuse the same instance in other scripts by specifying the same display number and reuse: true.

# spawn_headless.rb
Headless.new(display: 100, destroy_at_exit: false).start

# test_suite_that_could_be_ran_multiple_times.rb
Headless.new(display: 100, reuse: true, destroy_at_exit: false).start

# reap_headless.rb
headless = Headless.new(display: 100, reuse: true)
headless.destroy

# kill_headless_without_waiting.rb
headless = Headless.new
headless.destroy_without_sync

There's also a different approach that creates a new virtual display for every parallel test process - see this implementation by @rosskevin.

Cucumber with wkhtmltopdf

Note: this is true for other programs which may use headless at the same time as cucumber is running

When wkhtmltopdf is using Headless, and cucumber is invoking a block of code which uses a headless session, make sure to override the default display of cucumber to retain browser focus. Assuming wkhtmltopdf is using the default display of 99, make sure to set the display to a value != 99 in features/support/env.rb file. This may be the cause of Connection refused - connect(2) (Errno::ECONNREFUSED).

headless = Headless.new(:display => '100')
headless.start

Capturing video

Video is captured using ffmpeg. You can install it on Debian/Ubuntu via sudo apt-get install ffmpeg or on OS X via brew install ffmpeg. You can capture video continuously or capture scenarios separately. Here is typical use case:

require 'headless'

headless = Headless.new
headless.start

Before do
  headless.video.start_capture
end

After do |scenario|
  if scenario.failed?
    headless.video.stop_and_save("/tmp/#{BUILD_ID}/#{scenario.name.split.join("_")}.mov")
  else
    headless.video.stop_and_discard
  end
end

Video options

When initiating Headless you may pass a hash with video options.

headless = Headless.new(:video => { :frame_rate => 12, :codec => 'libx264' })

Available options:

  • :codec - codec to be used by ffmpeg
  • :frame_rate - frame rate of video capture
  • :provider - ffmpeg provider - either :libav (default) or :ffmpeg
  • :provider_binary_path - Explicit path to avconv or ffmpeg. Only required when the binary cannot be discovered on the system $PATH.
  • :pidfile_path - path to ffmpeg pid file, default: "/tmp/.headless_ffmpeg#{@display}.pid"
  • :tmpfile_path - path to tmp video file, default: "/tmp/.headless_ffmpeg#{@display}.mov"
  • :log_file_path - ffmpeg log file, default: "/dev/null"
  • :extra - array of extra ffmpeg options, default: []

Taking screenshots

Call headless.take_screenshot to take a screenshot. It needs two arguments:

  • file_path - path where the image should be stored
  • options - options, that can be: :using - :imagemagick or :xwd, :imagemagick is default, if :imagemagick is used, image format is determined by file_path extension

Screenshots can be taken by either using import (part of imagemagick library) or xwd utility.

import captures a screenshot and saves it in the format of the specified file. It is convenient but not too fast as it has to do the encoding synchronously.

xwd will capture a screenshot very fast and store it in its own format, which can then be converted to one of other picture formats using, for example, netpbm utilities - xwdtopnm <xwd_file> | pnmtopng > capture.png.

To install the necessary libraries on ubuntu:

import - run sudo apt-get install imagemagick xwd - run sudo apt-get install X11-apps and if you are going to use netpbm utilities for image conversion - sudo apt-get install netpbm

Troubleshooting

/tmp/.X11-unix is missing

Xvfb requires this directory to exist. It cannot be created automatically, because the directory must be owned by the root user. (You will never get this error if running as root - for example, in a Docker container.)

On macOS, the directory will be created when you run XQuartz.app. But since /tmp is cleared on reboot, you will need to open XQuartz.app after a reboot before running Xvfb. (You don't need to leave it running.)

To create this directory manually, on either macOS or Linux:

mkdir /tmp/.X11-unix
sudo chmod 1777 /tmp/.X11-unix
sudo chown root /tmp/.X11-unix/

Note that you may need to run these commands after every reboot, too.

Display socket is taken but lock file is missing

This means that there is an X server that is taking up the chosen display number, but its lock file is missing. This is an exceptional situation. Please stop the server process manually (pkill Xvfb) and open an issue.

Video not recording

If video is not recording, and there are no visible exceptions, try passing the following option to Headless to figure out the reason: Headless.new(video: {log_file_path: STDERR}). In particular, there are some issues with the version of avconv packaged with Ubuntu 12.04 - an outdated release, but still in use on Travis.

##Contributors


Β© 2011-2015 Leonid Shevtsov, released under the MIT license

More Repositories

1

ClickableUrls_SublimeText

Underlines URLs in Sublime Text, and lets you open them with a keystroke.
Python
155
star
2

unobtrusive_flash

Turnkey Flash messages for your Rails app
Ruby
147
star
3

SearchInProject_SublimeText

Use ag, ack, grep and git grep directly from Sublime Text 2 and 3
Python
124
star
4

s3-browser-upload-demo

Demonstration of direct S3 upload from browser
JavaScript
85
star
5

split_tests

Utility to split test files into parallel CI containers
Go
48
star
6

git-timesheet

A script to create a timesheet from a git log
Ruby
47
star
7

liqpay

This Ruby gem implements the LiqPAY billing system API.
Ruby
35
star
8

vydumschik

A fake data generation library in Russian
Ruby
32
star
9

monkeysort

JavaScript
32
star
10

ua-cities

A structured list of Ukrainian cities
Ruby
32
star
11

send_to_omnifocus.crx

Send to OmniFocus for Google Chrome
JavaScript
25
star
12

pivotal_shell

An (unmaintained) command-line client for Pivotal Tracker
Ruby
21
star
13

railsbug

(Discontinued) A Firebug extension for Ruby on Rails debugging
JavaScript
18
star
14

vimrun-silent

A vimrun.exe that does not spawn a visible command line window
C
12
star
15

rails_db_dump

Rake task for foolproof database dumps
Ruby
11
star
16

moves_geotag

Geotag photos using tracks from Moves.app
Shell
9
star
17

gvimfullscreen_win32

Plugin to allow full screen use in gvim on Windows
C
8
star
18

launchy

This is my fork of Launchy, the open-source application launcher. It contains some Linux-specific fixes; see commit log for more info
C++
6
star
19

kalman

JavaScript
6
star
20

vkontakte-friends-parser

Parses your Vkontakte.ru friends list, finds friends of your friends, lists them ordered by number of common friends
Ruby
5
star
21

privatbank2csv

Π‘ΠΊΡ€ΠΈΠΏΡ‚ для прСобразования выписок ΠŸΡ€ΠΈΠ²Π°Ρ‚24 Π² CSV
Ruby
5
star
22

tleds

Keyboard lights network indicator for Linux
C
5
star
23

jenkins.php

A PHP-on-Apache error log analyzer
PHP
4
star
24

rails-templates

Rails template - the way it should be.
Ruby
4
star
25

page_visit_tracker

A simple page visit tracker for Rails
Ruby
4
star
26

cuoco

(Deprecated in favor of using Knife) Use Capistrano for server provisioning and configuration management, not only application deployment.
Ruby
3
star
27

chef-rails-helpers

A collection of definitions that I use when deploying Ruby on Rails ails apps
Ruby
3
star
28

proxytools

Some tools for working with proxies on Ruby
Ruby
3
star
29

rubygems-gui

A Sinatra-based interface for the gem command
3
star
30

math3d

A 3D math library implemented entirely in Ruby
Ruby
2
star
31

liqpay_demo

Demo for the liqpay gem
Ruby
2
star
32

rack_my_openid

Single-user OpenID provider implemented as a Rack (Sinatra) application.
Ruby
2
star
33

opengl-in-ruby

A little OpenGL demo in Ruby that actually works
Ruby
2
star
34

dotfiles

My dotfiles
Vim Script
2
star
35

GemLinks_SublimeText

Easily navigate Rubygems used by your project
Python
2
star
36

telegold

πŸ†πŸ“’ A Goldmark renderer for Telegram
Go
2
star
37

nenka

A VPN for friends and family
Ruby
2
star
38

input_hint.js

Show placeholder text when an input is empty
JavaScript
2
star
39

glamour_math

Cross-platform 3D linear algebra for C++
C++
2
star
40

ambient_theme.vim

Automatically switch Vim background based on ambient light.
C++
2
star
41

transit_in_ua

Ruby
2
star
42

uni-programming

Dnipropetrovsk National University C++ course labs.
C++
2
star
43

knife-edit-encrypted-data-bag

Edit encrypted data bags with convenience. TODO: merge with chef core codebase
Ruby
2
star
44

clojure-syntax-check

A syntax checker for Clojure
Clojure
2
star
45

behaving_rodent

A collection of behavioral helpers for Capybara the browser emulator
Ruby
2
star
46

Dash_Sublime_Text_Plugin_API

Clojure
2
star
47

send_to_omnifocus_from_pivotal_tracker

Exactly what it says on the label.
JavaScript
2
star
48

wordpress-remote-installer

Download and install WordPress through a simple web interface. You only need to manually upload a single small script to the server.
PHP
2
star
49

SublimeLinter-contrib-clojure-syntax-check

SublimeLinter plugin for clojure-syntax-check
Python
2
star
50

git-housekeeping

A script to check health of project folders
Ruby
1
star
51

ls_feedstats

A better feed statistics plugin for WordPress
PHP
1
star
52

formtastic-susy

More like Susy-ed Formtastic, but that just doesn't make sense.
1
star
53

google-reader-font-settings

Deprecated since Google Reader is now extinct
JavaScript
1
star
54

glew

Ruby SWIG bindings for GLEW
1
star
55

takeoff

My barebone Rails application
Ruby
1
star
56

ruby-dynamic-text-replacement

A hack toward a DTR library for use with Rails
Ruby
1
star
57

pys60stuff

Various scripts for Python on S60
1
star
58

tscheck-rb

Ruby
1
star
59

rtm-for-pys60

A client for Remember The Milk for Symbian S60v5 (Nokia XpressMusic 5800). Not finished and never will be.
Python
1
star
60

one-man-shortener

A personal url shortener and image store
Ruby
1
star
61

osx-tor-starter

A script to start and configure Tor
Shell
1
star
62

vagrant-mpich

Script to set up an MPICH cluster with Vagrant & Chef
Ruby
1
star
63

things-app-export

Script to export the local Things.app database into a JSON file
Ruby
1
star
64

cronograph

Collect, view and analyze cronjob execution logs.
Python
1
star
65

omnifocus-scripts

Ruby
1
star
66

luxon-date

TypeScript
1
star
67

homeblocker

DNS blocking for computer dads and moms
Go
1
star
68

WindowsPhoneSync_app

Python
1
star
69

habrahabr-comments-like-reddit

Habrahabr 'comments like reddit' mod
1
star
70

mozdebug.vim

A frontend debugger plugin for Vim
Vim Script
1
star