Commit 3347f9e9 3347f9e9d857098ad544fdd90aaae966901ceb67 by Nicolas Perriault

fixes #107: client utils were possibly not yet injected sometimes

1 parent a00ae19f
......@@ -146,6 +146,7 @@ Casper.prototype.back = function back() {
* @return string Base64 encoded result
*/
Casper.prototype.base64encode = function base64encode(url, method, data) {
this.injectClientUtils();
return this.evaluate(function _evaluate(url, method, data) {
return __utils__.getBase64(url, method, data);
}, { url: url, method: method, data: data });
......@@ -608,6 +609,25 @@ Casper.prototype.getTitle = function getTitle() {
};
/**
* Injects Client-side utilities in current page context.
*
*/
Casper.prototype.injectClientUtils = function injectClientUtils() {
var clientUtilsInjected = this.evaluate(function() {
return typeof __utils__ === "object";
});
if (true === clientUtilsInjected) {
return;
}
var clientUtilsPath = require('fs').pathJoin(phantom.casperPath, 'modules', 'clientutils.js');
if (true === this.page.injectJs(clientUtilsPath)) {
this.log("Successfully injected Casper client-side utilities", "debug");
} else {
this.log("Failed to instantiate Casper client-side utilities!", "warning");
}
};
/**
* Logs a message.
*
* @param String message The message to log
......@@ -1297,12 +1317,7 @@ function createPage(casper) {
}
}
// Client-side utils injection
var clientUtilsPath = fs.pathJoin(phantom.casperPath, 'modules', 'clientutils.js');
if (true === page.injectJs(clientUtilsPath)) {
casper.log("Successfully injected Casper client-side utilities", "debug");
} else {
casper.log("Failed to instantiate Casper client-side utilities!", "warning");
}
casper.injectClientUtils();
// history
casper.history.push(casper.getCurrentUrl());
casper.emit('load.finished', status);
......
......@@ -8,10 +8,10 @@ casper.start('tests/site/index.html', function() {
this.test.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents');
this.test.comment('Casper.download()');
this.download(imageUrl, 'logo.png');
this.test.assert(fs.exists('logo.png'), 'Casper.download() downloads a file');
if (fs.exists('logo.png')) {
fs.remove('logo.png');
this.download(imageUrl, '__test_logo.png');
this.test.assert(fs.exists('__test_logo.png'), 'Casper.download() downloads a file');
if (fs.exists('__test_logo.png')) {
fs.remove('__test_logo.png');
}
});
......