Ugly of CasperJS
2017-07-04 本文已影响16人
RoyTien
- 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()
andcasper.prototype()
will be executed secondly orderly by the number of level they are or how deep they are.
- variable and calling function will be executed firstly and then the
- 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 = [];