Faster AJAX integration tests using Rails, Cucumber, Capybara, and envjs

Sunday, July 4th, 2010

I admit it. I put off using Cucumber for a few years while it became more stable. I took a look at it a few years ago and found it didn’t really work properly according to the then current documentation. So, I shelved it until recently. There seems a renewed interest in BDD and Cucumber recently so I decided to take another look. Needless to say, I was impressed and since I was starting a new project I figured it was the perfect time to start using Cucumber.

An hour or so into it, I still didn’t have my first test passing but I felt I was making progress so I persisted. Here is my first test:

Feature: Registration
  In order to use the site
  As a visitor
  I want to be able to create a new account

  @javascript
  Scenario: Signup Form
    Given there are no users with the email address test@domain.com
    And I am on the home page
    When I follow "Signup"
    Then I should see "First Name"

I added the ‘@javascript’ part later. I couldn’t figure out why my AJAX query wasn’t running in Cucumber. It was working fine in my browser so I asked my buddy Corey. He pointed me to Capybara.

Basically, there are 2 distinct pieces to the testing process in Cucumber.

1. the step definitions
2. the browser simulator

For the step definitions part, its not really about you write the steps but more about how those steps are translated back to the DOM and how they interface with the browser simulator. Cucumber uses Webrat as the default for step definitions and I may have just followed the introduction docs to end up with that as the default. The problem with Webrat is that it doesn’t support interfacing with the DOM very well and so it doesn’t allow access to page elements that have been updated by Javascript. That means any AJAX testing is completely out the window. Unfortunately, most websites today pretty much require Javascript so Webrat is pretty much useless to me.

Capybara is a Webrat alternative that does support Javascript. Capybara allows you to access DOM elements that have been updated by Javascript. Now here’s where things get tricky. Both Webrat and Capybara integrate with Selenium, a browser simulator. The same simulator is being run in both cases but only Capybara has the ability to access the updated DOM elements.

This allowed me to finish up my first test. I didn’t notice it at first but after I added a second test I realized how slowly these tests were running. I didn’t get any quantitative performance data to share but just sitting there waiting for 2 simple tests to run I was pretty amazed that anyone would even be testing like this. There would be no way this could scale to a suite of tests for an entire site. The time wasted in just waiting for all the tests to run would prevent any actual development to occur.

So I started looking for alternatives. The culprit in the slowdown here was Selenium. I came across a blog post by RubyFlare the was talking about a much faster alternative to Selenium called envjs. You need to install the capybara-envjs gem, the envjs gem, and reconfigure some things in Cucumber but its fairly simple.

Now that I had my brand new envjs browser simulator working with Cucumber and Capybara, I tried to run my first 2 tests on it. FAILED

What I discovered in the envjs output was:

ERROR: [Sun Jul 04 2010 17:44:10 GMT-0600 (CST)] {ENVJS} response XML does not apear to be well formed xml Line: 1377
ERROR: [Sun Jul 04 2010 17:44:10 GMT-0600 (CST)] {ENVJS} ReferenceError: $domparser is not defined

I started digging through the envjs code and basically discovered it doesn’t really support AJAX calls returning HTML. Just XML. Well back to the drawing board. It doesn’t seem like it would be incredibly difficult to add support for that to envjs and the rest of the chain of libraries up to Cucumber. When? I’m not going to start responding to all my AJAX queries with well formed XML instead of simple HTML anytime soon. It would just add an unnecessary layer of complexity to the application and I would have to do it just to use the envjs browser. No thanks. Back to the drawing board.

Since I did get my first AJAX tests to pass using Selenium, I reverted back to using that so I can at least progress for now, albeit slowly. I know a lot of people are frustrated by the slowness of Selenium so I doubt it’ll be too long before envjs supports HTML to be returned from AJAX calls.

Engine Yard problems

Saturday, March 6th, 2010

Here I am with yet another proprietary EngineYard problem again. I simply clone an existing application that’s up and running fine and the new cloned environment doesn’t boot. Its giving me this error instead:

/usr/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/rails/gem_dependency.rb:224:in `specification’: undefined method `version_requirements=’ for # (NoMethodError)

Here’s a recent thread about the same issue:

http://rubyforge.org/tracker/index.php?func=detail&aid=27868&group_id=126&atid=575

Looking into the differences in my ‘cloned’ environment and my actual environment and I see the gems list isn’t even the same. So basically Engine Yard’s clone command doesn’t actually clone the environment at all. It just took my old apps and tried to copy and paste everything onto a newly configured environment which obviously doesn’t work.

My original environment gems:

