Well, CasperJS solves this kind of problem using a convenient API
for dealing with asynchronous stuff:

l

l

API docummentation
What is PhantomJS?
PhantomJS is a headless WebKit with a JavaScript API. It has fast and native support
for various web standards: DOM handling, CSS selectors, JSON, Canvas and SVG.
Capturing screenshots
Capturing a screenshot of a given area is as easy as this:

l
Filling and handling forms
Filling and submitting a form is not much harder:

l
Implementation for digaku2
For all page

l
Output :
Inference
Using casperjs more easy & powerfull than javascript, because casperjs directly call
API of the library
API docummentation
Installation
Thank you... :D

casperjs presentation