Reputation: 27852
Here is what I am trying to do:
var casper = require('casper').create();
casper.start('mysite.html', function() {
});
/* Casper configuration */
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
})
function getAllImages()
{
// Get all images
var images = document.getElementsByTagName('img');
evaluateImages(images);
}
function evaluateImages(images)
{
console.log("I am in evaluateImages");
}
// Then find all pictures
casper.then(function() {
var product_image = this.evaluate(getAllImages);
});
casper.run();
But it never gets to evaluteImages function. What am I missing here?
Upvotes: 2
Views: 2412
Reputation: 43718
Well, I have never used CasperJS, however from what i've read in the docs, I believe that it's because the evaluateImages
function doesn't exist in the page's context.
I am not sure what's the best practice here, however it seems that you can return primitives from evaluate callbacks, so you could technically do something like:
function getAllImages() {
// Get all images
var images = document.getElementsByTagName('img');
//Return an array that contains all images src attribute
return Array.prototype.map.call(images, function (img) {
return img.src;
});
}
Then you can do something like:
var images = this.evaluate(getAllImages);
evaluateImages(images);
EDIT:
I need to treat all the images in the page context, as I am comparing each of them with other elements that are in the page...
Perhaps you could define a module in the page's context first:
this.evaluate(function () {
window.yourNS = {
evaluateImages: function (images) {
//do something with images
},
getAllImages: function () {
return document.getElementsByTagName('img');
}
};
});
Then you can do something like:
this.evaluate(function () {
yourNS.evaluateImages(yourNS.getAllImages());
});
Upvotes: 4