Headless selenium testing in PHP and Ruby on Ubuntu

In this tutorial I'll outline two ways I have been working with Selenium in PHP and Ruby to do headless testing and screenshots on Ubuntu. These guides were done using Ubuntu-Server 12.04 LTS 64bit.

Headless Selenium Ruby Version

# update system packages
apt-get update
apt-get upgrade

# install ruby and make
apt-get install ruby1.9.3 make -y

# install xvfb
apt-get install xvfb -y

# install ruby gems
gem install headless
gem install selenium-webdriver

# install chrome
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
apt-get update
apt-get install google-chrome-stable

# install chrome driver
wget http://chromedriver.googlecode.com/files/chromedriver_linux64_23.0.1240.0.zip
apt-get install unzip
unzip chromedriver_linux64_23.0.1240.0.zip
cp chromedriver /usr/local/bin

Here is a simple ruby test script:

#!/usr/bin/env ruby

require 'headless'
require 'selenium-webdriver'

headless = Headless.new
headless.start

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

headless.destroy

Headless Selenium PHP Version

# update system packages
apt-get update
apt-get upgrade

# install ssh server
apt-get install openssh-server -y

# install java
apt-get install openjdk-7-jdk -y

# install chrome
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list
apt-get update
apt-get install google-chrome-stable

# install xvfb
apt-get install xvfb -y

# installing chrome driver
wget http://chromedriver.googlecode.com/files/chromedriver_linux64_23.0.1240.0.zip
apt-get install unzip
unzip chromedriver_linux64_23.0.1240.0.zip
cp chromedriver /usr/local/bin

# install git
apt-get install git

# cloning facebook web driver
git clone https://github.com/facebook/php-webdriver.git

# downloading selenium standalone
wget http://selenium.googlecode.com/files/selenium-server-standalone-2.25.0.jar

# install php
apt-get install php5-cli php5-curl -y

# start xvfb
Xvfb :99 -screen 0 1280x1024x24 -ac &

# start selenium standalone
export DISPLAY=:99 && java -jar selenium-server-standalone-2.25.0.jar &

The above installation works with a few lines of PHP using the FaceBook web driver.

#!/usr/bin/php
<?php
require_once('php-webdriver/__init__.php');
$wd_host = 'http://localhost:4444/wd/hub';
$web_driver = new WebDriver($wd_host);
$session = $web_driver->session('chrome');
$session->open('http://www.google.com');
?>

Executing the above script has the following Selenium output:

root@Eric-VM-Ubuntu:~# ./php-test.php
13:45:01.167 INFO - Executing: [new session: {browserName=chrome}] at URL: /session)
Started ChromeDriver
port=22717
version=23.0.1240.0
log=/root/chromedriver.log
Xlib:  extension "RANDR" missing on display ":99".
13:45:04.979 INFO - Done: /session
13:45:04.990 INFO - Executing: org.openqa.selenium.remote.server.handler.GetSessionCapabilities@68d6490d at URL: /session/1348335884949)
13:45:04.996 INFO - Done: /session/1348335884949
13:45:05.025 INFO - Executing: [get: http://www.google.com] at URL: /session/1348335884949/url)
13:45:05.764 INFO - Done: /session/1348335884949/url