Wynn Netherland changelog.com/posts

tobi: Expressive server-side functional testing with jQuery

Ruby devs have long profited from powerful testing tools such as Cucumber, Webrat, and Capybara for functional testing.

Now TJ and LearnBoost bring the approach to server-side JavaScript apps with Tobi. Stitching together jsdom, htmlparser, and jQuery, Tobi lets you step through the DOM, follow links, fill in & submit forms, and assert results.

To use, configure Tobi in your test:

var tobi = require('tobi')
  , app = require('./my/app')
  , browser = tobi.createBrowser(app);

The browser object provides the entry point to Tobi. Let’s start by performing a GET to our application:

browser.get('/', function(res, $){

});

The first argument is the path, in this case the application root. We also supply a callback function that accepts two arguments - the response and the jQuery $ function which allows us to traverse the DOM. We can assert on both:

browser.get('/', function(res, $){
  res.should.have.status(200);
  $('h1').should.have.text("Howdy y'all");
});

As we mentioned, Tobi can also fill in forms

browser
  .fill({
      'user[name]': 'tj'
    , 'user[email]': 'tj@learnboost.com'
    , 'user[agreement]': true
    , 'user[digest]': 'Daily'
    , 'user[favorite-colors]': ['red', 'Green']
  }).submit(function(){

  });

… and follow links

$('a.register', function(res, $){

});

Tobi extends TJ’s should.js project to provide a large number of assertions for testing everything from DOM text and attribute to header fields and status codes.

[Source on GitHub]


Discussion

Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00