CasperJS
-
Upload
learningtech -
Category
Documents
-
view
107 -
download
3
description
Transcript of CasperJS
![Page 1: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/1.jpg)
CasperJS
Howard 2013/12/22
![Page 2: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/2.jpg)
CasperJS
CasperJS is an open source navigation scripting & testing utility written in Javascript for the PhantomJS WebKit headless browser and SlimerJS (Gecko).
It eases the process of defining a full navigation scenario and provides useful high-level functions, methods & syntactic sugar
![Page 3: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/3.jpg)
Doing common tasks
such as: defining & ordering browsing navigation steps
filling & submitting forms
clicking & following links
capturing screenshots of a page (or part of it)
testing remote DOM
logging events
downloading resources, including binary ones
writing functional test suites, saving results as JUnit XML
scraping Web contents
![Page 4: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/4.jpg)
Installation
Prerequisites PhantomJS
PhantomJS is a headless WebKit scriptable with a JavaScript API.
It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.
http://phantomjs.org/download.html
CasperJS
![Page 5: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/5.jpg)
目標
列舉出目前 IPTECH TW 的目前可檢索的站台
![Page 6: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/6.jpg)
A minimal scraping script
What did we just do? we created a new Casper instance
we started it and opened http://casperjs.org/
once the page has been loaded, we asked to print the title of that webpage (the content of its <title> tag)
then we opened another url, http://phantomjs.org/
once the new page has been loaded, we asked to print its title too
we executed the whole process
![Page 7: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/7.jpg)
start(), then(), run(), exit()
start()
Signature: start(String url [, Function then])
Configures and starts Casper, then open the provided url and optionally adds the step provided by the then argument
then()
Signature: then(Function then)
run()
Signature: run(fn onComplete[, int time])
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.
exit()
Signature: exit([int status])
Exits PhantomJS with an optional exit status code.
Note that the current Casper instance automatically binds the this keyword for you within step functions
![Page 8: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/8.jpg)
start(), then(), run(), exit()
![Page 9: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/9.jpg)
start(), then(), run(), exit()
?
![Page 10: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/10.jpg)
casper module – fill
Signature: fill(String selector, Object values[, Boolean submit])
Fills the fields of a form with given values and optionally submits it. Fields are referenced by their name attribute.
![Page 11: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/11.jpg)
click
click(Number x, Number y)
click(String selector)
Performs a click on the first element found matching the provided selector expression or at given coordinates if two numbers are passed
selector expression CSS3 selector strings
Xpath expressions
![Page 12: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/12.jpg)
evaluate
Signature: evaluate(Function fn[, arg1[, arg2[, …]]])
Evaluates an expression in the current page DOM context
![Page 13: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/13.jpg)
Understanding evaluate()
Think of the evaluate() method as a gate between the CasperJS environment and the one of the page you have opened;
Every time you pass a closure to evaluate(), you’re entering the page and execute code as if you were using the browser console.
![Page 14: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/14.jpg)
waitForSelector
Signature: waitForSelector(String selector[, Function then, Function onTimeout, Number timeout])
Waits until an element matching the provided selector expression exists in remote DOM to process any next step
![Page 15: CasperJS](https://reader036.fdocuments.in/reader036/viewer/2022082805/54c6846e4a795997128b45c9/html5/thumbnails/15.jpg)
references
http://docs.casperjs.org/en/latest/quickstart.html
http://docs.casperjs.org/en/latest/modules/index.html
http://docs.casperjs.org/en/latest/modules/casper.html#start
http://docs.casperjs.org/en/latest/modules/casper.html#then
http://docs.casperjs.org/en/latest/modules/casper.html#run
http://docs.casperjs.org/en/latest/modules/casper.html#exit
http://docs.casperjs.org/en/latest/modules/casper.html#fill
http://docs.casperjs.org/en/latest/selectors.html
http://docs.casperjs.org/en/latest/modules/mouse.html#click