Reputation: 6346
The call to this.getPageContent()
doesn't retrieve the content of the last page opened.
How retrieve the content of the last page opened ?
var casper = require("casper").create({
verbose: true,
logLevel: "debug",
waitTimeout: 10000,
retryTimeout: 1000
});
casper.start(function() {
this.open('http://example.com/contentTypes', {
method: 'get',
headers: {'Accept': 'application/json'}
});
console.log(this.getCurrentUrl());
});
casper.then(function() {
// get content of http://example.com/contentTypes << OK
var ressources_type = JSON.parse(this.getPageContent());
require('utils').dump(ressources_type);
for (var i in ressources_type.data) {
this.thenOpen('http://example.com/ressource?type=' + ressources_type['data'][i]['key'] , {
method: 'get',
headers: { 'Accept': 'application/json'}
});
// get content of http://example.com/contentTypes instead of http://example.com/ressource?type=' + ressources_type['data'][i]['key']
var ressources = JSON.parse(this.getPageContent());
require('utils').dump(ressources);
for (var j in ressources['data']) {
...
}
}
});
casper.run(function() {
this.echo('Done.').exit();
});
Upvotes: 0
Views: 196
Reputation: 61922
CasperJS is asynchronous in nature. Every then*
and wait*
call (step functions) schedules the passed function, but doesn't execute it immediately. You are mixing asynchronous calls (thenOpen
) with synchronous calls (getPageContent
).
You need to move every synchronous call into a step function.
this.thenOpen('http://example.com/ressource?type=' + ressources_type['data'][i]['key'] , {
method: 'get',
headers: { 'Accept': 'application/json'}
});
this.then(function(){
var ressources = JSON.parse(this.getPageContent());
require('utils').dump(ressources);
for (var j in ressources['data']) {
...
}
});
Upvotes: 1