I'm new to SpookyJS/CasperJS and I'm trying to figure out the execution flow.
This is what I'm trying to achieve:
load a page
store an image of the page
pass this image to a function and execute it (this process is quite long: ~15 seconds)
wait for the function to return the result
use the returned value to fill a field in the form in the loaded page
submit the form
this is a code snippet which tries to explain the solution I came up with:
var globProcessedImage;
try {
var Spooky = require('spooky');
} catch (e) {
var Spooky = require('../lib/spooky');
}
var spooky = new Spooky({
child: {
transport: 'http'
},
casper: {
logLevel: 'debug',
verbose: true
}
}, function (err) {
if (err) {
e = new Error('Failed to initialize SpookyJS');
e.details = err;
throw e;
}
spooky.start('http://example.com/');
spooky.then(function() {
this.captureSelector('./image.png', '#img-node');
});
spooky.waitFor(function() {
this.emit('image.processed');
return globProcessedImage !== undefined;
}, function then() {
processedImage = globProcessedImage;
this.sendKeys('#imagePassword', processedImage);
});
spooky.then(function() {
this.capture('./page.png');
});
spooky.run();
spooky.on('image.processed', function() {
setTimeout(function() {
globProcessedImage = 'my_result_string';
}, 15000);
});
});
spooky.on('error', function (e, stack) {
console.error(e);
if (stack) {
console.log(stack);
}
});
spooky.on('log', function (log) {
if (log.space === 'remote') {
console.log(log.message.replace(/ \- .*/, ''));
}
});
When I run the application I receive the following error:
ReferenceError: Can't find variable: globProcessedImage
How do I make globProcessedImage
visible in SpookyJS? Is this the correct approach to deal with external functions during Web automation?
Thanks in advance.