@@ -13,669 +13,4 @@ Casper.js is a navigation utility for [PhantomJS](http://www.phantomjs.org/). It
Feel free to browse our [sample examples repository](https://github.com/n1k0/casperjs/tree/master/samples). Don't hesitate to pull request for any cool example of yours as well!
## Quickstart
In the following example, we'll query google for two terms consecutively, `capserjs` and `phantomjs`, and aggregate the result links in a standard Array. Then we'll output the result to the console:
``` javascript
phantom.injectJs('casper.js');
functiongetLinks(){
varlinks=document.querySelectorAll('h3.r a');
returnArray.prototype.map.call(links,function(e){
return{
title:e.innerText,
href:e.getAttribute('href')
};
});
}
varlinks=[];
varcasper=newphantom.Casper({
logLevel:"info",// we only want "info" or higher level log messages
loadImages:false,// do not download images to save bandwidth
loadPlugins:false,// do not load plugins to save kitten
verbose:true// write log messages to the console
})
.start('http://google.fr/')
.then(function(self){
// search for 'casperjs' from google form
self.fill('form[name=f]',{
q:'casperjs'
},true);
})
.then(function(self){
// aggregate results for the 'casperjs' search
links=self.evaluate(getLinks);
// now search for 'phantomjs' by fillin the form again
self.fill('form[name=f]',{
q:'phantomjs'
},true);
})
.then(function(self){
// aggregate results for the 'phantomjs' search
links=links.concat(self.evaluate(getLinks));
})
.run(function(self){
// echo results in some pretty fashion
self.echo(links.map(function(i){
returni.title+' ('+i.href+')';
}).join('\n')).exit();
})
;
```
**Hint:** Method chaining is not mandatory but provided as an alternative way to structure your code.
Paris JS #10 : Introduction à PhantomJS, un navigateur webkit ... (http://svay.com/blog/index/post/2011/08/31/Paris-JS-10-%3A-Introduction-%C3%A0-PhantomJS,-un-navigateur-webkit-headless)
Proxy method for PhantomJS' `WebPage#render`. Adds a clipRect parameter for automatically setting page clipRect setting values and sets it back once done.
Evaluates an expression in the page context, a bit like what PhantomJS' `WebPage#evaluate` does, but can also replace values by their placeholer names.
Logs a message with an optional level in an optional space. Available levels are `debug`, `info`, `warning` and `error`. A space is a kind of namespace you can set for filtering your logs. By default, Casper logs messages in two distinct spaces: `phantom` and `remote`, to distinguish what happens in the PhantomJS environment from the remote one.
Runs the whole suite of steps and optionally executes a callback when they've all been done. Obviously, **calling this method is mandatory** in order to run the Casper navigation suite.
Configures and starts Casper, then open the provided `url` and optionnaly adds the step provided by the `then` argument.
Example:
``` javascript
casper.start('http://google.fr/',function(self){
self.echo("I'm loaded.");
}).run(function(){
self.exit();
});
```
Alternatively:
``` javascript
casper.start('http://google.fr/');
casper.then(function(self){
self.echo("I'm loaded.");
});
casper.run(function(self){
self.exit();
});
```
Or alternatively:
``` javascript
casper.start('http://google.fr/');
casper.then(function(){
casper.echo("I'm loaded.");
});
casper.run(function(){
casper.exit();
});
```
Matter of taste!
Please note that **you must call the `start()` method in order to be able to add navigation steps** and run the suite. If you don't you'll get an error message inviting you to do so anyway.
### Casper#then(function fn)
The standard way to add a new navigation step to the Casper suite by provide a callback function which will be executed when the requested page is loaded.
Casper ships with a few client-side utilitites which are injected in the remote DOM environement, and accessible from there through the `__utils__` object instance of the `phantom.Casper.ClientUtils` class.
### CasperUtils#getBase64(String url)
This method will retrieved a base64 encoded version of any resource behind an url. For example, let's imagine we want to retrieve the base64 representation of some website's logo:
Sometimes it can be convenient to add your own methods to the `Casper` class; it's easily doable using the `Casper.extend()` method as illustrated below:
CasperJS has some unit and functional tests, located in the `tests` subfolder. More tests will be added in the future. To run the test suite, from the root of a checkout of the casperjs repository:
$ phantomjs tests/run.js
## Licensing
`Casper.js` is released under the terms of the [MIT license](http://en.wikipedia.org/wiki/MIT_License).
## Now what
Feel free to play with the code and [report an issue on github](https://github.com/n1k0/casperjs/issues). I'm also reachable [on twitter](https://twitter.com/n1k0).
**Read the [full documentation](http://n1k0.github.com/casperjs/) on casperjs dedicated website.**