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
/***************************/
});
});
});
}