Commit a50f6991 a50f6991811d52d44682bbb8b7d44bbe62d7f4c0 by Mickaël Andrieu

Merge pull request #892 from r8k/841

check javascriptEnabled status in the page
2 parents 06d6dc2f a5c7d0d5
......@@ -690,6 +690,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.options.pageSettings.javascriptEnabled === false) {
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 +984,13 @@ Casper.prototype.getCurrentUrl = function getCurrentUrl() {
"use strict";
this.checkStarted();
try {
return utils.decodeUrl(this.evaluate(function _evaluate() {
return document.location.href;
}));
if (this.options.pageSettings.javascriptEnabled === false) {
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;
});
});
......