Testing is (or should be) important part of every software development. Over time various testing strategies and supporting tools and frameworks have been developed. Regarding Web development biggest advance has been made in Behavior Driven Development. Consequently many tools for BDD are published and used.
Ruby on Rails framework had great built-in support for testing from the very beginning. As it usually happens, lot of specialised testing tools appeared aside of it and among all of them my favourites are RSpec and Cucumber. The first one for unit and the second for functional tests. Both of them are well integrated with Ruby on Rails and are very easy to set up and start with. Moreover there is no need to use real browsers which results in fast tests execution. Perfect way for BDD.
Option —pre is used to install Capybara 0.4.0 rc
Important notice for MS Windows users: Webrat depends on Json gem which installs binaries compiled against Rubyinstaller Ruby 1.8.x version. If you are using 1.9.2 Ruby you must uninstall Json gem and install it again but with
Since article focuses on the functional testing outside of Rails we should manually create folder structure that Cucumber expects.
tests |- features |- support |- step_definitions
.feature files go in the
features folder. In the
env.rb file should be created and within it all set up must be made. Finally steps are implemented in Ruby files in
Webrat controls Selenium through Selenium RC (remote control) and selenium-client gem. In order to use Selenium through Webrat put following code in your
That’s all if you are running Linux based system. On Windows a little bit more effort must be made. First of all, Webrat usess 0.0.0.0 IP address when it starts Selenium and MS Windows does not like it at all. Secondly it uses
/dev/null stream which is not available on MS Windows. Patch is already submitted and you can follow a ticket at Webrat Lighthouse. But until fix is accepted and new version is released, you can take a patch from Github gitst and apply it to Webrat sources.
Besides this patch few more things must be done. Line:
must be added to the
config block as is already shown in the above snippet. Unfortunately selenium-client gem does not recognize Rubyinstaller since it is built using MinGW tools. Therefore one more tiny patch must be made in the
selenium-client-1.2.18/lib/nautilus/shell.rb file. Function
windows? must be replaced with:
You are ready for application testing. By default Selenium will use Firefox and if you want to use other browser (in the example Internet Explorer is set) add following line to
Although Capybara can use Selenium RC, it primarily uses Selenium WebDriver which is still in beta phase but is working good. Since we already installed all necessary gems we can go on with configuring our testing environment. File
env.rb should look like this:
Setting up Capybara is definitely much easier. But on MS Windows systems, if you want to use Internet Explorer, you still have to patch sources. Authors are already notified about required patch and I believe that new version of
selenium-webdriver gem will be released with it. In the meantime you just have to change definition of
initialize method in
Changing browser is as easy as changing
:chrome. Instead of
:firefox you can also use
:ff and for Internet Explorer
:internet_explorer. One more notice about differences if you are switching from Webrat to Capybara. Capybara will reset session after each step. If you do not want that (for example you log in to your application in the first scenario, and do not want to repeat it in each succeeding one) just add:
With Capybara you are not limited to Selenium WebDriver. If you want to use Selenium RC you just need to configure it in
Although I’m still not sure which one is better to use, I switched from Webrat to Capybara and I think that tests that use Selenium WebDriver are running faster. There is an initiative for merging these projects into one but I do not know if it will happen and when. I would like to hear what you think. What is your choice: Webrat or Capybara?