This is a bit of a tricky question.
I am very familiar with javascript, however I am on a project that auto-crawls a website using PhantomJS and CasperJS. These are entirely new subjects to me.
I was able to figure out how to use Casper and navigate, log in to pages, etc, however it is unweildy as the general flow seems to be:
casper.start('http://google.fr/'); casper.then(function() { this.echo("I'm in your google."); }); casper.then(function() { this.echo('Now, let me write something'); }); casper.then(function() { this.echo('Oh well.'); }); casper.run(); My problem with this is that I want to do all sorts of things with the website, depending on what data is gotten with it. I can't pre-layout the sequence of navigations and not have it change. I hope this makes sense.
To solve this, I created a Javascript Navigator object with builtin functions. My general concept was:
navigator.logIn(function() { navigator.actionA(parameters, function() { if (navigator.data.a == navigator.data.b) { navigator.actionB(); } else { navigator.actionC(); } }); }); And embedded in each of these functions would be casper functions.
Here is a shortened version of my actual code, and where things started getting funky:
var casper = require('casper').create({ clientScripts: [ 'jquery.min.js' ], onError: function(self, m) { console.log('FATAL:' + m); self.exit(); }, }); var navigator = new _Navigator(); function _Navigator() { } _Navigator.prototype.logIn = function(aCallback) { var self = this; casper.start('https://website/login.asp', function() { if (1 == 1) { this.evaluate(function() { $("input[name=blah]").val('blahblah'); }); // ... A LOT MORE CODE aCallback(); } }); } _Navigator.prototype.search = function(aDataSet, aCallback) { var self = this; console.log('this works'); casper.then(function(){ console.log('this works'); }); var firstName = 'foobar'; casper.then(function(){ console.log('this works'); this.evaluate(function() { console.log('this no longer works!!'); $('input[id=blah]').val(firstName); aCallback(); }); }); } navigator.logIn(function() { // LOG IN RUNS, AND CALLS BACK SUCCESSFULLY... navigator.search({'dataset'}, function() { console.log('This never runs'); }); }); casper.run(); You'll notice that in the navigator.login function, I call casper.start(); In this, the evaluation function works fine, however then I do a callback function within that casper.start(); In my callback, I call the next function, navigator.search, which I suppose is still technically executing in the casper.start?
When I try running casper.evaluate within this new function called by the first callback function, everything seems to behave fine with the exception that casper.evaluate no longer works! It seems to eat the function, not printing any console logs or anything.
I have tried everything on this. I am not sure how to do this correctly. Does anyone have any suggestions on what I am doing wrong? Thanks.