Using Capybara-Webkit with Cucumber Without Rails or Rack

Using Capybara¬†with Capybara-Webkit and Cucumber allows behaviour driven testing that runs a browser proper in a similar manner to Selenium but headlessly. Most people using it will be using it as part of a Ruby based stack, likely with Rails, but we develop mostly in PHP. Fortunately its fairly easy to get Capybara and Capybara-Webkit going even though the majority of your code is not in Ruby. In the past the team has used Behat to write these kind of tests¬†(Mink is roughly equivalent to Capybara). Since both Behat and Cucumber use the Gherkin language to describe behaviour its fairly easy to drop one backend for another as required. I just prefer Ruby to PHP and will try and sneak its use in wherever possible, and really like Capybara’s easy syntax.

A good start is to clone the Standalone Cucumber repository over at Github. Then you’ll need to add Capybara-Webkit (gem 'capybara-webkit') to the Gemfile. Capybara-Webkit requires you to install the QT libraries in order that it can be compiled. The installation of QT varies depending on your OS but I on Mac OS X Lion it was a simple brew install qt and I was good to go. You then need to ensure all the gems are installed by running bundle install.

Next head over to the features/support/env.rb and make some additions so it resembles the below.

begin require 'rspec/expectations'; rescue LoadError; require 'spec/expectations'; end
require 'capybara'
require 'capybara/dsl'
require 'capybara/cucumber'
require 'capybara-webkit' # I added this

Capybara.default_driver = :webkit # And changed this!

Write your tests using Cucumber as usual then you should be good to go! Use bundle exec cucumber (because you want to use Bundler’s version of the Gems) and you are away. I have this aliased to “c”, so I can run tests in a couple of keystrokes.

Since it is loading a browser for every scenario, out of the box Capybara-Webkit can appear slow. In the next few weeks I’ll be experimenting with ways to make tests as fast as possible with a view to using Cucumber in a continuous integration setup.

2 responses to “Using Capybara-Webkit with Cucumber Without Rails or Rack

  1. How are you doing set up set up, initiating models and such
    We looked at a tech to run php in capybara but in the end opted for Behat
    Can’t remember what it was called now, but it was a pain in the arse to get working for everyone

    1. Hey,

      I haven’t got that far into it yet as this was for very basic functional testing (click here, does it do it, no – L & RD have a whole suite of this over Moodle in Capybara which they run in Jenkins every commit (!)) which didn’t need any dry setup or mocking. However, I suspect I’ll probably play about with this a bit.

Leave a Reply

Your email address will not be published.