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:
[codesyntax lang=”bash”]
yum install Xvfb libXfont Xorg
[/codesyntax]
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:
[codesyntax lang=”bash”]
chmod +x /etc/init.d/xvfb
chkconfig –add xvfbd
[/codesyntax]
Start the service:
[codesyntax lang=”bash”]
service xvfb start
[/codesyntax]
Firefox
Install Firefox:
[codesyntax lang=”bash”]
yum install firefox
[/codesyntax]
Selenium WebDriver for Ruby
Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation.
Install the gem:
[codesyntax lang=”bash”]
gem install selenium-webdriver
[/codesyntax]
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:
[codesyntax lang=”bash”]
Xvfb :99 &
[/codesyntax]
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:
[codesyntax lang=”bash”]
export DISPLAY=:99
[/codesyntax]
Now we are ready to run tests against web browser:
[codesyntax lang=”bash”]
ruby mytest.rb
[/codesyntax]
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: [codesyntax lang=”bash”] ruby test1.rb [/codesyntax] The output would be similar to this: [codesyntax lang=”bash”] Page title is Stack Overflow [/codesyntax] 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: [codesyntax lang=”bash”] gem install capybara [/codesyntax] Write a test script:
Run the test:
[codesyntax lang=”bash”]
ruby test2.rb
[/codesyntax]
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:
[codesyntax lang=”bash”]
gem install headless
[/codesyntax]
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