Home > Articles > Open Source > Ajax & JavaScript

Like this article? We recommend 

Like this article? We recommend

Going Headless

Going Headless

A headless web browser doesn't have a GUI, hence the name "headless." Think of this as a somewhat-virtual web browser. It can display web pages, and most importantly run our test suite, but it can do it all from the terminal window.

Several great headless web browsers are out there, but my personal favorite is PhantomJS. It's very easy to install and works greatβ€”two of the main bullet points I want in a product like this.

After installing PhantomJS, we need to install the Poltergeist gem, which contains the driver that will let us hook our tests into the PhantomJS headless browser:

# Gemfile
group :development, :test do
  gem "konacha"
  gem "poltergeist"
end

All that's left to do is configure Konacha to use Poltergeist and PhantomJS. In the config/initializers directory, create a new file called konacha.rb:

# config/initializers/konacha.rb
if defined?(Konacha)
  require 'capybara/poltergeist'
  Konacha.configure do |config|
    config.driver = :poltergeist
  end
end

In this file, we're requiring the driver that Poltergeist gives us (require 'capybara/poltergeist') and telling Konacha to use that driver (config.driver = :poltergeist).

When we run rake konacha:run this time from the terminal window, the test suite should run without Firefox appearing. Perfect!

If you want to run individual specs from the command line, Konacha lets you do it like this:

> rake konacha:run SPEC=greeter_spec

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.