(BDD) Testing with Cucumber/Selenium/Webrat
Grappling with the best way to capture our client's requirements in an expressive (and agile way), we are giving Cucumber a serious consideration.
I wont go over the ins & outs of Cucumber, as there are plenty of resources already available. Here's a brief of the steps that I followed (and some of the issues I ran into), which should prove useful if you are doing something similar.
h4. Install the Cucumber gem
@sudo gem install cucumber@
If you're also having trouble with "gem install" (mirror server timeouts), wget the .gem, and build the gem yourself
h4. Getting Cucumber to talk to Selenium
Follow the steps in "Aslaks Setup":http://wiki.github.com/aslakhellesoy/cucumber/setting-up-selenium
h4. Install Webrat
@sudo gem install webrat@
h4. Start the Selenium RC server
Note: if you are also having trouble with timeouts, at time of writing there was an "unpatched bug":https://webrat.lighthouseapp.com/projects/10503/tickets/205-patch-timeout-parameter-passed-incorrectly-to-selenium in Webrat, which can be fixed by a monkey patch. For me this solved one issue, but the above remained.
But I personally prefer the syntactic sugar that Webrat gives me, so its worth getting this working.
wget <http://server/path/to/cucumber-0.2.3.1.gem> git clone git://github.com/aslakhellesoy/cucumber.git cd cucumber rake install_gem
selenium
Cucumber comes with 2 trivial examples, which you can run;
h4. Running the sample Cucumber + Selenium test
cucumber ../selenium/features/
Ensure this works first before trying the Webrat test below;
h4. Running the sample Cucumber + Selenium + Webrat test
To run the example test which includes Webrat, you can also use the rakefile supplied;
@../examples/selenium_webrat]$ rake features --trace@
Jackpot Winner!
or not. For me at least. I hit the wall with a timeout error, which is probably some missing Webrat config;
23:59:22.555 INFO - Command request: type[webrat=q, rspec] on session fadcc5971c4d47e5aae10c41e03af3b2
23:59:22.578 INFO - Got result: OK on session fadcc5971c4d47e5aae10c41e03af3b2
23:59:22.581 INFO - Command request: waitForCondition[ var element;
try {
element = selenium.browserbot.findElement('button=evalregex:/btnG/');
} catch(e) {
element = null;
}
element != null;, 10000] on session fadcc5971c4d47e5aae10c41e03af3b2
23:59:32.607 INFO - Got result: Timed out after 10000ms on session fadcc5971c4d47e5aae10c41e03af3b2
23:59:32.611 INFO - Command request: testComplete[, ] on session fadcc5971c4d47e5aae10c41e03af3b2click_button('btnG')
versus
@browser.click 'btnG'
h4. Other Methods
In my quest for Cucumber Glory, I did stumble across these useful, related resources;
# "How To Setup RSpec, Cucumber, Webrat, RCov and Autotest on Leopard":http://www.claytonlz.com/index.php/2009/04/how-to-setup-rspec-cucumber-webrat-rcov-and-autotest-on-leopard/
# "Testing Non-Ruby Applications with Cucumber":http://www.vitarara.org/cms/testing_non-ruby_applications_with_cucumber
# "Using Cucumber for Acceptance Testing":http://www.pathf.com/blogs/2009/03/using-cucumber-for-acceptance-testing/
# "Cucumber...with webrat mechanized sessions":http://www.pathf.com/blogs/2009/03/using-cucumber-for-acceptance-testing/comment-page-1/#comment-6101
## And oelmekkis "env.rb":http://gist.github.com/82813
h4. My currently installed Gems
FYI my gem versions at time of writing;
*** LOCAL GEMS *** actionmailer (2.3.2, 2.2.2) actionpack (2.3.2, 2.2.2, 1.13.6) actionwebservice (1.2.6) activerecord (2.3.2, 2.2.2, 1.15.6) activeresource (2.3.2, 2.2.2) activesupport (2.3.2, 2.2.2, 1.4.4) builder (2.1.2) cucumber (0.2.3.1, 0.1.12) diff-lcs (1.1.2) fastercsv (1.4.0, 1.2.3) hoe (1.12.1) mechanize (0.9.2) nokogiri (1.2.3) polyglot (0.2.5) rails (2.3.2, 2.2.2) rake (0.8.4) rspec (1.1.12) ruby-oci8 (1.0.4) rubyforge (1.0.3) Selenium (1.1.14) selenium-client (1.2.14) syntax (1.0.0) term-ansicolor (1.0.3) treetop (1.2.5) webrat (0.4.3)