Wednesday, March 23, 2011

Selenium in headless with Xvfb under RUBY

How to manage to get Selenium (browser => “firefox”) running under headless state (click here to know more on this) .
Now we need Xvfb to accomplish this dirty task.
For Ubuntu the package is fairly straight forward just type in the command

sudo aptitude install Xvfb

Under Fedora the same can be achieve using command .

sudo yum install xorg-x11-server-Xvfb

For more on Xvfb package  check your distribution provider .
Now start the Xvfb .

Xvfb -ac :99 &

The above command start the Xvfb in background with name Display=:99 .
Now to accomplish Selenium to work under headless state we need .
  1. Selenium RC Server [here] .
  2. Selenium Client Drivers .
more on both can be found here .
Start the Selenium RC Server .

java -jar selenium-server-standalone-2.0b2.jar &

The above command basically start the Selenium-RC-Server on  http://localhost:4444.
Now install selenium-webdriver [a.k.a Selenium Client Drivers] .
Note : Get your specific Selenium Client Driver based on language you prefer .

sudo gem install selenium-webdriver
Installation should work with any possible errors .
Now start your irb console .




require "rubygems"
require "selenium-webdriver"
s1 = Selenium::WebDriver::Remote.new :url => "http://localhost:4444/wb/pub"
s1.page_source
Selenium running under headless state .
Now there are couple of handy methods I want you to know before I say enough for this post 
Suppose you need to capture ScreenShoot of the visited page by Selenium .
To achieve this all you need to do is .

s1.save_screenshoot "example.png"
Hope it may help you. For more information click here

No comments:

Post a Comment