Thursday, March 29, 2012

Headless gem


The headless gem is a ruby wrapper for Xvfb, the virtual frame buffer.Virtual frame buffers are used to run graphical software on a “headless”, i.e. display-less, server.Headless gem makes it possible to create and release frame buffers straight from Ruby code. 

This gem makes it easy to run graphical applications (such as real web browsers) on a headless Linux machine. 

Command to install this gem:

gem install headless

An Example:

require 'watir-webdriver'
require 'headless'
headless = Headless.new
headless.start
b = Watir::Browser.start 'www.google.com'
puts b.title
b.close
headless.destroy

Finally, running WebDriver headless tests requires you to run a Selenium Server, which is additional overhead for your tests.


But why headless?


Whilst running automated browser tests in a headless form can speed things up, the main reason,  people wanting headless watir-web driver support is so that tests can be run on headless Linux machines, for example, a Jenkins Server.

Stop loading browser page

Sometimes the page is loading too slow, and we get a timeout error from watir-webdriver. Like in the below case:

require 'watir-webdriver'
browser = Watir::Browser.new :chrome
begin
  browser.goto("any url for a slow site")
 # Raise Timeout exception in 60 sec
rescue
  browser.send_keys(:escape) # => Raise another Timeout exception
end

Browser just refuses to respond to any othere commands before it finished loading the page. AS in this case the escape command is never executed until page loading is done.

This can be solved in below way:

require 'watir-webdriver'

client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 60 
@browser = Watir::Browser.new :chrome, :http_client => client

begin
  @browser.goto mySite
rescue => e
  puts "Browser timed out: #{e}"
end

next_command

I hope this helps...