The other thing about HTMLUnit is the lack of screenshot capability, as it’s purely headless, there’s no way to capture a screenshot when something goes wrong, so you’ll find yourself re-running failed tests using a different browser, which may or may not reproduce your problem. Not ideal!
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 I see people wanting headless watir-webdriver support is so that tests can be run on headless Linux machines, for example, a Jenkins Server. I’ve found running tests in parallel speeds things up more than headless execution can, so it’s mainly headless server support most people are after.
Enter the headless gem.
The headless gem is a ruby wrapper for Xvfb that makes it super easy to run graphical applications (such as real web browsers) on a headless machine. This gem is perfect for running headless watir-webdriver tests using a real browser.
How? It’s simple.
require 'watir-webdriver' require 'headless' headless = Headless.new headless.start b = Watir::Browser.start 'www.google.com' puts b.title b.close headless.destroy
You’ll need Xvfb installed, which is as easy as:
sudo apt-get install xvfb
Or if you’re using cucumber like me, you just need the following code in your env.rb file:
if ENV['HEADLESS'] require 'headless' headless = Headless.new headless.start at_exit do headless.destroy end end
This then means you can still launch and use Firefox (or Chrome) on a headless machine.
The best thing about it though is that Watir-WebDriver’s inbuilt screenshots still work perfectly, so it captures a screenshot of the actual page even though it has no display. How awesome is that!
Stay tuned as I’ll be writing a post soon about how to use an EC2 instance to run your headless watir-webdriver tests in the cloud, for free!