Running Selenium tests with Ruby on Centos

This posts shows how to run Selenium tests on Centos. Tests written on Ruby will use Selenium WebDriver API to run the browser directly. We don’t need to install Selenium Server because selenium tests will be executed locally against Firefox browser on the same server (Centos).

Xvfb

We will use Xvfb X11 as a display server.

Xvfb (X virtual framebuffer) is a display server implementing the X11 display server protocol. In contrast to other display servers Xvfb performs all graphical operations in memory without showing any screen output.

 

Install Xvfb

Install required packages:

yum install Xvfb libXfont Xorg

 

Start Xvfb automatically:

In order to start Xvfb automatically after server starts we need an init script ‘/etc/init.d/xvfb’:

Find the init script on gist.github.com.

Add script to start automatically:

chmod +x /etc/init.d/xvfb
chkconfig --add xvfbd

Start the service:

service xvfb start

 

 

Firefox

Install Firefox:

yum install firefox

 

Selenium WebDriver for Ruby

Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation.

 

Install the gem:

gem install selenium-webdriver

 

Run Selenium tests

We have two options to run tests:

Option 1:
– Start Xvfb on a specific display port and background the process (or run Xvfb as a service in Linux)
– Use the display port in terminal session
– Run the test

Option 2:
– Use the Headless gem
– Use Headless in test code: headless = Headless.new, headless.start, destroy
– Run the test

Run tests using Xvfb and exporting display

Before running Selenium tests we need to launch Xvfb:

Xvfb :99 &

This will run the Xvfb process in background.

Note! If you installed Xvfb service to start automatically then you don’t need to run it again.

 

Tell the terminal session to use the display port:

export DISPLAY=:99

 

Now we are ready to run tests against web browser:

ruby mytest.rb

 

If you have problems running Selenium tests, first, check if Selenium version is compatible with installed Firefox version: http://selenium.googlecode.com/git/rb/CHANGES.

 

Selenium WebDriver

Write a simple test “test1.rb”:

 




  It opens the site and outputs the page title.     Run the script:

ruby test1.rb  
    The output would be similar to this:
Page title is Stack Overflow
            Don’t forget to call start Xvfb and “export DISPLAY=:99 ” before running tests.            

Capybara

Capybara helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and comes with Rack::Test and Selenium support built in.   Install Capybara gem:

gem install capybara  
  Write a test script:    


 

Run the test:

ruby test2.rb

 

Run tests using the Headless gem

Headless is a Ruby interface for Xvfb. It allows you to create a headless display straight from Ruby code, hiding some low-level action. It can also capture images and video from the virtual framebuffer.

Note! You don’t need to call “export DISPLAY=:99” before running tests using Headless gem.

 

Install the gem:

gem install headless

A Ruby  test script:



Rspec

You can write tests using RSpec testing framework. Read this post about creating a rake task that executes rspec tests.

 

 

Useful links

* Examples of RSpec tests using Selenium webdriver and Headless

* How To Run Your Tests Headlessly