Commit 6fffaef3 6fffaef3aa09811608fdc2396f97f98dcabffad1 by Rajiv Kilaparti

check javascriptEnabled status in the page

 * checks javascriptEnabled status, before `evaluating` JS
 * fixes #841
1 parent 332e6383
......@@ -432,6 +432,19 @@ Casper.prototype.checkStarted = function checkStarted() {
};
/**
* Checks if executing JavaScript in the Page is enabled.
*
* @return Boolean
*/
Casper.prototype.javascriptEnabled = function javascriptEnabled() {
"use strict";
if (this.options.pageSettings.javascriptEnabled === false) {
return false;
}
return true;
};
/**
* Clears the current page execution environment context. Useful to avoid
* having previously loaded DOM contents being still active (refs #34).
*
......@@ -690,6 +703,10 @@ Casper.prototype.echo = function echo(text, style, pad) {
Casper.prototype.evaluate = function evaluate(fn, context) {
"use strict";
this.checkStarted();
// check whether javascript is enabled !!
if (!this.javascriptEnabled()) {
throw new CasperError("evaluate() requires javascript to be enabled");
}
// preliminary checks
if (!utils.isFunction(fn) && !utils.isString(fn)) { // phantomjs allows functions defs as string
throw new CasperError("evaluate() only accepts functions or strings");
......@@ -980,9 +997,13 @@ Casper.prototype.getCurrentUrl = function getCurrentUrl() {
"use strict";
this.checkStarted();
try {
if (!this.javascriptEnabled()) {
return this.page.url;
} else {
return utils.decodeUrl(this.evaluate(function _evaluate() {
return document.location.href;
}));
}
} catch (e) {
// most likely the current page object has been "deleted" (think closed popup)
if (/deleted QObject/.test(e.message))
......
......@@ -110,3 +110,22 @@ casper.test.begin("evaluate() returns a value which can be altered", 1, function
test.done();
});
});
// https://github.com/n1k0/casperjs/issues/841
casper.test.begin("evaluate() with js disabled, throws error", 1, function(test) {
casper.options.pageSettings.javascriptEnabled = false;
casper.start().then(function() {
function getListResult() {
return this.evaluate(function() {
return [{a: 1}, {b: 2}];
});
}
test.assertThrows(getListResult, undefined,
"Casper.evaluate() raises an error if JavaScript is disabled in the page");
});
casper.run(function() {
test.done();
casper.options.pageSettings.javascriptEnabled = true;
});
});
\ No newline at end of file
......
......@@ -51,6 +51,26 @@ casper.test.begin('open() GET casing tests', 2, {
}
});
casper.test.begin('open() (JS disabled) tests', 3, {
setUp: setUp,
tearDown: tearDown,
test: function(test) {
casper.options.pageSettings.javascriptEnabled = false;
casper.open('tests/site/alert.html').then(function() {
test.pass("Casper.open() can open and load a location using GET, with JS disabled");
test.assertEquals(usedSettings, {
method: "get"
}, "Casper.open() used the expected GET settings");
test.assertHttpStatus(200, "Response Code is 200");
});
casper.run(function() {
test.done();
casper.options.pageSettings.javascriptEnabled = true;
});
}
});
casper.test.begin('open() POST tests', 2, {
setUp: setUp,
tearDown: tearDown,
......
......@@ -23,3 +23,17 @@ casper.test.begin('urls tests', 6, function(test) {
test.done();
});
});
// https://github.com/n1k0/casperjs/issues/841
casper.test.begin('url tests with javascript disabled', 1, function(test) {
casper.options.pageSettings.javascriptEnabled = false;
casper.start('tests/site/urls.html');
casper.then(function() {
test.assertMatch(this.getCurrentUrl(), /urls\.html$/,
'Casper.getCurrentUrl() can work, with javascript disabled');
});
casper.run(function() {
test.done();
casper.options.pageSettings.javascriptEnabled = true;
});
});
......