美文网首页
CasperJS pass variables to caspe

CasperJS pass variables to caspe

作者: RoyTien | 来源:发表于2017-06-19 11:18 被阅读162次

From: https://stackoverflow.com/questions/26538463/how-to-pass-data-from-the-then-methods-in-casperjs
Author: Artjom B.

There are many way, but the easiest would be to use global variables. If you don't want to clutter your scripts with global variables (which should not be of the same concern as global variables in the browser, because there you could have different libraries), you can use IIFEs to reduce the scope.

casper.start(url);
(function(casper){
  var a;
  casper.then(function(){
    // set a
  }).then(function(){
    // use a
  });
})(casper);
casper.run();

Another version of the global one is to add those variables to the casper object.

Probably the cleanest solution would be to nest those blocks that need the variable. You have to keep in mind that a synchronous function call cannot come after an asynchronous one (those are all wait* and then* step functions). Scheduled steps are executed after the current stap has ended:

casper.start(url).then(function(){
  var a; // set a somehow
  this.then(function(){
    // use a
  });
}).then(function(){
  // don't use a
}).run();

相关文章

网友评论

      本文标题:CasperJS pass variables to caspe

      本文链接:https://www.haomeiwen.com/subject/rclgqxtx.html