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) {
"use strict";
this.checkStarted();
tag = tag || "*";
var escapedLabel = label.toString().replace(/"/g, '\\"');
var selector = selectXPath(f('//%s[text()="%s"]', tag, escapedLabel));
var escapedLabel = utils.quoteXPathAttributeString(label);
var selector = selectXPath(f('//%s[text()=%s]', tag, escapedLabel));
return this.click(selector);
};
......
......@@ -628,6 +628,22 @@ function objectValues(obj) {
exports.objectValues = objectValues;
/**
* Prepares a string for xpath expression with the condition [text()=].
*
* @param String string
* @return String
*/
function quoteXPathAttributeString(string) {
"use strict";
if (/"/g.test(string)) {
return 'concat("' + string.toString().replace(/"/g, '", \'"\', "') + '")';
} else {
return '"' + string + '"';
}
}
exports.quoteXPathAttributeString = quoteXPathAttributeString;
/**
* Serializes a value using JSON.
*
* @param Mixed value
......
......@@ -10,6 +10,8 @@
<a id="test3" href="page1.html" onclick="results.test3 = true; return false">test3</a>
<a id="test4" href="http://www.google.com/">test4</a>
<a id="test5" href="#">test5</a>
<a id="test6" href="#">Label with double "quotes"</a>
<a id="test7" href="#">Label with single 'quotes'</a>
<script>
(function(window) {
window.results = {
......@@ -18,6 +20,8 @@
test3: false,
test4: false,
test5: [],
test6: false,
test7: false,
testdown: [],
testup: [],
testmove: [],
......@@ -47,6 +51,14 @@
test5elem.addEventListener('mouseup', function(event) {
results.test5.push('mouseup');
});
document.querySelector('#test6').onclick = function(event) {
results.test6 = true;
event.preventDefault();
};
document.querySelector('#test7').onclick = function(event) {
results.test7 = true;
event.preventDefault();
};
})(window);
</script>
</body>
......
......@@ -30,7 +30,7 @@ casper.test.begin('onclick variants tests', 8, function(test) {
});
});
casper.test.begin('clickLabel tests tests', 8, function(test) {
casper.test.begin('clickLabel tests tests', 12, function(test) {
casper.start('tests/site/click.html', function() {
test.assert(this.clickLabel('test1'),
'Casper.clickLabel() can click an `href="javascript:` link');
......@@ -40,6 +40,10 @@ casper.test.begin('clickLabel tests tests', 8, function(test) {
'Casper.clickLabel() can click an `onclick=".*; return false"` link');
test.assert(this.clickLabel('test4'),
'Casper.clickLabel() can click an unobstrusive js handled link');
test.assert(this.clickLabel('Label with double "quotes"'),
'Casper.clickLabel() can click the link with double quotes in the label');
test.assert(this.clickLabel("Label with single 'quotes'"),
'Casper.clickLabel() can click the link with the single quotes in the label');
var results = this.getGlobal('results');
test.assert(results.test1,
'Casper.clickLabel() has clicked an `href="javascript:` link');
......@@ -49,6 +53,10 @@ casper.test.begin('clickLabel tests tests', 8, function(test) {
'Casper.clickLabel() has clicked an `onclick=".*; return false"` link');
test.assert(results.test4,
'Casper.clickLabel() has clicked an unobstrusive js handled link');
test.assert(results.test6,
'Casper.clickLabel() has clicked the link with double quotes in the label');
test.assert(results.test7,
'Casper.clickLabel() has clicked the link with single quotes in the label');
}).run(function() {
test.done();
});
......
......@@ -339,6 +339,19 @@ casper.test.begin('objectValues() tests', 2, function(test) {
test.done();
});
casper.test.begin('quoteXPathAttributeString() tests', 2, function(test) {
casper.start('tests/site/click.html', function() {
var selector = utils.format('//a[text()=%s]',
utils.quoteXPathAttributeString('Label with double "quotes"'));
test.assertExists(x(selector), utils.format('Xpath selector "%s" is found on "tests/site/click.html" page', selector));
selector = utils.format('//a[text()=%s]',
utils.quoteXPathAttributeString("Label with single 'quotes'"));
test.assertExists(x(selector), utils.format('Xpath selector "%s" is found on "tests/site/click.html" page', selector));
}).run(function() {
test.done();
});
});
casper.test.begin('unique() tests', 4, function(test) {
var testCases = [
{
......