April 26, 2010 by thilo
The more layers you add when testing your application the slower these tests become.
With Culerity or Steam there are alternatives to Webrat which use HtmlUnit: HtmlUnit is a “GUI-Less browser for Java programs”. It models HTML documents and provides an API that allows you to load pages, fill out forms, click links, etc… just like you do in your “regular” browser. They are significantly faster than scripting a normal browser with Selenium at the cost that you don’t see how design works or if any browser issues cause trouble and it is still slower than Webrat without a special backend.
Capybara addresses the shortcomings of a two environments solution and still allows you to be flexible in what backend you use for integration testing on a per scenario basis in Cucumber. It does that by using Cucumber’s tags feature.
Below is an example that runs one scenario with Selenium and the other with Culerity.
You might have noticed the XPath expression in the second scenario. From time to time you will have to deal with XPath expressions when writing custom steps as XPath is used to locate elements within the DOM. The default steps can be scoped with CSS selectors or XPath, Capybara takes care of the conversion to XPath then.
Using Capybara with Cucumber is straightforward: First get the gem:
gem install capybara
On OSX you may have to install libffi, you can install it via MacPorts with:
sudo port install libffi
Then go to your rails project and generate the cucumber files as followed:
script/generate cucumber --capybara
Capybara definitely eases maintaining acceptance test and will give impulses to improve other tools, e.g. env-js.
Are you stuck in a maintenance nightmare or simply want to bring your testing skills to the next level? Get in touch with us.