Commit 0f6fd644 0f6fd644e04e1b4866a80f55a0bbe602ac889c7b by Nicolas Perriault

backport of b6f060d5 to require branch - refs #32 - ClientUtils.click() handles <…

…a href='javascript:'> links
1 parent 42633d67
...@@ -232,7 +232,7 @@ Casper.prototype = { ...@@ -232,7 +232,7 @@ Casper.prototype = {
232 */ 232 */
233 click: function(selector, fallbackToHref) { 233 click: function(selector, fallbackToHref) {
234 fallbackToHref = utils.isType(fallbackToHref, "undefined") ? true : !!fallbackToHref; 234 fallbackToHref = utils.isType(fallbackToHref, "undefined") ? true : !!fallbackToHref;
235 this.log("click on selector: " + selector, "debug"); 235 this.log("Click on selector: " + selector, "debug");
236 return this.evaluate(function(selector, fallbackToHref) { 236 return this.evaluate(function(selector, fallbackToHref) {
237 return __utils__.click(selector, fallbackToHref); 237 return __utils__.click(selector, fallbackToHref);
238 }, { 238 }, {
...@@ -533,14 +533,18 @@ Casper.prototype = { ...@@ -533,14 +533,18 @@ Casper.prototype = {
533 try { 533 try {
534 result.value = JSON.stringify(window[name]); 534 result.value = JSON.stringify(window[name]);
535 } catch (e) { 535 } catch (e) {
536 result.error = 'Unable to JSON encode window.' + name + ': ' + e; 536 var message = 'Unable to JSON encode window.' + name + ': ' + e;
537 __utils__.log(message, "error");
538 result.error = message;
537 } 539 }
538 return result; 540 return result;
539 }, {'name': name}); 541 }, {'name': name});
540 if (result.error) { 542 if ('error' in result) {
541 throw result.error; 543 throw new Error(result.error);
542 } else { 544 } else if (isType(result.value, "string")) {
543 return JSON.parse(result.value); 545 return JSON.parse(result.value);
546 } else {
547 return undefined;
544 } 548 }
545 }, 549 },
546 550
......
...@@ -57,15 +57,29 @@ ...@@ -57,15 +57,29 @@
57 fallbackToHref = typeof fallbackToHref === "undefined" ? true : !!fallbackToHref; 57 fallbackToHref = typeof fallbackToHref === "undefined" ? true : !!fallbackToHref;
58 var elem = this.findOne(selector); 58 var elem = this.findOne(selector);
59 if (!elem) { 59 if (!elem) {
60 this.log("click(): Couldn't find any element matching '" + selector + "' selector");
60 return false; 61 return false;
61 } 62 }
62 var evt = document.createEvent("MouseEvents"); 63 var evt = document.createEvent("MouseEvents");
63 evt.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, elem); 64 evt.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, elem);
65 // dispatchEvent return value is false if at least one of the event
66 // handlers which handled this event called preventDefault
64 if (elem.dispatchEvent(evt)) { 67 if (elem.dispatchEvent(evt)) {
65 return true; 68 return true;
66 } 69 }
67 if (fallbackToHref && elem.hasAttribute('href')) { 70 if (fallbackToHref && elem.hasAttribute('href')) {
68 document.location = elem.getAttribute('href'); 71 var hrefValue = elem.getAttribute('href');
72 var hrefJavascriptMatch = hrefValue.match(/^\s?javascript\s?:(.*)/i);
73 if (hrefJavascriptMatch) {
74 try {
75 eval(hrefJavascriptMatch[1]);
76 } catch (e) {
77 this.log("click(): Unable to evaluate href javascript contents: " + e, "error");
78 return false;
79 }
80 } else {
81 document.location = hrefValue;
82 }
69 return true; 83 return true;
70 } 84 }
71 return false; 85 return false;
......
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>CasperJS test click</title>
6 </head>
7 <body>
8 <a id="test1" href="javascript:results.test1 = true;">test1</a>
9 <a id="test2" href="#" onclick="results.test2 = true;">test2</a>
10 <a id="test3" href="page1.html" onclick="results.test3 = true; return false">test3</a>
11 <a id="test4" href="page1.html">test4</a>
12 <script>
13 (function(window) {
14 window.results = {
15 test1: false,
16 test2: false,
17 test3: false,
18 test4: false
19 };
20 document.querySelector('#test4').onclick = function(event) {
21 results.test4 = true;
22 event.preventDefault();
23 };
24 })(window);
25 </script>
26 </body>
27 </html>
1 (function(t) { 1 (function(t) {
2 t.comment('Casper.click()');
3
4 casper.start('tests/site/index.html', function(self) { 2 casper.start('tests/site/index.html', function(self) {
5 self.click('a[href="test.html"]'); 3 self.click('a[href="test.html"]');
6 }); 4 });
7 5
8 casper.then(function(self) { 6 casper.then(function(self) {
7 t.comment('Casper.click()');
9 t.assertTitle('CasperJS test target', 'Casper.click() can click on a link'); 8 t.assertTitle('CasperJS test target', 'Casper.click() can click on a link');
10 }).thenClick('a', function(self) { 9 }).thenClick('a', function(self) {
10 t.comment('Casper.thenClick()');
11 t.assertTitle('CasperJS test form', 'Casper.thenClick() can click on a link'); 11 t.assertTitle('CasperJS test form', 'Casper.thenClick() can click on a link');
12 }); 12 });
13 13
14 // onclick variants tests
15 casper.thenOpen('tests/site/click.html', function(self) {
16 t.comment('CasperUtils.click()');
17 self.test.assert(self.click('#test1'), 'CasperUtils.click() can click an `href="javascript:` link');
18 self.test.assert(self.click('#test2'), 'CasperUtils.click() can click an `href="#"` link');
19 self.test.assert(!self.click('#test3'), 'CasperUtils.click() can click an `onclick=".*; return false"` link');
20 self.test.assert(!self.click('#test4'), 'CasperUtils.click() can click an unobstrusive js handled link');
21 var results = self.getGlobal('results');
22 self.test.assert(results.test1, 'CasperUtils.click() has clicked an `href="javascript:` link');
23 self.test.assert(results.test2, 'CasperUtils.click() has clicked an `href="#"` link');
24 self.test.assert(results.test3, 'CasperUtils.click() has clicked an `onclick=".*; return false"` link');
25 self.test.assert(results.test4, 'CasperUtils.click() has clicked an unobstrusive js handled link');
26 });
27
14 casper.run(function(self) { 28 casper.run(function(self) {
15 t.done(); 29 t.done();
16 }); 30 });
17 })(casper.test); 31 })(casper.test);
...\ No newline at end of file ...\ No newline at end of file
......