1

I'm trying to write a test for my site, by logging into it and performing some actions. I've got a script that is SO CLOSE to working but there is one key flaw which i haven't been able to circumvent. See logic below

var phantom = require('phantom');
var rl = require('readline-sync');
var Spooky = require('spooky');

// Go to login page
spooky.start(url);

// Generate an image of the captcha 
spooky.then(function () {
  this.capture('captcha.png');
});

// Fill in login form
  spooky.then([{
    captcha: new Captcha()
  }, function () {
    this.fill('form#login-form', {
      'username': 'username',
      'password': 'password',
      'recaptcha_response_field' : captcha.phrase
    });
  }]);

  spooky.wait(5000, function() {
    this.capture('afterlogin.png');
  });

  spooky.run();

function Captcha() {
  return {
    phrase: ask("Enter Captcha Phrase: ")
  }
}

function ask (msg, options){
  return rl.question(msg, options)
}

So, this generates the images fine, fills out the form fine. The problem is with injecting the "new Captcha()" function. If, for example, I didn't need to see an image of the form first, i could put in my username and pass with the ask function, and it would fill it all out. But this seems to evaluate before the spooky promises.

When I run the script, it asks for the phrase first, then after i enter it it generates the image. I need to generate the image BEFORE asking the phrase. If anybody could shed some light on my issue that would be great! Let me know if you need any more info.

Shan Robertson
  • 2,742
  • 3
  • 25
  • 43
  • Well, you're calling `Captcha`, so yes it will be immediately executed. You probably wanted to create a new function: `function Captcha(){ this.phrase = function(){ ask("...") } }` and call it later, but this will probably not work, because you cannot pass functions through contexts (from spookyjs context to casperjs context) – Artjom B. Apr 19 '16 at 22:43
  • yeah i hear you, there's gotta be some way to prompt from within the then though – Shan Robertson Apr 19 '16 at 22:50

0 Answers0