Ugly of CasperJS

2017-07-04  本文已影响16人  RoyTien
// Array as a global variable
secondUrl = [];
casper.thenOpen(firstUrl, function(){
      //get second URL;
      secondUrl.push(newUrl);
});
casper.thenOpen(secondUrl[0], function(){
      // Error, secondUrl is undefined
});
// Function which will open a new page using casper
function newFunction(){
      casper.thenOpen(urlArray[0], function() {
        // urlArray works here!
      }
  }
(function() {
      // Declare Array, IMPORTANT, there is no 'var'
      // Array as a global variable
      urlArray = [];

      // Init CasperJS
      casper = require('casper').create({});
      casper.start(firstUrl, function() {
        // push new url into url array
        urlArray.push(newUrl);
      });

      // Wait for 0.001 second WORKS!!!
      casper.wait( 1, newFunction );

      // NOT WORKING!!!!
      casper.then( newFunction );
      // NOT WORKING!!!!
      casper.evaluate( newFunction );
      // NOT WORKING!!!!
      casper.then(function(){ this.evaluate( newFunction ); });
      // NOT WORKING!!!!
      casper.wait(1000, function(){}).thenEvaluate( newFunction );

      // Ending
      casper.run(function() {
        this.echo("Finished running ...");
        return this.exit();
      });
    ).call(this);

Maybe, casper.wait(1000, newFunction) means pause 1 second and then add newFunction into task queue; .then(newFunction), .evaluate(newFunction), .then(function(){this.evaluate(newFunction);}) and .thenEvaluate(newFunction) will add newFunction into task queue immediately.

上一篇下一篇

猜你喜欢

热点阅读