enhanced documentation and sample examples
Showing
2 changed files
with
51 additions
and
111 deletions
... | @@ -13,7 +13,7 @@ Casper.js is a navigation utility for [PhantomJS](http://www.phantomjs.org/). It | ... | @@ -13,7 +13,7 @@ Casper.js is a navigation utility for [PhantomJS](http://www.phantomjs.org/). It |
13 | 13 | ||
14 | ## Quickstart | 14 | ## Quickstart |
15 | 15 | ||
16 | In the following example, we'll query google for two terms consecutively, `capser` and `homer`, and aggregate the result links in a standard Array. Running the script will output a standard JSON string containing both the logs and the results: | 16 | 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: |
17 | 17 | ||
18 | ``` javascript | 18 | ``` javascript |
19 | function getLinks() { | 19 | function getLinks() { |
... | @@ -28,30 +28,35 @@ function getLinks() { | ... | @@ -28,30 +28,35 @@ function getLinks() { |
28 | 28 | ||
29 | var links = []; | 29 | var links = []; |
30 | var casper = new phantom.Casper({ | 30 | var casper = new phantom.Casper({ |
31 | logLevel: "debug", | 31 | logLevel: "info", // we only want "info" or higher level log messages |
32 | verbose: true | 32 | loadImages: false, // do not download images to save bandwidth |
33 | loadPlugins: false, // do not load plugins to save kitten | ||
34 | verbose: true // write log messages to the console | ||
33 | }) | 35 | }) |
34 | .start('http://google.fr/') | 36 | .start('http://google.fr/') |
35 | .then(function(self) { | 37 | .then(function(self) { |
38 | // search for 'casperjs' from google form | ||
36 | self.fill('form[name=f]', { | 39 | self.fill('form[name=f]', { |
37 | q: 'casperjs' | 40 | q: 'casperjs' |
38 | }, true); | 41 | }, true); |
39 | }) | 42 | }) |
40 | .then(function(self) { | 43 | .then(function(self) { |
44 | // aggregate results for the 'casperjs' search | ||
41 | links = self.evaluate(getLinks); | 45 | links = self.evaluate(getLinks); |
46 | // now search for 'phantomjs' by fillin the form again | ||
42 | self.fill('form[name=f]', { | 47 | self.fill('form[name=f]', { |
43 | q: 'plop' | 48 | q: 'phantomjs' |
44 | }, true); | 49 | }, true); |
45 | }) | 50 | }) |
46 | .then(function(self) { | 51 | .then(function(self) { |
52 | // aggregate results for the 'phantomjs' search | ||
47 | links = links.concat(self.evaluate(getLinks)); | 53 | links = links.concat(self.evaluate(getLinks)); |
48 | }) | 54 | }) |
49 | .run(function(self) { | 55 | .run(function(self) { |
50 | self.echo(JSON.stringify({ | 56 | // echo results in some pretty fashion |
51 | result: self.result, | 57 | self.echo(links.map(function(i) { |
52 | links: links | 58 | return i.title + ' (' + i.href + ')'; |
53 | }, null, ' ')); | 59 | }).join('\n')).exit(); |
54 | self.exit(); | ||
55 | }) | 60 | }) |
56 | ; | 61 | ; |
57 | ``` | 62 | ``` |
... | @@ -60,104 +65,39 @@ var casper = new phantom.Casper({ | ... | @@ -60,104 +65,39 @@ var casper = new phantom.Casper({ |
60 | Run it: | 65 | Run it: |
61 | 66 | ||
62 | $ phantomjs example.js | 67 | $ phantomjs example.js |
63 | { | 68 | [info] [phantom] Starting… |
64 | "result": { | 69 | [info] [phantom] Running suite: 3 steps |
65 | "log": [ | 70 | [info] [phantom] Step 1/3: http://www.google.fr/ (HTTP 301) |
66 | { | 71 | [info] [remote] set "q" value to casperjs |
67 | "level": "info", | 72 | [info] [remote] submitting form to /search, HTTP GET |
68 | "space": "phantom", | 73 | [info] [phantom] Step 1/3: done in 1592ms. |
69 | "message": "Starting…", | 74 | [info] [phantom] Step 2/3: http://www.google.fr/search?sclient=psy-ab&hl=fr&site=&source=hp&q=casperjs&pbx=1&oq=&aq=&aqi=&aql=&gs_sm=&gs_upl= (HTTP 301) |
70 | "date": "Mon Sep 05 2011 16:10:56 GMT+0200 (CEST)" | 75 | [info] [remote] set "q" value to phantomjs |
71 | }, | 76 | [info] [remote] submitting form to /search, HTTP GET |
72 | { | 77 | [info] [phantom] Step 2/3: done in 3091ms. |
73 | "level": "info", | 78 | [info] [phantom] Step 3/3: http://www.google.fr/search?sclient=psy-ab&hl=fr&source=hp&q=phantomjs&pbx=1&oq=&aq=&aqi=&aql=&gs_sm=&gs_upl= (HTTP 301) |
74 | "space": "phantom", | 79 | [info] [phantom] Step 3/3: done in 3862ms. |
75 | "message": "Running suite: 4 steps", | 80 | [info] [phantom] Done 3 steps in 4111ms. |
76 | "date": "Mon Sep 05 2011 16:10:56 GMT+0200 (CEST)" | 81 | n1k0/casperjs - GitHub (https://github.com/n1k0/casperjs) |
77 | }, | 82 | #2: Some functionality has broken due to 1.3 update - Issues - n1k0 ... (https://github.com/n1k0/casperjs/issues/2) |
78 | { | 83 | Commit History for n1k0/casperjs - GitHub (https://github.com/n1k0/casperjs/commits/master) |
79 | "level": "info", | 84 | #1: Way to step forward and backwards - Issues - n1k0/casperjs ... (https://github.com/n1k0/casperjs/issues/1) |
80 | "space": "phantom", | 85 | Casper Js | Facebook (http://www.facebook.com/people/Casper-Js/100000337260665) |
81 | "message": "Step 1/4: http://www.google.fr/ (HTTP 301)", | 86 | Casper Js Profiles | Facebook (http://www.facebook.com/public/Casper-Js) |
82 | "date": "Mon Sep 05 2011 16:10:57 GMT+0200 (CEST)" | 87 | hashtags.org - CasperJS (http://hashtags.org/tag/CasperJS/) |
83 | }, | 88 | Zerotohundred.com - View Profile: Casper JS (http://www.zerotohundred.com/newforums/members/casper-js.html) |
84 | { | 89 | J S Enterprises in Casper, WY | Casper J S Enterprises - YP.com (http://www.yellowpages.com/casper-wy/j-s-enterprises) |
85 | "level": "info", | 90 | Best Guitat Backing Traks Free Download: ICFMeister, Handy Backup ... (http://www.softwaregeek.com/guitat-backing-traks/p2.html) |
86 | "space": "phantom", | 91 | PhantomJS: Headless WebKit with JavaScript API (http://www.phantomjs.org/) |
87 | "message": "Step 1/4: done in 1259ms.", | 92 | phantomjs - headless WebKit with JavaScript API - Google Project ... (http://code.google.com/p/phantomjs/) |
88 | "date": "Mon Sep 05 2011 16:10:57 GMT+0200 (CEST)" | 93 | QuickStart - phantomjs - 5-Minute Guide - headless WebKit with ... (http://code.google.com/p/phantomjs/wiki/QuickStart) |
89 | }, | 94 | 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) |
90 | { | 95 | ariya/phantomjs - GitHub (https://github.com/ariya/phantomjs) |
91 | "level": "info", | 96 | twitter.com/search/%23%23PhantomJS/grid (http://twitter.com/search/%23%23PhantomJS/grid) |
92 | "space": "phantom", | 97 | Phantom.js | Pilvee blog (http://pilvee.com/blog/tag/phantom-js/) |
93 | "message": "Step 2/4: http://www.google.fr/search?sclient=psy&hl=fr&site=&source=hp&q=casper&pbx=1&oq=&aq=&aqi=&aql=&gs_sm=&gs_upl= (HTTP 301)", | 98 | don't code today what you can't debug tomorrow: PhantomJS ... (http://ariya.blogspot.com/2011/01/phantomjs-minimalistic-headless-webkit.html) |
94 | "date": "Mon Sep 05 2011 16:10:58 GMT+0200 (CEST)" | 99 | DailyJS: PhantomJS, load.js, Phantom Limb, OpenOdyssey (http://dailyjs.com/2011/01/28/phantoms/) |
95 | }, | 100 | PhantomJS: The Power of WebKit but Without the Broswer (http://www.readwriteweb.com/hack/2011/03/phantomjs-the-power-of-webkit.php) |
96 | { | ||
97 | "level": "info", | ||
98 | "space": "phantom", | ||
99 | "message": "Step 2/4: done in 2145ms.", | ||
100 | "date": "Mon Sep 05 2011 16:10:58 GMT+0200 (CEST)" | ||
101 | }, | ||
102 | { | ||
103 | "level": "info", | ||
104 | "space": "phantom", | ||
105 | "message": "Step 3/4: http://www.google.fr/search?sclient=psy&hl=fr&site=&source=hp&q=casper&pbx=1&oq=&aq=&aqi=&aql=&gs_sm=&gs_upl= (HTTP 301)", | ||
106 | "date": "Mon Sep 05 2011 16:10:58 GMT+0200 (CEST)" | ||
107 | }, | ||
108 | { | ||
109 | "level": "info", | ||
110 | "space": "phantom", | ||
111 | "message": "Step 3/4: done in 2390ms.", | ||
112 | "date": "Mon Sep 05 2011 16:10:58 GMT+0200 (CEST)" | ||
113 | }, | ||
114 | { | ||
115 | "level": "info", | ||
116 | "space": "phantom", | ||
117 | "message": "Step 4/4: http://www.google.fr/search?sclient=psy&hl=fr&source=hp&q=homer&pbx=1&oq=&aq=&aqi=&aql=&gs_sm=&gs_upl= (HTTP 301)", | ||
118 | "date": "Mon Sep 05 2011 16:10:59 GMT+0200 (CEST)" | ||
119 | }, | ||
120 | { | ||
121 | "level": "info", | ||
122 | "space": "phantom", | ||
123 | "message": "Step 4/4: done in 3077ms.", | ||
124 | "date": "Mon Sep 05 2011 16:10:59 GMT+0200 (CEST)" | ||
125 | }, | ||
126 | { | ||
127 | "level": "info", | ||
128 | "space": "phantom", | ||
129 | "message": "Done 4 steps in 3077ms.", | ||
130 | "date": "Mon Sep 05 2011 16:10:59 GMT+0200 (CEST)" | ||
131 | } | ||
132 | ], | ||
133 | "status": "success", | ||
134 | "time": 3077 | ||
135 | }, | ||
136 | "links": [ | ||
137 | "http://fr.wikipedia.org/wiki/Casper_le_gentil_fant%C3%B4me", | ||
138 | "http://fr.wikipedia.org/wiki/Casper", | ||
139 | "http://casperflights.com/", | ||
140 | "http://www.allocine.fr/film/fichefilm_gen_cfilm=13018.html", | ||
141 | "/search?q=casper&hl=fr&prmd=ivns&tbm=isch&tbo=u&source=univ&sa=X&ei=cdhkTurpFa364QTB5uGeCg&ved=0CFkQsAQ", | ||
142 | "http://www.youtube.com/watch?v=Kuvo0QMiNEE", | ||
143 | "http://www.youtube.com/watch?v=W7cW5YlHaeQ", | ||
144 | "http://www.imdb.com/title/tt0112642/", | ||
145 | "http://blog.caspie.net/", | ||
146 | "http://www.casperwy.gov/", | ||
147 | "http://www.lequipe.fr/Cyclisme/CyclismeFicheCoureur147.html", | ||
148 | "http://homer-simpson-tv.blog4ever.com/", | ||
149 | "http://fr.wikipedia.org/wiki/Homer_Simpson", | ||
150 | "http://en.wikipedia.org/wiki/Homer", | ||
151 | "/search?q=homer&hl=fr&prmd=ivnsb&tbm=isch&tbo=u&source=univ&sa=X&ei=cthkTr73Hefh4QSUmt3UCg&ved=0CEQQsAQ", | ||
152 | "http://www.youtube.com/watch?v=Ajd08hgerRo", | ||
153 | "http://www.koreus.com/video/homer-simpson-photo-39-ans.html", | ||
154 | "http://www.nrel.gov/homer/", | ||
155 | "http://www.luds.net/homer.php", | ||
156 | "http://www.thesimpsons.com/bios/bios_family_homer.htm", | ||
157 | "http://www.homeralaska.org/", | ||
158 | "http://homeralaska.com/" | ||
159 | ] | ||
160 | } | ||
161 | 101 | ||
162 | ## CoffeeScript | 102 | ## CoffeeScript |
163 | 103 | ... | ... |
... | @@ -12,10 +12,10 @@ function getLinks() { | ... | @@ -12,10 +12,10 @@ function getLinks() { |
12 | 12 | ||
13 | var links = []; | 13 | var links = []; |
14 | var casper = new phantom.Casper({ | 14 | var casper = new phantom.Casper({ |
15 | logLevel: "debug", | 15 | logLevel: "info", // we only want "info" or higher level log messages |
16 | loadImages: false, | 16 | loadImages: false, // do not download images to save bandwidth |
17 | loadPlugins: false, | 17 | loadPlugins: false, // do not load plugins to save kitten |
18 | verbose: true | 18 | verbose: true // write log messages to the console |
19 | }) | 19 | }) |
20 | .start('http://google.fr/') | 20 | .start('http://google.fr/') |
21 | .then(function(self) { | 21 | .then(function(self) { | ... | ... |
-
Please register or sign in to post a comment