Question

try {
  var Spooky = require("spooky");
} catch (e) {
  console.log(e);
}

var spooky = new Spooky({
  capser: {
    logLevel: "debug",
    verbose: true
  },
  child: {
    command: "./casperjs/bin/casperjs",
    port: 8081,
    spooky_lib: "./node_modules/spooky/"
  }
}, function (err) {
  if(err) {
    console.log(err);
  }
  spooky.start("http://www.google.com");
  spooky.then(function () {
    console.log("7331");
    this.emit("printmsg", "1337");
  });
  spooky.run();
});

spooky.on("printmsg", function (msg) {
  console.log(msg);
});

spooky.on("error", function (e) {
  console.error(e);
});

When run, 1337 will be displayed, but 7331 will not. Why is this? The reason I'm asking is because it makes it difficult to debug when you want to log the values of certain variables.

Also, if you want to change the then function like so:

spooky.then(function () {
  var self = this;
  this.evaluate(function () {
    self.emit("printmsg", "Hello World!");
  });
});

This won't work because evaluate doesn't have access to the self variable. In PhantomJS you can make it page.evaluate(function (self) { but that isn't working when I try it with Spooky. So it's very difficult to log data when you want to.

Is there a way around this?

Était-ce utile?

La solution

I've found what was causing me this problem since posting this question, so I will share the answer below, in case anyone else encounters a similar problem:

In the standard quickstart example on the SpookyJS Github page, there's a commented-out 'console' event listener in Spooky which, when uncommented, will cause all output from Casper to appear on the screen:

// Uncomment this block to see all of the things Casper has to say.
// There are a lot.
// He has opinions.
spooky.on('console', function (line) {
    console.log(line);
});

When this event listener is set, it will log all output from Casper to the screen. The examples I was trying to log to the console in my question were all being run inside calls that Spooky was passing to Casper to process, so that's why I was not seeing them displayed. With this event listener set, my output appears, as well as the output used by the __utils__.echo function call in xShirase's answer.

Additionally, Casper provides an echo function which can be used to send output outside of Casper's evaluate function, which, by default, only has access to the scope of the page being viewed:

spooky.then(function () {
  this.echo("foo");
});

Since the standard configuration sets Casper's logging level to debug and sets verbose logging on, a lot of information will be displayed once this event listener is set. This can be avoided by setting the logging level to error or some other logging level supported by Casper.

Autres conseils

Oh how I stuggled with that one! At first, i was basically doing :

var msg = this.evaluate(function () {
    return('1337');
  });
console.log(msg);

Then I discovered that Casper injects a really useful clientutils module to each page : http://docs.casperjs.org/en/latest/modules/clientutils.html

It enables you to send logs from the remote DOM like so :

casper.then(function () {
this.evaluate(function () {
    __utils__.echo('1337');
  });
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top