-
Notifications
You must be signed in to change notification settings - Fork 16
/
casperjs.node_cli.txt
327 lines (279 loc) · 20.8 KB
/
casperjs.node_cli.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
CASPERJS
casperjs FILE [ARGS...] #Based on PhantomJS, but easier (prefer it).
#Node module 1.1.0
#Can use all PhantomJS command line options
--engine=phantomjs|slimerjs #Can use SlimerJS instead (same API as PhantomJS but Gecko engine instead of WebKit, and is
#not headerless)
require("spooky") #Node module (0.2.4)
#Makes it possible to use CasperJS as a Node module.
#Works by communicating with CasperJS process using RPC means
var SPOOKYVAR = new SPOOKY(OBJ, #OBJ has members:
FUNC(ERROR)) # - child { transport "http|stdio" }: use "http" if "stdio" does not work (def)
# - casper OBJ: command line options to CasperJS camelcased
#FUNC() can use SPOOKYVAR with only some CASPER.* functions:
# - options.casper (like CASPER.options)
# - start, run, create, open, then[Click|Evaluate|Open[AndEvaluate]]
# - back, forward
# - waitFor[Popup|Resource|Selector]|UntilVisible|WhileSelector|Visible
# - userAgent
# - withFrame|Popup
#Different environments:
# - Functions fired by FUNC(), e.g. SPOOKYVAR.then(FUNC2) run in CasperJS process
# (i.e. isolated JavaScript environment, just like the webpage environment)
# - Can pass variables from Node.js environment to CasperJS env using a last argument OBJ,
# e.g. instead of CASPER.FUNC(...), SPOOKYVAR.FUNC(..., OBJ)
SPOOKYVAR.on("error",FUNC(EROR))#
SPOOKYVR.on("console",FUNC(STR))#CasperJS console
SPOOKYVAR.on("log", FUNC(OBJ)) #Members space STR, level STR and message VAL
require("casper").create([OBJ]) #Returns CASPER (implicit everywhere below)
#OBJ are CASPER.options
cli.args #STR_ARR (arguments without dash)
cli.options #OBJ (arguments with dash), includes two implicit ones "cli" and "casper-path"
cli.has(VAL) #
cli.get(VAR) #
cli.drop(VAR) #
log(STR[, STR2[, STR3]]) #Like console.log (but Node-specific so not available), but as:
#[STR2] [STR3] STR (use colors)
# - STR2 is level: "debug|info|warning|error" (déf: "debug").
# logLevel (def: "error") filters the output.
# Can also use casperjs --log-level=STR
# - STR is message
# - STR3 is space
#Only printed if options.verbose true (can also use casperjs --verbose)
#Output also available as CASPER.logs OBJ
on("log", FUNC(OBJ)) #EVENT_FILTER "log.message" FUNC(STR)
#OBJ has members level, space, message, date DATE_STR
echo(STR, STR2) #Same but without prepending anything (only use if needed), and colors according to STR2
#"ERROR|INFO|TRACE|PARAMETER|COMMENT|WARNING|GREEN_BAR|RED_BAR|INFO_BAR|WARN_BAR"
#EVENT_FILTER "echo.message" FUNC(STR)
require("utils").dump(OBJ) #Like echo() but for an OBJ
EVENTS ==> # - on("ev.ent",FUNC(...)) can also be fired as options.onEvEnt = FUNC(CASPER,...)
# - setFilter(EVENT_FITER, FUNC(VAL[, ...])): extension of EVENTEMITTER modifying the VAL
# emitted on a given EVENT (might be different name than EVENT_FILTER) by returning VAL in
# FUNC()
# Returns true if success
WEBPAGE #PhantomJS WEBPAGE
RESPONSE #OBJ with members id NUM, url STR, time DATE, headers OBJ_ARR (OBJ is { name, value }),
#bodySize NUM, contentType STR redirectUrl STR, stage "start|end" (for chunks), status NUM,
#statusText STR, data OBJ
SELECTOR #Either CSS3 selector or x(XPATH_STR)
#Not finding an element fires an error.
/=+===============================+=\
/ : : \
)==: EXIT/ERROR :==(
\ :_______________________________: /
\=+===============================+=/
exit([NUM]) #NUM is error code (déf: undefined)
#Called internally by run()
on("exit", FUNC(NUM)) #
options.exitOnError #If true (def), exits if error
die(STR, NUM) #Same but fires log(STR, "error") too
on("die", FUNC(STR, NUM)) #
on("error", FUNC(STR)) #Error not on webpage
on("page.error",FUNC(STR)) #Error on webpage
on("step.error", FUNC(ERROR)) #
on("load.error", FUNC(URL, NUM) #When a requested URL has an error NUM response code.
on("resource.error",FUNC(ERROR))#
options.timeout #Timeout of the whole script execution.
on("timeout", FUNC()) #Default handler: exits
options.stepTimeout #Same for individual steps
on("step.timeout",FUNC(NUM)) #NUM is step number
/=+===============================+=\
/ : : \
)==: STEPS :==(
\ :_______________________________: /
\=+===============================+=/
start([URL]) #Starts the PROMISE chain:
# - between start() and run()
# - each PROMISE must be registered via then*() ("step")
# - All FUNC (unless specified otherwise) return CASPER, allowing chaining.
# - Must use individual steps for every actions, including clicks, changing viewport, etc.
#If URL, performs thenOpen(URL)
on("starting|started", FUNC()) #
run([FUNC()[, UINT]]) #Runs the PROMISE chain.
#If FUNC() is defined, must call exit()
#UINT is the interval where it tries again (def: options.retryTimeout)
on("run.start|complete", FUNC())#
on("step.added", FUNC(FUNC2)) #
on("step.started", FUNC(FUNC2)) #
on("step.complete",
FUNC(RETURN_VAL)) #
then(FUNC(RESPONSE)) #Some then*() shortcuts are also provided (see other functions),
#e.g. CASPER.then(function(){CASPER.bypass(UINT)}) -> CASPER.thenBypass(UINT)
#this is bound to CASPER in FUNC()
#RESPONSE is relative to last navigation.
[thenB|b]ypass(UINT) #Do not execute next UINT PROMISE steps (to use to conditionally jump some actions).
thenBypassIf|Unless
(BOOL|FUNC(), UINT) #FUNC() returns BOOL
repeat(UINT, FUNC(RESPONSE)) #Like then(), except it performs FUNC() UINT times
/=+===============================+=\
/ : : \
)==: NAVIGATION :==(
\ :_______________________________: /
\=+===============================+=/
[thenO|o]pen(URL[, OBJ]) #Opens a new page. OBJ is { method METHOD, data OBJ2 }
on("open", FUNC(URL, OBJ)) #EVENT_FILTER "open.location" FUNC(URL)
options.pageSettings #Same as OBJ2 in PhantomJS openUrl(), i.e.:
# - javascriptEnabled BOOL (déf: true)
# - loadImages BOOL (déf: true)
# Ca also use casperjs ... --load-images=BOOL
# - localToRemoteUrlAccessEnabled (déf: true): enable file:// to http://
# Can also use casperjs ... --local-to-remote-url-access=BOOL
# - userAgent STR
# - userName STR, password STR: HTTP authentication
# - XSSAuditingEnabled BOOL (déf: false)
# - webSecurityEnabled BOOL (déf: true), e.g. forbidding cross XHR
# Can also use casperjs ... --web-security=BOOL
options.client|remoteScripts #Inject JavaScript files URL|FILE_ARR
setHttpAuth(USERNAME, PASSWORD) #Sets HTTP Authentication headers (before open())
on("http.auth",
FUNC(USERNAME, PASSWORD)) #
userAgent(STR) #Sets user agent (before open())
getCurrentUrl() #Returns page URL
page #Returns WEBPAGE
on("page.initialized",
FUNC(WEBPAGE)) #Before any URL is loaded (when blank WEBPAGE is created)
back|forward() #Web browser history
on("back|forward", FUNC()) #
reload() #Reloads the page
status([BOOL]) #Returns several information as OBJ (or JSON’d STR if BOOL true), where members are:
# - currentHTTPStatus NUM: last status code
# - loadInProgress|navigationRequested BOOL: current navigation state
# - pendingWait BOOL: a wait*() is being performed
# - options: CASPER.options
# - requestUrl STR: last URL requested
# - started: start() has been caleld
# - step NUM: step inside the PROMISE chain
on("http.status[.NUM]", #Fires FUNC when XHR gets status code NUM.
FUNC(RESPONSE)) #Can also use options.httpStatusHandlers { NUM: FUNC(CASPER, RESPONSE) }
on("[page.]resource.requested", #When making HTTP requests.
FUNC(OBJ, OBJ2)) #OBJ has members id NUM, method STR, url STR, time DATE and headers OBJ (see below)
#OBJ2 has members:
# - abort()
# - changeUrl(STR)
# - setHeader(VAR_STR, VAL_STR)
#With "page.", only main frame
on("[page.]resource.received", #When big HTTP requests, will fire several times.
FUNC(RESOURCE)) #With "page.", only main frame with stage "end"
on("load.started",FUNC()) #When page is opening
on("load.finished|failed",
FUNC(NUM|ERROR)) #When page finishes opening (no error or error)
on("url.changed", FUNC(URL)) #
on("navigation.requested", #TYPE can be "LinkClicked|Form[Res|S]ubmitted|BackOrForward|Reload|Other".
FUNC(URL, TYPE, BOOL, BOOL2)) #BOOL is whether navigation is locked.
#BOOL2 is whether this is main frame.
withFrame(STR|UINT,FUNC()) #Executes FUNC() with <frame> number UINT or name STR focused.
withPopup(REGEXP, FUNC()) #Same with popup which URL matches REGEXP
on("alert", FUNC(STR)) #Fired by popups
#EVENT_FILTER "page.confirm|prompt" FUNC(STR[, VAL]) returning BOOL|VAL
on("popup.created|loaded|
closed", FUNC(WEBPAGE)) #
/=+===============================+=\
/ : : \
)==: INTERACTION :==(
\ :_______________________________: /
\=+===============================+=/
[thenC|c]lick(SELECTOR) #
on("click", FUNC(SELECTOR)) #
clickLabel(STR[,TAG_STR]) #Click on first element (as HTML TAG if present) parent of a textNode === STR (not partially)
mouseEvent(STR, SELECTOR) #Same as click(), but can specify STR "[double]click|mouseup|down|move|over|out".
on("mouse.click|move|up|down",
FUNC(SELECTOR)) #
require("mouse").create(CASPER).
click|doubleclick|down|move|up
(X, Y) #Click on specific coordinates
sendKeys(SELECTOR, STR[, OBJ]) #Fill in an INPUT|TEXTAREA with text STR using keypress events. OBJ members are:
# - reset BOOL: if true, resets the controls value first
# - keepFocus BOOL: if true, maintains focus
# - modifiers "ctrl|alt|shift|meta|keypad[+...]"
fill(SELECTOR, OBJ[, BOOL]) #For the <form> selected by SELECTOR, fill in the values in OBJ, where the keys are the
#controls name.
#For a <select> control, values are a STR_ARR.
#If BOOL true, also submit the form.
on("fill", FUNC(SELECTOR,
OBJ, BOOL)) #
scrollTo(X, Y) #
scrollToBottom() #
/=+===============================+=\
/ : : \
)==: RENDERING :==(
\ :_______________________________: /
\=+===============================+=/
viewport(WIDTH, HEIGHT) #Changes viewport size.
#Can change default one (400x300) with options.viewportSize { width, height }
on("viewport.changed",
FUNC([ WIDTH,HEIGH ]) #
zoom(DOUBLE) #Changes zoom factor
capture(FILE[, OBJ[, OBJ2]]) #Same as render(FILE, OBJ2) with WEBPAGE.clipRect OBJ (copies from doc)
on("capture.saved", FUNC(FILE)) #EVENT_FILTER "capture.target_filename" FUNC(FILE, OBJ, OBJ2)
captureSelector(FILE,
SELECTOR[, OBJ2]) #Same as capture() but using a SELECTOR (doesn't work well with zoom())
captureBase64(FORMAT_STR, #Same as capture[Selector]() but returns as Base64 STR.
OBJ2|SELECTOR) #Supported formats are "png", "jpg", "bmp", "ppm", "tiff", "xbm" and "xpm"
download(URL,FIL[,METHOD[,OBJ]])#Download URL and saves it as FILE. Uses HTTP METHOD, with OBJ data.
on("download", FUNC(FILE)) #
base64encode(URL[,METHOD[,OBJ]])#Same but returns as Base64 STR
/=+===============================+=\
/ : : \
)==: INSPECTION :==(
\ :_______________________________: /
\=+===============================+=/
[thenE|e]valuate(FUNC[,ARGS...])#Evaluate sync. FUNC() in PAGE context/sandbox. Returns FUNC() return value.
#ARGS and return value must be plain OBJ or native type VAL.
evaluateOrDie(FUNC[,STR[, NUM]])#Same but FUNC() must return true, or it will die(STR[, NUM])
thenOpenAndEvaluate(URL, FUNC
[, ARGS...]) #then() + open() + evaluate()
getGlobal(STR) #Like evaluate, but with eval("return window." + STR) (STR must not contain any ".")
getHTML([SELECTOR[, BOOL]]) #Get inner|outerHTML (def: false, i.e. innerHTML) of whole page or specific element.
getPageContent() #Same as getHTML(), but works for non-HTML pages.
debugHTML(...) #Same as echo(CASPER.getHTML(...))
debugContent() #Same as echo(CASPER.getPageContent())
fetchText(SELECTOR) #Get textContent
getElement[s]Attribute
(SELECTOR, ATTR_STR) #Get matching elements’ HTML attribute (not JavaScript attribute) value, as STR[_ARR]
clear() #Clear the HTML content of the page
getTitle() #Returns page <title> as STR
exists|visible(SELECTOR) #Returns BOOL
resourceExists #Checks if a resource URL has been loaded.
(STR|REGEXP|FUNC(RESOURCE)) #FUNC() iterates over RESOURCEs until it returns true, then returning true for resourceExists()
#(otherwise false). this is undefined in FUNC()
getElement[s]Bounds(SELECTOR) #Get matching elements’ position as OBJ[_ARR] where OBJ is { height, left, top, width }
getElement[s]Info(SELECTOR) #Get matching elements’ informations as OBJ[_ARR] where OBJ has members:
# - attributes OBJ (HTML attributes)
# - height|width|x|y
# - nodeName: tag
# - text: textContent
# - html: outerHTML
# - visible BOOL
getFormValues(SELECTOR) #Get matching FORM controls values as OBJ (where keys are controls name)
/=+===============================+=\
/ : : \
)==: WAIT :==(
\ :_______________________________: /
\=+===============================+=/
wait(NUM) #Sleeps
waitFor(FUNC()[, FUNC2() #Polls every options.retryTimeout (def: 20ms) waiting for FUNC() to return true,
[, FUNC3(OBJ)[, UINT[, OBJ]]]]) #then executes FUNC2().
#If waits for more than UINT (def: options.waitTimeout, i.e. 5000), times out: default handler
#can be overriden by on("wait.timeout",FUNC(OBJ)) or FUNC3(OBJ) where OBJ is final waitFor()
#argument (for passing information).
#I think each step automatically wait for pending URL changes, but not for AJAX requests
#termination.
on("wait.start|done", FUNC()) #
waitForAlert(FUNC2() #Waits for window.alert()
[, FUNC3(OBJ)[, UINT]]) #Use RESPONSE as state value.
waitForPopup(STR|REGEXP #Waits for popups which URL matches STR|REGEXP.
[,FUNC2()[,FUNC3(OBJ)[, UINT]]])#Current popups available as CASPER.popups WEBPAGE_ARR
waitForResource
(STR|REGEXP|FUNC(RESOURC)[,...])#Waits for a resource to be loaded. FUNC() returns BOOL
waitForUrl(STR|REGEXP[,...]) #Waits for current page URL to match STR|REGEXP
waitFor|WhileSelector
(SELECTOR[,...]) #Waits for an element [not] to match SELECTOR
waitUntil|WhileVisible
(SELECTOR[,...]) #Waits for an element [not] matching SELECTOR to be displayed|hidden
waitForSelectorTextChange
(SELECTOR[,...]) #Waits for an element matching SELECTOR have its contentText change
waitForText(STR[,...]) #Waits for any element containing STR in its contentText
unwait() #Aborts all wait*()