Using Selenium with Cucumber
Selenium and Cucumber, an unhappy couple?
I've recently noted that the Cucumber-wiki wasn't able to give me a complete setup anymore regarding Selenium. Installing it is a bit cumbersome to say the least and I struggle with it during every setup of a new project or when I need to upgrade the gems involved in one - as you know, once you start upgrading a gem, there's no end to it! This was happening to me a couple of days ago again. Instead of just cursing my way through it I thought I could share the information I've found to make things easier. So this post is about how my environment looks like and how I use the tools to be able to enjoy testing. I hope this will help some of you saving valuable time in the future.
How I use these tools
I use Selenium a bit with Webrat but I don't like the magic in it trying to wait at the correct moments for the correct thing to happen in the browser. It may be better by now, but since I have a workflow I like, I won't be changing this anytime soon.
That said, I also absolutely can't work with the standard Selenium api too. Oh God, that thing isn't straightforward. It may be me, but we really are enemies.
How do I craft my tests then? Well, where Webrat stops working (beyond standard opening, clicking, filling out and submitting forms), I use my own rSquery in jQuery-based applications (all my following apps will use jQuery in the foreseeable future). It boosted my Selenium productivity by about 10000% or something in that order :-).
Another thing I did is stop Webrat from starting the Selenium jars and the rails server. I've put the necessary commands in a tiny Applescript which gets called by a rake task. I do that because I want more control over the commands Selenium tries to give (= following it inside the terminal) and because I wouldn't need to start it again during a session (= winning lot's of time).
What do you need?
I think that most of my setup is based on the newest code available, except for my OS, which is still Mac OS 10.5.8. (I did not check if any part of this wouldn't be needed for a working environment, so I may be documenting too much here, but after all, I'm documenting my own):
- Webrat 0.5.3 (sudo gem install webrat)
- selenium-client 1.2.17 (sudo gem install selenium-client )
- Selenium 1.1.14 (sudo gem install Selenium)
- rspec 1.2.7 (sudo gem install rspec)
- rspec-rails 1.2.7.1 (sudo gem install rspec-rails)
- Cucumber 0.4.3 (sudo gem install cucumber)
- rSquery 0.1.2 (sudo gem install caifara-rSquery)
- Firefox (now 3.5.5)
Like the Cucumber-wiki notes, you have to replace the selenium-server.jar.txt in the Selenium gem to be able to use a recent Firefox. You can download Selenium-RC from http://seleniumhq.org/download/ and replace the selenium-server.jar.txt in location-of-the-gem/lib/selenium/openqa/ (just add the extra .txt suffix to the jar file).
Setup
You can find all the files mentioned below on github as a gist.
And the Applescript here (the praktijk command used in it, is just an alias to cd to the rails root of the project I'm working on, you'll have to change it if you want to use it)
Environment
I've separated my features in Selenium and standard Webrat ones like this, so I have several setup files. One for common stuff, one for Selenium and one for standard Webrat features. I've tried to cut everything out of these files to show only the basics to let this work. I hope I didn't forget something.
I also have a setup file for the webrat_hacks that disable the automatic start of the Selenium and the Rails server.
Selenium and Rails server
Like I mentioned before, I use an Applescript for this task and call that script from a rake task. (I've put the rake task in rails_root/lib/tasks (naturally) and the applescript in a rails_root/lib/applescript/ directory.
Running a Cucumber feature
First you have to start the servers: rake selenium:env
Then you can run the feature with the Selenium profile: cucumber -p selenium features/some_test.selenium.feature
Watch out for ...
You should make sure you don't require the Webrat environment when using the Selenium profile and vice versa.
You will have to alter this setup on some parts because I still have my step_definition-files coupled with my features on this project (which is considered a bad practice nowadays). Some lines of the files I posted are a consequence of that "wrong" setup as it isn't supported out of the box by Cucumber anymore and I have to jump through loops to require all step_definition files at once. That's also why I didn't post my Cucumber profiles (these are really ugly). This will be refactored at some point in the future.
So: one happy couple after all?
Well, I do like Selenium-Cucumber once it works. Also, rSquery tends to help me a lot. Writing Selenium tests is really easy if I use rSquery to trigger the Ajax based stuff on the page.
It is, however, not very easy to set up and each time one of the required gems change a bit I have to repair some stuff in the setup.
Is it worth the effort? You'll have to decide for yourself, but to me it sure is. Once I'm up and running, implementing steps tends to be straightforward and fast so I can make up for the lost time setting up Selenium and Cucumber.
blog comments powered by Disqus