abstract (1.0.0)
actionmailer (2.3.5, 2.3.3, 2.2.2, 1.3.6)
actionpack (2.3.5, 2.3.3, 2.2.2, 1.13.6)
actionwebservice (1.2.6)
activerecord (2.3.5, 2.3.3, 2.2.2, 1.15.6)
activeresource (2.3.5, 2.3.3, 2.2.2)
activesupport (2.3.5, 2.3.3, 2.2.2, 1.4.4)
addressable (2.1.1)
aws-s3 (0.6.2)
builder (2.1.2)
daemons (1.0.10)
erubis (2.6.2)
eventmachine (0.12.6)
extlib (0.9.9)
eyrubygems (0.0.2)
facter (1.5.2)
fastercsv (1.5.1)
fastthread (1.0.7)
ferret (0.11.6)
gem_plugin (0.2.3)
hpricot (0.8.2, 0.8.1)
igrigorik-em-http-request (0.1.5)
json (1.1.3)
mime-types (1.16)
mixlib-cli (1.0.4)
mixlib-config (1.0.12)
mixlib-log (1.0.3)
mongrel (1.1.5.1)
mongrel_cluster (1.0.5)
ohai (0.2.0)
open4 (0.9.6)
rack (1.0.1, 0.4.0)
rails (2.3.5, 2.3.3, 2.2.2, 1.2.6)
rake (0.8.3)
rest-client (1.3.1, 0.9.2)
right_aws (1.10.0)
right_http_connection (1.2.4)
rmagick (2.8.0)
ruby-openid (2.1.2)
rubyforge (1.0.3)
sparklines (0.5.2)
stomp (1.0.6)
systemu (1.2.0)
will_paginate (2.3.12)
xml-simple (1.0.12)

And the ‘Cloned’ environment gems:

abstract (1.0.0)
actionmailer (2.3.3, 2.2.2, 1.3.6)
actionpack (2.3.3, 2.2.2, 1.13.6)
actionwebservice (1.2.6)
activerecord (2.3.3, 2.2.2, 1.15.6)
activeresource (2.3.3, 2.2.2)
activesupport (2.3.3, 2.2.2, 1.4.4)
addressable (2.1.1)
aws-s3 (0.6.2)
builder (2.1.2)
daemons (1.0.10)
erubis (2.6.2)
eventmachine (0.12.6)
extlib (0.9.9)
eyrubygems (0.0.2)
facter (1.5.2)
fastercsv (1.5.1)
fastthread (1.0.7)
ferret (0.11.6)
gem_plugin (0.2.3)
hpricot (0.8.2, 0.8.1)
igrigorik-em-http-request (0.1.5)
json (1.1.3)
mime-types (1.16)
mongrel (1.1.5.1)
mongrel_cluster (1.0.5)
ohai (0.2.0)
open4 (0.9.6)
rack (1.0.1, 0.4.0)
rails (2.3.3, 2.2.2, 1.2.6)
rake (0.8.3)
rest-client (0.9.2)
right_aws (1.10.0)
right_http_connection (1.2.4)
rmagick (2.8.0)
ruby-openid (2.1.2)
rubyforge (1.0.3)
sparklines (0.5.2)
stomp (1.0.6)
will_paginate (2.3.12)
xml-simple (1.0.12)

Clearly this new environment isn’t a ‘clone’ at all. You can’t just put feathers in your butt and call yourself a chicken. Don’t call this feature ‘clone’ when its really just recreating everything on a completely different setup if they’ve changed their default environment configuration. It should be called the ‘waste another 4 hours fixing our broken Engine Yard system’ feature. I think its a little more accurate.

Rojo: An easy to install Ruby on Rails CMS

Monday, February 8th, 2010

Rojo is Onomojo’s Ruby on Rails Content Management System. You can find it here:

http://github.com/onomojo/rojo

Today we made some changes to make the initial setup of a Rojo instance easier. The steps to setup a new Rojo instance are clearly detailed in the README. This should help eliminate most of the problems new users were encountering when trying to setup Rojo from scratch. Please let us know if you have any comments or suggestions for improving the setup process.

In the next few weeks, we will be releasing the plugins that we’ve developed for Rojo including a blog, video gallery, photo gallery, and more so stay tuned.

Rojo – A Ruby on Rails CMS

Sunday, January 31st, 2010

Rojo logo

We’ve been working on this part time for a while now and I finally decided to just go ahead and release it. Its far from perfect and isn’t what I originally envisioned but it does work and we do use it on a number of sites. Its fairly stable and we have a handful of plugins that I’ll be releasing in the next week or two.

The 2 main motivations for Rojo came from the lacking diversity in Ruby on Rails content management systems. There are a few and some work well but I wasn’t happy with the learning curve with the ones I explored when we initially started this project. It seemed to me that every time I embraced a CMS the CMS ended up determining how I had to write my application. Its simply impossible to craft a CMS that enforces a strict data and programming interface that works for every situation. I didn’t want a content management system that forced me to write my tables one way or another or forced me to communicate with the CMS in certain ways. In short, I really just wanted a simple core that I could easily extend in any way I needed without having to conform to any standard imposed by the CMS.

You can check out the Rojo code here: http://github.com/onomojo/rojo

Rojo – An open source Ruby on Rails CMS

Saturday, February 7th, 2009

I’ve been planning out a content management system in Ruby on Rails for a while now and will be releasing a beta version in the next few weeks. I’m calling the project Rojo. There are quite a few CMSs out there for PHP already and they’re quite mature at this point but the ones I’ve seen for Rails are pretty pathetic to say the least. They’re all really limited in functionality and lack modularity so they’re not always the easiest to extend.

My company, Onomojo, does a decent amount of Ruby on Rails development and have been working on building a solid CMS that’s modular, easily extended, and easily customized. The project itself is coming along nicely. I’ve taken concepts that I like from different content management systems that I’ve used over the past decade and combined them in a way that helps minimize the effort involved in developing Rails sites for our clients. I’m pretty excited about it and I’ll be releasing the core code and the plugin code to the public after we get a few pieces developed so stay tuned for more on Rojo.