Zombie.js/Node.js login with Captcha to Drupal 7 for testing...

I've been building a site in Drupal... and while I *like* the built in drupal simpleTest module, it falls short on actually clicky-browser-behavior. I'm not a huge fan of Selenium for just basic Use-Case testing, from a speed standpoint. So some of the things I want to do fall in the middle. And then I realized I needed to be able to pass a cpatcha as part of my tests (first item!).... so a little node.js and zombie.js and hitting mysql on the backend to fetch the actual captcha value.
NOTE - you will need to make a small change to drupal.js to prevent 'Drupal undefined on line 2' error from zombie. For some reason v8 differs with most browser implementations and complains about the Drupal variable not being defined on line 1 (line 2 below).. so a quick fix and we're ready to roll.

misc/drupal.js LINE 1:

if(typeof(Drupal) !== 'undefined'){ 
 var Drupal = Drupal || { 'settings': {}, 'behaviors': {}, 'locale': {} };
} else {
 var Drupal = { 'settings': {}, 'behaviors': {}, 'locale': {} };
}

Now that node.js will be happy.. here is some code that gets you logged in with Captcha. Yes, this is for testing, as the only way to get the captcha is to read mysql... this isn't for screen scraping someone elses site.

drupal_login.js:


var zombie = require("zombie");
var assert = require("assert");

var Client = require('mysql').Client;
var db = new Client();
var browser = new zombie.Browser();
 
db.user = '*****YOUR_mysql_user';
db.password = '*****YOUR_mysql_pass';

var homeURL = "http://YOUR_DRUPAL_7_WEBSITE/"; 

function loginToWebsite(homeURL,callback){
  browser.visit(
     homeURL, 
     function (err, browser, status) { 
        browser.captcha_token = 
       browser.field('captcha_token').value;
  db.query(
    "select solution from Drupal.captcha_sessions"+
    " where token='" + browser.captcha_token + "'",
    function selectCb(error, results, fields) {
      if (error) {
          console.log('GetData Error: ' + error.message);
          db.end();
          return;
      }
      captcha = results[0]['solution'];      
    browser.
    fill("name", "*****YOUR_Drupal_user").
    fill("pass", "*****YOUR_Drupal_user").
    fill("captcha_response",captcha).
    pressButton("op", function(err, browser, status) {  
         console.log('Successful login to '+homeURL);   
         
         /***************************/
         // Do whatever it is you need to do here
         /***************************/                    
         
    });  
      
     });
     });

}