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) { ...@@ -690,6 +690,10 @@ Casper.prototype.echo = function echo(text, style, pad) {
690 Casper.prototype.evaluate = function evaluate(fn, context) { 690 Casper.prototype.evaluate = function evaluate(fn, context) {
691 "use strict"; 691 "use strict";
692 this.checkStarted(); 692 this.checkStarted();
693 // check whether javascript is enabled !!
694 if (this.options.pageSettings.javascriptEnabled === false) {
695 throw new CasperError("evaluate() requires javascript to be enabled");
696 }
693 // preliminary checks 697 // preliminary checks
694 if (!utils.isFunction(fn) && !utils.isString(fn)) { // phantomjs allows functions defs as string 698 if (!utils.isFunction(fn) && !utils.isString(fn)) { // phantomjs allows functions defs as string
695 throw new CasperError("evaluate() only accepts functions or strings"); 699 throw new CasperError("evaluate() only accepts functions or strings");
...@@ -980,9 +984,13 @@ Casper.prototype.getCurrentUrl = function getCurrentUrl() { ...@@ -980,9 +984,13 @@ Casper.prototype.getCurrentUrl = function getCurrentUrl() {
980 "use strict"; 984 "use strict";
981 this.checkStarted(); 985 this.checkStarted();
982 try { 986 try {
987 if (this.options.pageSettings.javascriptEnabled === false) {
988 return this.page.url;
989 } else {
983 return utils.decodeUrl(this.evaluate(function _evaluate() { 990 return utils.decodeUrl(this.evaluate(function _evaluate() {
984 return document.location.href; 991 return document.location.href;
985 })); 992 }));
993 }
986 } catch (e) { 994 } catch (e) {
987 // most likely the current page object has been "deleted" (think closed popup) 995 // most likely the current page object has been "deleted" (think closed popup)
988 if (/deleted QObject/.test(e.message)) 996 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 });
......