Skip to content

Instantly share code, notes, and snippets.

@n1k0
Created February 14, 2012 23:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n1k0/1831787 to your computer and use it in GitHub Desktop.
Save n1k0/1831787 to your computer and use it in GitHub Desktop.
Casper script
$ casperjs event.js
[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step 2/3 file:///Users/niko/Sites/casperjs/test.html (HTTP 0)
[debug] [phantom] start page is loaded
[info] [phantom] Step 2/3: done in 207ms.
[info] [phantom] Step 3/3 file:///Users/niko/Sites/casperjs/test.html (HTTP 0)
[info] [phantom] Step 3/3: done in 310ms.
[info] [phantom] Done 3 steps in 406ms
[info] [remote] __event
A click occured!
<!doctype html>
<html>
<head>
<title>Hey.</title>
<script type="text/javascript">
function simulateClick(selector) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
var cb = document.querySelector(selector);
cb.dispatchEvent(evt)
console.log('clicked')
}
setTimeout(function() {
simulateClick("input");
}, 1000);
</script>
</head>
<body>
<h1>Hello</h1>
<input type="checkbox"/>
</body>
</html>
var casper = require('casper').create({
verbose: true,
logLevel: "debug"
});
var url = 'file://' + require('fs').workingDirectory + '/test.html';
casper.on('remote.message', function(message) {
if (message === "__event") {
this.echo('A click occured!').exit();
}
});
casper.start(url).thenEvaluate(function() {
var onclick = function(evt) {
console.log('__event');
};
window.addEventListener("click", onclick, true);
});
casper.run(function(){});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment