Home > Articles > Open Source > Ajax & JavaScript

  • Print
  • + Share This
From the author of 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
  • + Share This
  • 🔖 Save To Your Account