Commit 12ec6142 12ec61420304146436b26cbb97bba8bc0419b671 by Laurent Jouanneau

Merge pull request #519 from Claud/dev

Fixes an issue: clickLabel didn't support labels containing quotes
2 parents 83c4e4b4 d5877ed5
...@@ -445,8 +445,8 @@ Casper.prototype.clickLabel = function clickLabel(label, tag) { ...@@ -445,8 +445,8 @@ Casper.prototype.clickLabel = function clickLabel(label, tag) {
445 "use strict"; 445 "use strict";
446 this.checkStarted(); 446 this.checkStarted();
447 tag = tag || "*"; 447 tag = tag || "*";
448 var escapedLabel = label.toString().replace(/"/g, '\\"'); 448 var escapedLabel = utils.quoteXPathAttributeString(label);
449 var selector = selectXPath(f('//%s[text()="%s"]', tag, escapedLabel)); 449 var selector = selectXPath(f('//%s[text()=%s]', tag, escapedLabel));
450 return this.click(selector); 450 return this.click(selector);
451 }; 451 };
452 452
......
...@@ -628,6 +628,22 @@ function objectValues(obj) { ...@@ -628,6 +628,22 @@ function objectValues(obj) {
628 exports.objectValues = objectValues; 628 exports.objectValues = objectValues;
629 629
630 /** 630 /**
631 * Prepares a string for xpath expression with the condition [text()=].
632 *
633 * @param String string
634 * @return String
635 */
636 function quoteXPathAttributeString(string) {
637 "use strict";
638 if (/"/g.test(string)) {
639 return 'concat("' + string.toString().replace(/"/g, '", \'"\', "') + '")';
640 } else {
641 return '"' + string + '"';
642 }
643 }
644 exports.quoteXPathAttributeString = quoteXPathAttributeString;
645
646 /**
631 * Serializes a value using JSON. 647 * Serializes a value using JSON.
632 * 648 *
633 * @param Mixed value 649 * @param Mixed value
......
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
10 <a id="test3" href="page1.html" onclick="results.test3 = true; return false">test3</a> 10 <a id="test3" href="page1.html" onclick="results.test3 = true; return false">test3</a>
11 <a id="test4" href="http://www.google.com/">test4</a> 11 <a id="test4" href="http://www.google.com/">test4</a>
12 <a id="test5" href="#">test5</a> 12 <a id="test5" href="#">test5</a>
13 <a id="test6" href="#">Label with double "quotes"</a>
14 <a id="test7" href="#">Label with single 'quotes'</a>
13 <script> 15 <script>
14 (function(window) { 16 (function(window) {
15 window.results = { 17 window.results = {
...@@ -18,6 +20,8 @@ ...@@ -18,6 +20,8 @@
18 test3: false, 20 test3: false,
19 test4: false, 21 test4: false,
20 test5: [], 22 test5: [],
23 test6: false,
24 test7: false,
21 testdown: [], 25 testdown: [],
22 testup: [], 26 testup: [],
23 testmove: [], 27 testmove: [],
...@@ -47,6 +51,14 @@ ...@@ -47,6 +51,14 @@
47 test5elem.addEventListener('mouseup', function(event) { 51 test5elem.addEventListener('mouseup', function(event) {
48 results.test5.push('mouseup'); 52 results.test5.push('mouseup');
49 }); 53 });
54 document.querySelector('#test6').onclick = function(event) {
55 results.test6 = true;
56 event.preventDefault();
57 };
58 document.querySelector('#test7').onclick = function(event) {
59 results.test7 = true;
60 event.preventDefault();
61 };
50 })(window); 62 })(window);
51 </script> 63 </script>
52 </body> 64 </body>
......
...@@ -30,7 +30,7 @@ casper.test.begin('onclick variants tests', 8, function(test) { ...@@ -30,7 +30,7 @@ casper.test.begin('onclick variants tests', 8, function(test) {
30 }); 30 });
31 }); 31 });
32 32
33 casper.test.begin('clickLabel tests tests', 8, function(test) { 33 casper.test.begin('clickLabel tests tests', 12, function(test) {
34 casper.start('tests/site/click.html', function() { 34 casper.start('tests/site/click.html', function() {
35 test.assert(this.clickLabel('test1'), 35 test.assert(this.clickLabel('test1'),
36 'Casper.clickLabel() can click an `href="javascript:` link'); 36 'Casper.clickLabel() can click an `href="javascript:` link');
...@@ -40,6 +40,10 @@ casper.test.begin('clickLabel tests tests', 8, function(test) { ...@@ -40,6 +40,10 @@ casper.test.begin('clickLabel tests tests', 8, function(test) {
40 'Casper.clickLabel() can click an `onclick=".*; return false"` link'); 40 'Casper.clickLabel() can click an `onclick=".*; return false"` link');
41 test.assert(this.clickLabel('test4'), 41 test.assert(this.clickLabel('test4'),
42 'Casper.clickLabel() can click an unobstrusive js handled link'); 42 'Casper.clickLabel() can click an unobstrusive js handled link');
43 test.assert(this.clickLabel('Label with double "quotes"'),
44 'Casper.clickLabel() can click the link with double quotes in the label');
45 test.assert(this.clickLabel("Label with single 'quotes'"),
46 'Casper.clickLabel() can click the link with the single quotes in the label');
43 var results = this.getGlobal('results'); 47 var results = this.getGlobal('results');
44 test.assert(results.test1, 48 test.assert(results.test1,
45 'Casper.clickLabel() has clicked an `href="javascript:` link'); 49 'Casper.clickLabel() has clicked an `href="javascript:` link');
...@@ -49,6 +53,10 @@ casper.test.begin('clickLabel tests tests', 8, function(test) { ...@@ -49,6 +53,10 @@ casper.test.begin('clickLabel tests tests', 8, function(test) {
49 'Casper.clickLabel() has clicked an `onclick=".*; return false"` link'); 53 'Casper.clickLabel() has clicked an `onclick=".*; return false"` link');
50 test.assert(results.test4, 54 test.assert(results.test4,
51 'Casper.clickLabel() has clicked an unobstrusive js handled link'); 55 'Casper.clickLabel() has clicked an unobstrusive js handled link');
56 test.assert(results.test6,
57 'Casper.clickLabel() has clicked the link with double quotes in the label');
58 test.assert(results.test7,
59 'Casper.clickLabel() has clicked the link with single quotes in the label');
52 }).run(function() { 60 }).run(function() {
53 test.done(); 61 test.done();
54 }); 62 });
......
...@@ -339,6 +339,19 @@ casper.test.begin('objectValues() tests', 2, function(test) { ...@@ -339,6 +339,19 @@ casper.test.begin('objectValues() tests', 2, function(test) {
339 test.done(); 339 test.done();
340 }); 340 });
341 341
342 casper.test.begin('quoteXPathAttributeString() tests', 2, function(test) {
343 casper.start('tests/site/click.html', function() {
344 var selector = utils.format('//a[text()=%s]',
345 utils.quoteXPathAttributeString('Label with double "quotes"'));
346 test.assertExists(x(selector), utils.format('Xpath selector "%s" is found on "tests/site/click.html" page', selector));
347 selector = utils.format('//a[text()=%s]',
348 utils.quoteXPathAttributeString("Label with single 'quotes'"));
349 test.assertExists(x(selector), utils.format('Xpath selector "%s" is found on "tests/site/click.html" page', selector));
350 }).run(function() {
351 test.done();
352 });
353 });
354
342 casper.test.begin('unique() tests', 4, function(test) { 355 casper.test.begin('unique() tests', 4, function(test) {
343 var testCases = [ 356 var testCases = [
344 { 357 {
......