After my search from earlier this week I encountered another hard-to-debug problem.
After reading an article about using gemsets to isolate a specific environment, I was ready to try the rvm-gemset-thingy in a new project we're working on.
I already had rvm on my MacBook Pro, so this ought to be a breeze. After upgrading rvm to the newest version I installed a .rvmrc file and ran bundler install` and I thought I was all set. The result, however, was very much the contrary!
I started to get all kinds of errors like:
**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance.
You can install the extension as follows:
gem install bson_ext
If you continue to receive this message after installing, make sure that the
bson_ext gem is in your load path and that the bson_ext and mongo gems are of the same version.
and like:
~/.rvm/gems/ruby-1.8.7-p249@carr/gems/nokogiri-1.4.2/lib/nokogiri/nokogiri.bundle, 9): no suitable image found.
After a lot of searching and blaming mongo (which I'm using for the first time), I found this stackoverflow answer. I remembered I had setup rvm on Leopard and then upgraded to Snow Leopard.
So I did a complete reinstall of rvm and was really pleased to see everything working as expected. I hope I can save someone a bunch of time with this.
I lost the better part of this friday evening setting up Rails 3 with rSpec. The solution to these problems may be documented somewhere, but I sure had trouble finding and solving these. Hopefully this information can help you!
issue 1: testing Rails helpers that render Haml
If you need to test Haml with rSpec, make sure you have the following lines in your spec_helper (within Rspec.configure do |config|):
Nothing changed in comparison to rSpec 1 but config.prepend_before, which is now config.before(:each).
issue 2: undefined method `has_tag?'
I allways took the availability of this matcher for granted. Not anymore! To use it in a helper method (which renders a piece of haml in my case), I needed to add another line to Rspecs configure block:
Didn't even know this method is defined in Webrat in the first place. If someone knows a better solution, let me know, but I allready lost an embarrassing amount of time looking into this. Off to bed now …
I'm not a Google fanboy, but we all have to face it: folks at Google make splendid software and they sure hit a lot of sweet spots.
Yesterday however, I got stuck on a problem with Google calendar. As with desktop calendars you can subscribe to a feed. Adding a feed works as expected, but Google only refreshes feeds once a day or even less. This makes the feature worthless to me and my clients as our software uses its own calendar (and its own logic). Calendar updates also tend to happen multiple times a day.
Because of this I started looking for another vendor and after a while (there isn't much to look at when you're in the market for a free solution) I discovered that Yahoo is working on a new calendar (the old one is really worthless and the looks are ... well, you should try it). You can use the new beta calendar if you go through this form.
I don't really know how good the calendar works as I lack experience, but it sure handles feeds predictable and updates them often. You can also click a manual refresh button. See the screenshot to convince yourself:
I think the Yahoo calendar is definitely worth looking at. Bonus is that Blackberry phones can synchronize with Yahoo calendar. That is in fact why I was searching in the first place: trying to get our calendar feed onto a Blackberry.
I've recently noted that the Cucumber-wiki wasn't able to give me a complete setup anymore regardingSelenium. 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):
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.
I've always been very interested in how people behave and feel and how that correlates with my life and feelings, both in the past and present. The TED-talk of Alain de Botton stroked me immediately:
"As on a Sunday evening as the sun is starting to set and the gap between my hopes for myself and the reality of my life start to diverge so painfully that I normally end up weeping into a pillow." - Alain de Botton
Now, he knows how to make everybody listen!
Although a bit exaggerated here, I suppose everyone has had thoughts like these before. De Botton describes and analyses these typically career related thoughts with verve and puts them in a wider perspective. He also suggests some new ideas.
If possible, try to make some time to watch the complete talk.
How odd, a first post that isn't the first post. It is in fact the second and most uninteresting post. The rSquery post was the first one. When I wrote it, we didn't have a blog at all, so I had to put it in a folder and wait till we did. To make it realistic, we've predated the previous post to the time when I effectively wrote it. So this post serves the only cause to inform you about the date this blog takes off ...
Choosing the red pill and maybe we'll stay in wonderland ... *
The moment had to come eventually: we are proud to present our first gem, rSquery. A project that aims to reduce the pain involved in writing Selenium tests in rSpec environments for webapps that use jQuery.
The project contains several methods to help you with writing javascript commands for selenium and javascript based tests for Cucumber or maybe even rSpec in projects that use jQuery.
The name is made of the purposes:
r - rSpec
S - Selenium
query - jQuery
History
The project's origin lies in a project I work on at by2.be which makes use of jQuery for most of its tasks. Selenium tests were the only way to test this bugger (as webrat isn't an option to test js actions) and these tests were taking all the speed and fun out of the project. Testing with jquery matchers instead of those provided by selenium sounded very handy and after some hours of frustration we hacked these few lines together and because of the enormous speed enhancements in writing tests I decided to wrap things up in a gem and release them to whoever would like to use jQuery matchers in an easy way to handle tests.
How it works
The source code is so short it should speak for itself but let me explain the concept:
Everything works around a central jQuery matcher you call with 'jquery(matcher)'. If you leave out 'matcher' the matcher returns the whole document.
The matcher must match exactly one element or it will raise an error. This behaviour makes testing easier for me, but I'm still in search of a better idea here. For now it does the job. (If you have ideas, let me know! Or fork the project on github)
You can call methods upon the matched jquery object: wait_for_element(matcher), wait_for_text(text), wait_for_visible, wait_for_invisible.
Other methods will call that same method upon the jquery. Example: jquery("#content").html will call $("#content").html() in the page you're testing and return what that method returns.
Examples
Using the gem in a project with jQuery you can use lines like these inside tests:
Pitfalls
rSquery only works if jQuery is activated on the page you test (obviously).
rSquery can only do tests that are purely javascript based. Clicking links will not work, use normal Selenium commands for triggering js-less events (just like jQuery itself).
requirements
rSpec
Cucumber
Ruby-Selenium
Image
Image from http://feedingthepuppy.typepad.com/feeding_the_puppy/
This blog is written by Wouter Vancraeynest and Ivo Dancet and is about what we come across while working at our company by2.be, located in Bruges, Belgium. This can be anything related - though sometimes only remotely - to programming, testing, marketing, Ruby, Apple and other things we do and love ...