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() { ...@@ -432,6 +432,19 @@ Casper.prototype.checkStarted = function checkStarted() {
432 }; 432 };
433 433
434 /** 434 /**
435 * Checks if executing JavaScript in the Page is enabled.
436 *
437 * @return Boolean
438 */
439 Casper.prototype.javascriptEnabled = function javascriptEnabled() {
440 "use strict";
441 if (this.options.pageSettings.javascriptEnabled === false) {
442 return false;
443 }
444 return true;
445 };
446
447 /**
435 * Clears the current page execution environment context. Useful to avoid 448 * Clears the current page execution environment context. Useful to avoid
436 * having previously loaded DOM contents being still active (refs #34). 449 * having previously loaded DOM contents being still active (refs #34).
437 * 450 *
...@@ -690,6 +703,10 @@ Casper.prototype.echo = function echo(text, style, pad) { ...@@ -690,6 +703,10 @@ Casper.prototype.echo = function echo(text, style, pad) {
690 Casper.prototype.evaluate = function evaluate(fn, context) { 703 Casper.prototype.evaluate = function evaluate(fn, context) {
691 "use strict"; 704 "use strict";
692 this.checkStarted(); 705 this.checkStarted();
706 // check whether javascript is enabled !!
707 if (!this.javascriptEnabled()) {
708 throw new CasperError("evaluate() requires javascript to be enabled");
709 }
693 // preliminary checks 710 // preliminary checks
694 if (!utils.isFunction(fn) && !utils.isString(fn)) { // phantomjs allows functions defs as string 711 if (!utils.isFunction(fn) && !utils.isString(fn)) { // phantomjs allows functions defs as string
695 throw new CasperError("evaluate() only accepts functions or strings"); 712 throw new CasperError("evaluate() only accepts functions or strings");
...@@ -980,9 +997,13 @@ Casper.prototype.getCurrentUrl = function getCurrentUrl() { ...@@ -980,9 +997,13 @@ Casper.prototype.getCurrentUrl = function getCurrentUrl() {
980 "use strict"; 997 "use strict";
981 this.checkStarted(); 998 this.checkStarted();
982 try { 999 try {
983 return utils.decodeUrl(this.evaluate(function _evaluate() { 1000 if (!this.javascriptEnabled()) {
984 return document.location.href; 1001 return this.page.url;
985 })); 1002 } else {
1003 return utils.decodeUrl(this.evaluate(function _evaluate() {
1004 return document.location.href;
1005 }));
1006 }
986 } catch (e) { 1007 } catch (e) {
987 // most likely the current page object has been "deleted" (think closed popup) 1008 // most likely the current page object has been "deleted" (think closed popup)
988 if (/deleted QObject/.test(e.message)) 1009 if (/deleted QObject/.test(e.message))
......
...@@ -110,3 +110,22 @@ casper.test.begin("evaluate() returns a value which can be altered", 1, function ...@@ -110,3 +110,22 @@ casper.test.begin("evaluate() returns a value which can be altered", 1, function
110 test.done(); 110 test.done();
111 }); 111 });
112 }); 112 });
113
114 // https://github.com/n1k0/casperjs/issues/841
115 casper.test.begin("evaluate() with js disabled, throws error", 1, function(test) {
116 casper.options.pageSettings.javascriptEnabled = false;
117 casper.start().then(function() {
118 function getListResult() {
119 return this.evaluate(function() {
120 return [{a: 1}, {b: 2}];
121 });
122 }
123 test.assertThrows(getListResult, undefined,
124 "Casper.evaluate() raises an error if JavaScript is disabled in the page");
125 });
126
127 casper.run(function() {
128 test.done();
129 casper.options.pageSettings.javascriptEnabled = true;
130 });
131 });
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -51,6 +51,26 @@ casper.test.begin('open() GET casing tests', 2, { ...@@ -51,6 +51,26 @@ casper.test.begin('open() GET casing tests', 2, {
51 } 51 }
52 }); 52 });
53 53
54 casper.test.begin('open() (JS disabled) tests', 3, {
55 setUp: setUp,
56 tearDown: tearDown,
57 test: function(test) {
58 casper.options.pageSettings.javascriptEnabled = false;
59 casper.open('tests/site/alert.html').then(function() {
60 test.pass("Casper.open() can open and load a location using GET, with JS disabled");
61 test.assertEquals(usedSettings, {
62 method: "get"
63 }, "Casper.open() used the expected GET settings");
64 test.assertHttpStatus(200, "Response Code is 200");
65 });
66
67 casper.run(function() {
68 test.done();
69 casper.options.pageSettings.javascriptEnabled = true;
70 });
71 }
72 });
73
54 casper.test.begin('open() POST tests', 2, { 74 casper.test.begin('open() POST tests', 2, {
55 setUp: setUp, 75 setUp: setUp,
56 tearDown: tearDown, 76 tearDown: tearDown,
......
...@@ -23,3 +23,17 @@ casper.test.begin('urls tests', 6, function(test) { ...@@ -23,3 +23,17 @@ casper.test.begin('urls tests', 6, function(test) {
23 test.done(); 23 test.done();
24 }); 24 });
25 }); 25 });
26
27 // https://github.com/n1k0/casperjs/issues/841
28 casper.test.begin('url tests with javascript disabled', 1, function(test) {
29 casper.options.pageSettings.javascriptEnabled = false;
30 casper.start('tests/site/urls.html');
31 casper.then(function() {
32 test.assertMatch(this.getCurrentUrl(), /urls\.html$/,
33 'Casper.getCurrentUrl() can work, with javascript disabled');
34 });
35 casper.run(function() {
36 test.done();
37 casper.options.pageSettings.javascriptEnabled = true;
38 });
39 });
......