{"id":428,"date":"2012-08-02T17:37:02","date_gmt":"2012-08-02T16:37:02","guid":{"rendered":"http:\/\/blogs.kent.ac.uk\/webdev\/?p=428"},"modified":"2012-08-03T10:42:04","modified_gmt":"2012-08-03T09:42:04","slug":"using-capybara-webkit-with-cucumber-without-rails-or-rack","status":"publish","type":"post","link":"https:\/\/blogs.kent.ac.uk\/webdev\/2012\/08\/02\/using-capybara-webkit-with-cucumber-without-rails-or-rack\/","title":{"rendered":"Using Capybara-Webkit with Cucumber Without Rails or Rack"},"content":{"rendered":"<p>Using <a href=\"https:\/\/github.com\/jnicklas\/capybara\">Capybara<\/a>\u00a0with <a href=\"https:\/\/github.com\/thoughtbot\/capybara-webkit\">Capybara-Webkit<\/a> and <a href=\"http:\/\/cukes.info\">Cucumber<\/a> 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 <a href=\"http:\/\/blogs.kent.ac.uk\/webdev\/2011\/10\/04\/working-with-behat\/\">Behat to write these kind of tests<\/a>\u00a0(<a href=\"http:\/\/mink.behat.org\/\">Mink<\/a> is roughly equivalent to Capybara). Since <a href=\"http:\/\/docs.behat.org\/guides\/1.gherkin.html\">both Behat<\/a> and <a href=\"https:\/\/github.com\/cucumber\/cucumber\/wiki\/Gherkin\">Cucumber use the Gherkin language<\/a> 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&#8217;s <a href=\"https:\/\/gist.github.com\/428105\">easy syntax<\/a>.<\/p>\n<p>A good start is to clone the <a href=\"https:\/\/github.com\/thuss\/standalone-cucumber\">Standalone Cucumber<\/a> repository over at Github. Then you&#8217;ll need to add Capybara-Webkit (<code>gem 'capybara-webkit'<\/code>) to the Gemfile. Capybara-Webkit requires you to install the QT libraries in order that it can be compiled. The installation of QT varies <a href=\"https:\/\/github.com\/thoughtbot\/capybara-webkit\/wiki\/Installing-Qt-and-compiling-capybara-webkit\">depending on your OS<\/a> but I on Mac OS X Lion it was a simple <code>brew install qt<\/code> and I was good to go. You then need to ensure all the gems are installed by running <code>bundle install<\/code>.<\/p>\n<p>Next head over to the <code>features\/support\/env.rb<\/code> and make some additions so it resembles the below.<\/p>\n<pre class=\"brush: ruby\">begin require 'rspec\/expectations'; rescue LoadError; require 'spec\/expectations'; end\r\nrequire 'capybara'\r\nrequire 'capybara\/dsl'\r\nrequire 'capybara\/cucumber'\r\nrequire 'capybara-webkit' # I added this\r\n\r\nCapybara.default_driver = :webkit # And changed this!<\/pre>\n<p>Write your tests using Cucumber as usual then you should be good to go! Use <code>bundle exec cucumber<\/code> (because you want to use Bundler&#8217;s version of the Gems) and you are away. I have this aliased to &#8220;c&#8221;, so I can run tests in a couple of keystrokes.<\/p>\n<p>Since it is loading a browser for every scenario, out of the box Capybara-Webkit can appear slow. In the next few weeks I&#8217;ll be experimenting with ways to make tests as fast as possible with a view to using Cucumber in a continuous integration setup.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using Capybara\u00a0with Capybara-Webkit and Cucumber allows behaviour driven testing that runs a browser proper in a similar manner to Selenium but headlessly. Most people using &hellip; <a href=\"https:\/\/blogs.kent.ac.uk\/webdev\/2012\/08\/02\/using-capybara-webkit-with-cucumber-without-rails-or-rack\/\">Read&nbsp;more<\/a><\/p>\n","protected":false},"author":2424,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1464],"tags":[8960,17089,17085,17086,17087,17088,17084],"_links":{"self":[{"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/posts\/428"}],"collection":[{"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/users\/2424"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/comments?post=428"}],"version-history":[{"count":16,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/posts\/428\/revisions"}],"predecessor-version":[{"id":443,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/posts\/428\/revisions\/443"}],"wp:attachment":[{"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/media?parent=428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/categories?post=428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/tags?post=428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}