Ugly of CasperJS

  • Cannot pass variables into casper.prototype();
  • Not sure about the execution order;
    • variable and calling function will be executed firstly and then the casper.prototype();
    • the calling of casper.prototype() and casper.prototype() will be executed secondly orderly by the number of level they are or how deep they are.
  • As soon as casper.prototypes are in the task queue, the parameters they hold will be fixed.
    • This means, if you want use the url (which is capture from first casper.open) in second url. There will be an error.
// 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
});
  • However, the second level casper.prototypes can use the new url.
// 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.

  • There is no global variables, need to use array and without var with declaration: myArr = [];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 蛰伏余载未忘怀, 不言放弃盼花开。 阳光雨露勤呵护, 一帆风顺指日待。 养了多年的白掌很久没开花了,苦心栽培,今年...
    我是神话_33ff阅读 2,844评论 0 1
  • 星期二 晴 You don't judge da book by its cover or a person fr...
    sophietyl阅读 877评论 0 0