Commit 9ca9c9e8 9ca9c9e80106dbadaf998abe6512c564457f4900 by Nicolas Perriault

added Casper#click() method to emulate a click event on any element behind a CSS selector

1 parent f6f0ff70
......@@ -256,6 +256,32 @@ casper.start('http://www.google.fr/', function(self) {
});
```
### Casper#click(String selector)
Emulates a click on the element from the provided selector, if possible. In case of success, `true` is returned.
Example:
```javascript
casper.start('http://google.fr/')
.thenEvaluate(function() {
document.querySelector('input[name="q"]').setAttribute('value', '%term%');
document.querySelector('form[name="f"]').submit();
}, {
term: 'CasperJS'
})
.then(function(self) {
// Click on 1st result link
if (self.click('h3.r a')) {
console.log('clicked ok')
}
})
.run(function(self) {
self.debugPage();
})
;
```
### Casper#capture(String targetFilepath, Object clipRect)
Proxy method for PhantomJS' `WebPage#render`. Adds a clipRect parameter for automatically setting page clipRect setting values and sets it back once done.
......
......@@ -172,6 +172,41 @@
},
/**
* Emulates a click on the element from the provided selector, if
* possible. In case of success, `true` is returned.
*
* @param String selector A DOM CSS3 compatible selector
* @return Boolean
*/
click: function(selector) {
this.log("click on selector: " + selector, "debug");
return this.evaluate(function() {
var s = '%selector%';
try {
var elem = document.querySelector(s);
} catch (e) {
console.log('invalid selector: ' + s);
return false;
}
if (!elem) {
console.log('selector "' + s + '" did not find any matching element');
return false;
}
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, elem);
if (elem.dispatchEvent(evt)) {
return true;
}
if (elem.hasAttribute('href')) {
document.location = elem.getAttribute('href');
return true;
}
}, {
selector: selector.replace("'", "\'")
});
},
/**
* Logs the HTML code of the current page.
*
* @return Casper
......@@ -581,23 +616,6 @@
}
/**
* Checks if the provided var is a WebPage instance
*
* @param mixed what
* @return Boolean
*/
function isWebPage(what) {
if (!what || typeof(what) !== "object") {
return false;
}
if (phantom.version.major <= 1 && phantom.version.minor < 3) {
return what instanceof WebPage;
} else {
return what.toString().indexOf('WebPage(') === 0;
}
}
/**
* Object recursive merging utility.
*
* @param object obj1 the destination object
......@@ -639,4 +657,21 @@
}
return fn;
}
/**
* Checks if the provided var is a WebPage instance
*
* @param mixed what
* @return Boolean
*/
function isWebPage(what) {
if (!what || typeof(what) !== "object") {
return false;
}
if (phantom.version.major <= 1 && phantom.version.minor < 3) {
return what instanceof WebPage;
} else {
return what.toString().indexOf('WebPage(') === 0;
}
}
})(phantom);
......
......@@ -6,14 +6,15 @@ function q() {
}
function getLinks() {
return Array.prototype.map.call(document.querySelectorAll('h3.r a'), function(e) {
var links = document.querySelectorAll('h3.r a');
return [].map.call(links, function(e) {
return e.getAttribute('href');
});
}
var links = [];
var casper = new phantom.Casper({
logLevel: "info",
logLevel: "debug",
verbose: true
})
.start('http://google.fr/')
......@@ -28,6 +29,10 @@ var casper = new phantom.Casper({
})
.then(function(self) {
links = links.concat(self.evaluate(getLinks));
self.log("Click on 1st result link").click('h3.r a');
})
.then(function(self) {
self.debugPage();
})
.run(function(self) {
self.echo(JSON.stringify({
......