April 26, 2010 by thilo
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.
Below is an example that runs one scenario with Selenium and the other with Culerity.
@selenium Scenario: Searching a blog post using autocomplete Given a blog post titled "Autocomplete made easy" containing "Some example" When I go to the search page And I fill in "Search" with "Auto" And I follow "Autocomplete made easy" Then I should see "Some example" @culerity Scenario: delete found comments Given a comment by author "Thilo" Given a comment by author "Tilo" When I search for "ilo" And I check the element with xpath "//p[contains(@class,'delete')]/input" And I press "Submit" Then I should see "Are you really sure?" When I press "Yes" Then I should see "Comments were delete" And I should see "0 Comments"
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.