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 = {
*/
click: function(selector, fallbackToHref) {
fallbackToHref = utils.isType(fallbackToHref, "undefined") ? true : !!fallbackToHref;
this.log("click on selector: " + selector, "debug");
this.log("Click on selector: " + selector, "debug");
return this.evaluate(function(selector, fallbackToHref) {
return __utils__.click(selector, fallbackToHref);
}, {
......@@ -533,14 +533,18 @@ Casper.prototype = {
try {
result.value = JSON.stringify(window[name]);
} catch (e) {
result.error = 'Unable to JSON encode window.' + name + ': ' + e;
var message = 'Unable to JSON encode window.' + name + ': ' + e;
__utils__.log(message, "error");
result.error = message;
}
return result;
}, {'name': name});
if (result.error) {
throw result.error;
} else {
if ('error' in result) {
throw new Error(result.error);
} else if (isType(result.value, "string")) {
return JSON.parse(result.value);
} else {
return undefined;
}
},
......
......@@ -57,15 +57,29 @@
fallbackToHref = typeof fallbackToHref === "undefined" ? true : !!fallbackToHref;
var elem = this.findOne(selector);
if (!elem) {
this.log("click(): Couldn't find any element matching '" + selector + "' selector");
return false;
}
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, elem);
// dispatchEvent return value is false if at least one of the event
// handlers which handled this event called preventDefault
if (elem.dispatchEvent(evt)) {
return true;
}
if (fallbackToHref && elem.hasAttribute('href')) {
document.location = elem.getAttribute('href');
var hrefValue = elem.getAttribute('href');
var hrefJavascriptMatch = hrefValue.match(/^\s?javascript\s?:(.*)/i);
if (hrefJavascriptMatch) {
try {
eval(hrefJavascriptMatch[1]);
} catch (e) {
this.log("click(): Unable to evaluate href javascript contents: " + e, "error");
return false;
}
} else {
document.location = hrefValue;
}
return true;
}
return false;
......
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CasperJS test click</title>
</head>
<body>
<a id="test1" href="javascript:results.test1 = true;">test1</a>
<a id="test2" href="#" onclick="results.test2 = true;">test2</a>
<a id="test3" href="page1.html" onclick="results.test3 = true; return false">test3</a>
<a id="test4" href="page1.html">test4</a>
<script>
(function(window) {
window.results = {
test1: false,
test2: false,
test3: false,
test4: false
};
document.querySelector('#test4').onclick = function(event) {
results.test4 = true;
event.preventDefault();
};
})(window);
</script>
</body>
</html>
(function(t) {
t.comment('Casper.click()');
casper.start('tests/site/index.html', function(self) {
self.click('a[href="test.html"]');
});
casper.then(function(self) {
t.comment('Casper.click()');
t.assertTitle('CasperJS test target', 'Casper.click() can click on a link');
}).thenClick('a', function(self) {
t.comment('Casper.thenClick()');
t.assertTitle('CasperJS test form', 'Casper.thenClick() can click on a link');
});
// onclick variants tests
casper.thenOpen('tests/site/click.html', function(self) {
t.comment('CasperUtils.click()');
self.test.assert(self.click('#test1'), 'CasperUtils.click() can click an `href="javascript:` link');
self.test.assert(self.click('#test2'), 'CasperUtils.click() can click an `href="#"` link');
self.test.assert(!self.click('#test3'), 'CasperUtils.click() can click an `onclick=".*; return false"` link');
self.test.assert(!self.click('#test4'), 'CasperUtils.click() can click an unobstrusive js handled link');
var results = self.getGlobal('results');
self.test.assert(results.test1, 'CasperUtils.click() has clicked an `href="javascript:` link');
self.test.assert(results.test2, 'CasperUtils.click() has clicked an `href="#"` link');
self.test.assert(results.test3, 'CasperUtils.click() has clicked an `onclick=".*; return false"` link');
self.test.assert(results.test4, 'CasperUtils.click() has clicked an unobstrusive js handled link');
});
casper.run(function(self) {
t.done();
});
})(casper.test);
})(casper.test);
\ No newline at end of file
......