Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
John McEleney
/
casperjs
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit
f44d37b6
...
f44d37b6ae0a62ffcd54fd931ba004e696e32309
authored
2012-05-21 15:29:20 +0200
by
Brikou CARRE
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
moved var declaration to the top
1 parent
b1ed9a7e
Show whitespace changes
Inline
Side-by-side
Showing
36 changed files
with
368 additions
and
279 deletions
samples/bbcshots.coffee
samples/bbcshots.js
samples/cliplay.coffee
samples/cliplay.js
samples/customevents.coffee
samples/customevents.js
samples/customlogging.coffee
samples/customlogging.js
samples/download.js
samples/dynamic.coffee
samples/dynamic.js
samples/each.coffee
samples/each.js
samples/events.coffee
samples/events.js
samples/extends.coffee
samples/extends.js
samples/googlelinks.coffee
samples/googlelinks.js
samples/googlematch.coffee
samples/googlematch.js
samples/googlepagination.coffee
samples/googlepagination.js
samples/googletesting.coffee
samples/googletesting.js
samples/logcolor.coffee
samples/logcolor.js
samples/metaextract.coffee
samples/metaextract.js
samples/multirun.js
samples/screenshot.coffee
samples/screenshot.js
samples/statushandlers.js
samples/steptimeout.js
samples/timeout.coffee
samples/timeout.js
samples/bbcshots.coffee
View file @
f44d37b
...
...
@@ -7,7 +7,7 @@ nbLinks = 0
currentLink
=
1
images
=
[]
#
helper to hide some element from remote DOM
#
## helper to hide some element from remote DOM ###
casper
.
hide
=
(
selector
)
->
@
evaluate
(
selector
)
->
document
.
querySelector
(
selector
).
style
.
display
=
"none"
...
...
@@ -17,7 +17,7 @@ casper.start "http://www.bbc.co.uk/", ->
nbLinks
=
@
evaluate
->
return
__utils__
.
findAll
(
'#promo2_carousel_items_items li'
).
length
@
echo
"
#{
nbLinks
}
items founds"
#
hide navigation arrows
#
## hide navigation arrows ###
@
hide
".nav_left"
@
hide
".nav_right"
@
mouse
.
move
"#promo2_carousel"
...
...
@@ -28,10 +28,10 @@ casper.start "http://www.bbc.co.uk/", ->
@
echo
"Clicked on pause button"
@
waitUntilVisible
".autoplay.nav_play"
,
->
@
echo
"Carousel has been paused"
#
hide play button
#
## hide play button ###
@
hide
".autoplay"
#
Capture carrousel area
#
## Capture carrousel area ###
next
=
->
image
=
"bbcshot
#{
currentLink
}
.png"
images
.
push
image
...
...
@@ -45,13 +45,13 @@ next = ->
else
@
then
buildPage
#
Building resulting page and image
#
## Building resulting page and image ###
buildPage
=
->
@
echo
"Build result page"
fs
=
require
"fs"
@
viewport
624
,
400
pageHtml
=
"<html><body style='background:black;margin:0;padding:0'>"
for
image
in
images
images
.
forEach
(
image
)
->
pageHtml
+=
"<img src='file://
#{
fs
.
workingDirectory
}
/
#{
image
}
'><br>"
pageHtml
+=
"</body></html>"
fs
.
write
"result.html"
,
pageHtml
,
'w'
...
...
samples/bbcshots.js
View file @
f44d37b
...
...
@@ -2,11 +2,14 @@
Create a mosaic image from all headline photos on BBC homepage
*/
var
casper
=
require
(
"casper"
).
create
();
var
nbLinks
=
0
;
var
currentLink
=
1
;
var
images
=
[];
var
buildPage
,
casper
,
currentLink
,
images
,
nbLinks
,
next
;
casper
=
require
(
"casper"
).
create
();
nbLinks
=
0
;
currentLink
=
1
;
images
=
[];
/* helper to hide some element from remote DOM */
casper
.
hide
=
function
(
selector
)
{
this
.
evaluate
(
function
(
selector
)
{
document
.
querySelector
(
selector
).
style
.
display
=
"none"
;
...
...
@@ -20,7 +23,7 @@ casper.start("http://www.bbc.co.uk/", function() {
return
__utils__
.
findAll
(
'#promo2_carousel_items_items li'
).
length
;
});
this
.
echo
(
nbLinks
+
" items founds"
);
/
/ hide navigation arrows
/
* hide navigation arrows */
this
.
hide
(
".nav_left"
);
this
.
hide
(
".nav_right"
);
this
.
mouse
.
move
(
"#promo2_carousel"
);
...
...
@@ -31,14 +34,16 @@ casper.start("http://www.bbc.co.uk/", function() {
this
.
echo
(
"Clicked on pause button"
);
this
.
waitUntilVisible
(
".autoplay.nav_play"
,
function
()
{
this
.
echo
(
"Carousel has been paused"
);
/
/ hide play button
/
* hide play button */
this
.
hide
(
".autoplay"
);
});
});
});
var
next
=
function
next
()
{
var
image
=
"bbcshot"
+
currentLink
+
".png"
;
/* Capture carrousel area */
next
=
function
()
{
var
image
;
image
=
"bbcshot"
+
currentLink
+
".png"
;
images
.
push
(
image
);
this
.
echo
(
"Processing image "
+
currentLink
);
this
.
captureSelector
(
image
,
'.carousel_viewport'
);
...
...
@@ -53,11 +58,13 @@ var next = function next() {
}
};
var
buildPage
=
function
buildPage
()
{
/* Building resulting page and image */
buildPage
=
function
()
{
var
fs
,
pageHtml
;
this
.
echo
(
"Build result page"
);
var
fs
=
require
(
"fs"
);
fs
=
require
(
"fs"
);
this
.
viewport
(
624
,
400
);
var
pageHtml
=
"<html><body style='background:black;margin:0;padding:0'>"
;
pageHtml
=
"<html><body style='background:black;margin:0;padding:0'>"
;
images
.
forEach
(
function
(
image
)
{
pageHtml
+=
"<img src='file://"
+
fs
.
workingDirectory
+
"/"
+
image
+
"'><br>"
;
});
...
...
samples/cliplay.coffee
View file @
f44d37b
casper
=
require
(
"casper"
).
create
()
dump
=
require
(
"utils"
).
dump
#
removing default options passed by the Python executable
#
## removing default options passed by the Python executable ###
casper
.
cli
.
drop
"cli"
casper
.
cli
.
drop
"casper-path"
...
...
samples/cliplay.js
View file @
f44d37b
var
casper
=
require
(
"casper"
).
create
();
var
dump
=
require
(
"utils"
).
dump
;
var
casper
,
dump
;
// removing default options passed by the Python executable
casper
=
require
(
"casper"
).
create
();
dump
=
require
(
"utils"
).
dump
;
/* removing default options passed by the Python executable */
casper
.
cli
.
drop
(
"cli"
);
casper
.
cli
.
drop
(
"casper-path"
);
...
...
samples/customevents.coffee
View file @
f44d37b
casper
=
require
(
"casper"
).
create
()
#
listening to a custom event
casper
.
on
'google.loaded'
,
(
title
)
->
#
## listening to a custom event ###
casper
.
on
"google.loaded"
,
(
title
)
->
casper
.
echo
"Google page title is
#{
title
}
"
casper
.
start
"http://google.com/"
,
->
#
emitting a custom event
@
emit
'google.loaded'
,
@
getTitle
()
#
## emitting a custom event ###
@
emit
"google.loaded"
,
@
getTitle
()
casper
.
run
()
...
...
samples/customevents.js
View file @
f44d37b
var
casper
=
require
(
"casper"
).
create
()
;
var
casper
;
// listening to a custom event
casper
.
on
(
'google.loaded'
,
function
(
title
)
{
casper
=
require
(
"casper"
).
create
();
/* listening to a custom event */
casper
.
on
(
"google.loaded"
,
function
(
title
)
{
casper
.
echo
(
"Google page title is "
+
title
);
});
casper
.
start
(
"http://google.com/"
,
function
()
{
/
/ emitting a custom event
this
.
emit
(
'google.loaded'
,
this
.
getTitle
());
/
* emitting a custom event */
this
.
emit
(
"google.loaded"
,
this
.
getTitle
());
});
casper
.
run
();
...
...
samples/customlogging.coffee
View file @
f44d37b
###
A basic custom logging implementation. The idea is to (extremely) verbosely
log
every received resource.
A basic custom logging implementation. The idea is to (extremely) verbosely
log
every received resource.
###
casper
=
require
(
"casper"
).
create
# Every time a resource is received, a new log entry is added to the stack
# at the 'verbose' level.
###
Every time a resource is received, a new log entry is added to the stack at
the 'verbose' level.
###
onResourceReceived
:
(
self
,
resource
)
->
infos
=
[]
props
=
[
"url"
,
"status"
,
"statusText"
,
"redirectURL"
,
"bodySize"
]
props
=
[
"url"
"status"
"statusText"
"redirectURL"
"bodySize"
]
infos
.
push
resource
[
prop
]
for
prop
in
props
infos
.
push
"[
#{
h
.
name
}
:
#{
h
.
value
}
]"
for
h
in
resource
.
headers
@
log
infos
.
join
(
', '
),
'verbose'
infos
.
push
"[
#{
h
eader
.
name
}
:
#{
header
.
value
}
]"
for
header
in
resource
.
headers
@
log
infos
.
join
(
", "
),
"verbose"
verbose
:
true
# we want to see the log printed out to the console
logLevel
:
'verbose'
# of course we want to see logs to our new level :)
logLevel
:
"verbose"
# of course we want to see logs to our new level :)
### add a new 'verbose' logging level at the lowest priority ###
casper
.
logLevels
=
[
"verbose"
].
concat
casper
.
logLevels
# add a new 'verbose' logging level at the lowest priority
casper
.
logLevels
=
[
'verbose'
].
concat
casper
.
logLevels
# test our new logger with google
### test our new logger with google ###
casper
.
start
"http://www.google.com/"
casper
.
run
()
...
...
samples/customlogging.js
View file @
f44d37b
/*
A basic custom logging implementation. The idea is to (extremely) verbosely
log
every received resource.
A basic custom logging implementation. The idea is to (extremely) verbosely
log
every received resource.
*/
var
casper
=
require
(
"casper"
).
create
({
/**
* Every time a resource is received, a new log entry is added to the stack
* at the 'verbose' level.
*
* @param Object resource A phantomjs resource object
var
casper
;
casper
=
require
(
"casper"
).
create
({
/*
Every time a resource is received, a new log entry is added to the stack at
the 'verbose' level.
*/
onResourceReceived
:
function
(
self
,
resource
)
{
var
infos
=
[
resource
.
url
,
resource
.
status
,
resource
.
statusText
,
resource
.
redirectURL
,
resource
.
bodySize
var
header
,
infos
,
prop
,
props
,
_i
,
_j
,
_len
,
_len1
,
_ref
;
infos
=
[];
props
=
[
"url"
,
"status"
,
"statusText"
,
"redirectURL"
,
"bodySize"
];
resource
.
headers
.
forEach
(
function
(
header
)
{
infos
.
push
(
'['
+
[
header
.
name
,
header
.
value
].
join
(
', '
)
+
']'
);
});
self
.
log
(
infos
.
join
(
', '
),
'verbose'
);
for
(
_i
=
0
,
_len
=
props
.
length
;
_i
<
_len
;
_i
++
)
{
prop
=
props
[
_i
];
infos
.
push
(
resource
[
prop
]);
}
_ref
=
resource
.
headers
;
for
(
_j
=
0
,
_len1
=
_ref
.
length
;
_j
<
_len1
;
_j
++
)
{
header
=
_ref
[
_j
];
infos
.
push
(
"["
+
header
.
name
+
": "
+
header
.
value
+
"]"
);
}
this
.
log
(
infos
.
join
(
", "
),
"verbose"
);
},
verbose
:
true
,
// we want to see the log printed out to the console
logLevel
:
'verbose'
// of course we want to see logs to our new level :)
verbose
:
true
,
logLevel
:
"verbose"
});
/
/ add a new 'verbose' logging level at the lowest priority
casper
.
logLevels
=
[
'verbose'
].
concat
(
casper
.
logLevels
);
/
* add a new 'verbose' logging level at the lowest priority */
casper
.
logLevels
=
[
"verbose"
].
concat
(
casper
.
logLevels
);
/
/ test our new logger with google
casper
.
start
(
"http://www.google.com/"
)
.
run
(
function
(
self
)
{
self
.
exit
();
}
);
/
* test our new logger with google */
casper
.
start
(
"http://www.google.com/"
)
;
casper
.
run
(
);
\ No newline at end of file
...
...
samples/download.js
View file @
f44d37b
...
...
@@ -2,7 +2,9 @@
Download the google logo image as base64
*/
var
casper
=
require
(
"casper"
).
create
({
var
casper
;
casper
=
require
(
"casper"
).
create
({
verbose
:
true
});
...
...
samples/dynamic.coffee
View file @
f44d37b
casper
=
require
(
"casper"
).
create
verbose
:
true
casper
=
require
(
"casper"
).
create
verbose
:
true
#
If we don't set a limit, it could go on forever
upTo
=
~~
casper
.
cli
.
get
(
0
)
||
10
# max 10 links
#
## If we don't set a limit, it could go on forever ###
upTo
=
~~
casper
.
cli
.
get
(
0
)
||
10
# Fetch all <a> elements from the page and return
# the ones which contains a href starting with 'http://'
###
Fetch all <a> elements from the page and return
the ones which contains a href starting with 'http://'
###
searchLinks
=
->
filter
=
Array
::
filter
map
=
Array
::
map
...
...
@@ -13,20 +16,22 @@ searchLinks = ->
),
(
a
)
->
a
.
getAttribute
"href"
#
The base links array
#
## The base links array ###
links
=
[
'http://google.com/'
'http://yahoo.com/'
'http://bing.com/'
"http://google.com/"
"http://yahoo.com/"
"http://bing.com/"
]
#
Just opens the page and prints the title
#
## Just opens the page and prints the title ###
start
=
(
link
)
->
@
start
link
,
->
@
echo
"Page title:
#{
@
getTitle
()
}
"
# Get the links, and add them to the links array
# (It could be done all in one step, but it is intentionally splitted)
###
Get the links, and add them to the links array
(It could be done all in one step, but it is intentionally splitted)
###
addLinks
=
(
link
)
->
@
then
->
found
=
@
evaluate
searchLinks
...
...
@@ -35,11 +40,12 @@ addLinks = (link) ->
casper
.
start
()
casper
.
then
->
@
echo
"Starting"
casper
.
then
->
@
echo
"Starting"
currentLink
=
0
;
#
As long as it has a next link, and is under the maximum limit, will keep running
#
## As long as it has a next link, and is under the maximum limit, will keep running ###
check
=
->
if
links
[
currentLink
]
&&
currentLink
<
upTo
@
echo
"--- Link
#{
currentLink
}
---"
...
...
samples/dynamic.js
View file @
f44d37b
var
casper
=
require
(
"casper"
).
create
({
var
addLinks
,
casper
,
check
,
currentLink
,
links
,
searchLinks
,
start
,
upTo
;
casper
=
require
(
"casper"
).
create
({
verbose
:
true
});
/
/ If we don't set a limit, it could go on forever
var
upTo
=
~~
casper
.
cli
.
get
(
0
)
||
10
;
// max 10 links
/
* If we don't set a limit, it could go on forever */
upTo
=
~~
casper
.
cli
.
get
(
0
)
||
10
;
// Fetch all <a> elements from the page and return
// the ones which contains a href starting with 'http://'
var
searchLinks
=
function
searchLinks
()
{
var
filter
=
Array
.
prototype
.
filter
;
var
map
=
Array
.
prototype
.
map
;
/*
Fetch all <a> elements from the page and return
the ones which contains a href starting with 'http://'
*/
searchLinks
=
function
()
{
var
filter
,
map
;
filter
=
Array
.
prototype
.
filter
;
map
=
Array
.
prototype
.
map
;
return
map
.
call
(
filter
.
call
(
document
.
querySelectorAll
(
"a"
),
function
(
a
)
{
return
/^http:
\/\/
.*/i
.
test
(
a
.
getAttribute
(
"href"
));
}),
function
(
a
)
{
...
...
@@ -17,25 +22,28 @@ var searchLinks = function searchLinks() {
});
};
/
/ The base links array
var
links
=
[
'http://google.com/'
,
'http://yahoo.com/'
,
'http://bing.com/'
/
* The base links array */
links
=
[
"http://google.com/"
,
"http://yahoo.com/"
,
"http://bing.com/"
];
/
/ Just opens the page and prints the title
var
start
=
function
start
(
link
)
{
/
* Just opens the page and prints the title */
start
=
function
(
link
)
{
this
.
start
(
link
,
function
()
{
this
.
echo
(
"Page title: "
+
(
this
.
getTitle
()
));
this
.
echo
(
"Page title: "
+
this
.
getTitle
(
));
});
};
// Get the links, and add them to the links array
// (It could be done all in one step, but it is intentionally splitted)
var
addLinks
=
function
addLinks
(
link
)
{
/*
Get the links, and add them to the links array
(It could be done all in one step, but it is intentionally splitted)
*/
addLinks
=
function
(
link
)
{
this
.
then
(
function
()
{
var
found
=
this
.
evaluate
(
searchLinks
);
var
found
;
found
=
this
.
evaluate
(
searchLinks
);
this
.
echo
(
found
.
length
+
" links found on "
+
link
);
links
=
links
.
concat
(
found
);
});
...
...
@@ -47,10 +55,10 @@ casper.then(function() {
this
.
echo
(
"Starting"
);
});
var
currentLink
=
0
;
currentLink
=
0
;
/
/ As long as it has a next link, and is under the maximum limit, will keep running
var
check
=
function
check
()
{
/
* As long as it has a next link, and is under the maximum limit, will keep running */
check
=
function
()
{
if
(
links
[
currentLink
]
&&
currentLink
<
upTo
)
{
this
.
echo
(
"--- Link "
+
currentLink
+
" ---"
);
start
.
call
(
this
,
links
[
currentLink
]);
...
...
samples/each.coffee
View file @
f44d37b
casper
=
require
(
"casper"
).
create
()
links
=
[
'http://google.com/'
'http://yahoo.com/'
'http://bing.com/'
"http://google.com/"
"http://yahoo.com/"
"http://bing.com/"
]
casper
.
start
()
...
...
samples/each.js
View file @
f44d37b
var
casper
=
require
(
"casper"
).
create
()
;
var
casper
,
links
;
var
links
=
[
'http://google.com/'
,
'http://yahoo.com/'
,
'http://bing.com/'
casper
=
require
(
"casper"
).
create
();
links
=
[
"http://google.com/"
,
"http://yahoo.com/"
,
"http://bing.com/"
];
casper
.
start
();
casper
.
each
(
links
,
function
(
self
,
link
)
{
this
.
thenOpen
(
link
,
function
()
{
this
.
echo
(
(
this
.
getTitle
()
)
+
" - "
+
link
);
this
.
echo
(
this
.
getTitle
(
)
+
" - "
+
link
);
});
});
...
...
samples/events.coffee
View file @
f44d37b
###
This script will add a custom HTTP status code handler, here for 404 pages.
###
casper
=
require
(
"casper"
).
create
()
casper
.
on
"http.status.200"
,
(
resource
)
->
...
...
@@ -16,14 +20,15 @@ casper.on "http.status.500", (resource) ->
@
echo
"
#{
resource
.
url
}
is in error"
,
"ERROR"
links
=
[
'http://google.com/'
'http://www.google.com/'
'http://www.google.com/plop'
"http://google.com/"
"http://www.google.com/"
"http://www.google.com/plop"
]
casper
.
start
()
for
link
in
links
casper
.
thenOpen
link
,
->
@
echo
"
#{
link
}
loaded"
casper
.
each
links
,
(
self
,
link
)
->
self
.
thenOpen
link
,
->
@
echo
"
#{
link
}
loaded"
casper
.
run
()
...
...
samples/events.js
View file @
f44d37b
/**
* This script will add a custom HTTP status code handler, here for 404 pages.
*
*/
var
casper
=
require
(
"casper"
).
create
();
casper
.
on
(
'http.status.200'
,
function
(
resource
)
{
this
.
echo
(
resource
.
url
+
' is OK'
,
'INFO'
);
/*
This script will add a custom HTTP status code handler, here for 404 pages.
*/
var
casper
,
links
;
casper
=
require
(
"casper"
).
create
();
casper
.
on
(
"http.status.200"
,
function
(
resource
)
{
this
.
echo
(
resource
.
url
+
" is OK"
,
"INFO"
);
});
casper
.
on
(
'http.status.301'
,
function
(
resource
)
{
this
.
echo
(
resource
.
url
+
' is permanently redirected'
,
'PARAMETER'
);
casper
.
on
(
"http.status.301"
,
function
(
resource
)
{
this
.
echo
(
resource
.
url
+
" is permanently redirected"
,
"PARAMETER"
);
});
casper
.
on
(
'http.status.302'
,
function
(
resource
)
{
this
.
echo
(
resource
.
url
+
' is temporarily redirected'
,
'PARAMETER'
);
casper
.
on
(
"http.status.302"
,
function
(
resource
)
{
this
.
echo
(
resource
.
url
+
" is temporarily redirected"
,
"PARAMETER"
);
});
casper
.
on
(
'http.status.404'
,
function
(
resource
)
{
this
.
echo
(
resource
.
url
+
' is not found'
,
'COMMENT'
);
casper
.
on
(
"http.status.404"
,
function
(
resource
)
{
this
.
echo
(
resource
.
url
+
" is not found"
,
"COMMENT"
);
});
casper
.
on
(
'http.status.500'
,
function
(
resource
)
{
this
.
echo
(
resource
.
url
+
' is in error'
,
'ERROR'
);
casper
.
on
(
"http.status.500"
,
function
(
resource
)
{
this
.
echo
(
resource
.
url
+
" is in error"
,
"ERROR"
);
});
var
links
=
[
'http://google.com/'
,
'http://www.google.com/'
,
'http://www.google.com/plop'
links
=
[
"http://google.com/"
,
"http://www.google.com/"
,
"http://www.google.com/plop"
];
casper
.
start
().
each
(
links
,
function
(
self
,
link
)
{
casper
.
start
();
casper
.
each
(
links
,
function
(
self
,
link
)
{
self
.
thenOpen
(
link
,
function
()
{
this
.
echo
(
link
+
' loaded'
);
this
.
echo
(
link
+
" loaded"
);
});
});
...
...
samples/extends.coffee
View file @
f44d37b
casper
=
require
(
"casper"
).
create
loadImages
:
false
logLevel
:
"debug"
verbose
:
true
links
=
'http://edition.cnn.com/'
:
0
'http://www.nytimes.com/'
:
0
'http://www.bbc.co.uk/'
:
0
'http://www.guardian.co.uk/'
:
0
"http://edition.cnn.com/"
:
0
"http://www.nytimes.com/"
:
0
"http://www.bbc.co.uk/"
:
0
"http://www.guardian.co.uk/"
:
0
class
Fantomas
extends
require
(
"casper"
).
Casper
countLinks
:
->
@
evaluate
->
__utils__
.
findAll
(
'a'
).
length
fantomas
=
Object
.
create
(
casper
)
renderJSON
:
(
what
)
->
@
echo
JSON
.
stringify
what
,
null
,
' '
fantomas
.
countLinks
=
->
@
evaluate
->
__utils__
.
findAll
(
"a[href]"
).
length
fantomas
=
new
Fantomas
loadImages
:
false
logLevel
:
"debug"
verbose
:
true
fantomas
.
renderJSON
=
(
what
)
->
@
echo
JSON
.
stringify
(
what
,
null
,
" "
)
fantomas
.
start
()
for
url
of
links
do
(
url
)
->
Object
.
keys
(
links
).
forEach
(
url
)
->
fantomas
.
thenOpen
url
,
->
links
[
url
]
=
@
countLinks
()
fantomas
.
run
->
@
renderJSON
links
@
renderJSON
(
links
)
@
exit
()
...
...
samples/extends.js
View file @
f44d37b
var
casper
=
require
(
"casper"
).
create
({
var
casper
,
fantomas
,
links
;
casper
=
require
(
"casper"
).
create
({
loadImages
:
false
,
logLevel
:
"debug"
,
verbose
:
true
});
var
links
=
{
'http://edition.cnn.com/'
:
0
,
'http://www.nytimes.com/'
:
0
,
'http://www.bbc.co.uk/'
:
0
,
'http://www.guardian.co.uk/'
:
0
links
=
{
"http://edition.cnn.com/"
:
0
,
"http://www.nytimes.com/"
:
0
,
"http://www.bbc.co.uk/"
:
0
,
"http://www.guardian.co.uk/"
:
0
};
var
fantomas
=
Object
.
create
(
casper
);
fantomas
=
Object
.
create
(
casper
);
fantomas
.
countLinks
=
function
(
selector
)
{
fantomas
.
countLinks
=
function
()
{
return
this
.
evaluate
(
function
()
{
return
__utils__
.
findAll
(
'a[href]'
).
length
;
return
__utils__
.
findAll
(
"a[href]"
).
length
;
});
};
fantomas
.
renderJSON
=
function
(
what
)
{
return
this
.
echo
(
JSON
.
stringify
(
what
,
null
,
' '
));
this
.
echo
(
JSON
.
stringify
(
what
,
null
,
" "
));
};
fantomas
.
start
();
...
...
@@ -32,5 +34,6 @@ Object.keys(links).forEach(function(url) {
});
fantomas
.
run
(
function
()
{
this
.
renderJSON
(
links
).
exit
();
this
.
renderJSON
(
links
);
this
.
exit
();
});
\ No newline at end of file
...
...
samples/googlelinks.coffee
View file @
f44d37b
...
...
@@ -6,21 +6,21 @@ links = []
casper
=
require
(
"casper"
).
create
()
casper
.
start
"http://google.fr/"
,
->
#
search for 'casperjs' from google form
#
## search for 'casperjs' from google form ###
@
fill
'form[action="/search"]'
,
q
:
"casperjs"
,
true
casper
.
then
->
#
aggregate results for the 'casperjs' search
#
## aggregate results for the 'casperjs' search ###
links
=
@
evaluate
getLinks
#
search for 'phantomjs' from google form
#
## search for 'phantomjs' from google form ###
@
fill
'form[action="/search"]'
,
q
:
"phantomjs"
,
true
casper
.
then
->
#
concat results for the 'phantomjs' search
#
## concat results for the 'phantomjs' search ###
links
=
links
.
concat
@
evaluate
(
getLinks
)
casper
.
run
->
#
display results
#
## display results ###
@
echo
"
#{
links
.
length
}
links found:"
@
echo
" - "
+
links
.
join
"
\n
- "
@
exit
()
...
...
samples/googlelinks.js
View file @
f44d37b
var
getLinks
=
function
getLinks
()
{
var
casper
,
getLinks
,
links
;
getLinks
=
function
()
{
var
links
;
links
=
document
.
querySelectorAll
(
"h3.r a"
);
return
Array
.
prototype
.
map
.
call
(
links
,
function
(
e
)
{
...
...
@@ -6,32 +8,33 @@ var getLinks = function getLinks() {
});
};
var
links
=
[];
var
casper
=
require
(
"casper"
).
create
();
links
=
[];
casper
=
require
(
"casper"
).
create
();
casper
.
start
(
"http://google.fr/"
,
function
()
{
/
/ search for 'casperjs' from google form
/
* search for 'casperjs' from google form */
this
.
fill
(
'form[action="/search"]'
,
{
q
:
"casperjs"
},
true
);
});
casper
.
then
(
function
()
{
/
/ aggregate results for the 'casperjs' search
/
* aggregate results for the 'casperjs' search */
links
=
this
.
evaluate
(
getLinks
);
/
/ search for 'phantomjs' from google form
/
* search for 'phantomjs' from google form */
this
.
fill
(
'form[action="/search"]'
,
{
q
:
"phantomjs"
},
true
);
});
casper
.
then
(
function
()
{
/
/ concat results for the 'phantomjs' search
/
* concat results for the 'phantomjs' search */
links
=
links
.
concat
(
this
.
evaluate
(
getLinks
));
});
casper
.
run
(
function
()
{
/
/ display results
/
* display results */
this
.
echo
(
links
.
length
+
" links found:"
);
this
.
echo
(
" - "
+
links
.
join
(
"\n - "
));
this
.
exit
();
...
...
samples/googlematch.coffee
View file @
f44d37b
...
...
@@ -3,7 +3,7 @@ Takes provided terms passed as arguments and query google for the number of
estimated results each have.
Usage:
$ casperjs googlematch.
js
nicolas chuck borris
$ casperjs googlematch.
coffee
nicolas chuck borris
nicolas: 69600000
chuck: 49500000
borris: 2370000
...
...
samples/googlematch.js
View file @
f44d37b
...
...
@@ -10,18 +10,21 @@ Usage:
winner is "nicolas" with 69600000 results
*/
var
casper
=
require
(
"casper"
).
create
({
var
casper
,
scores
,
terms
;
casper
=
require
(
"casper"
).
create
({
verbose
:
true
});
casper
.
fetchScore
=
function
()
{
return
this
.
evaluate
(
function
()
{
var
result
=
document
.
querySelector
(
'#resultStats'
).
innerText
;
var
result
;
result
=
document
.
querySelector
(
'#resultStats'
).
innerText
;
return
~~
(
/Environ
([
0-9
\s]{1,})
.*/
.
exec
(
result
)[
1
].
replace
(
/
\s
/g
,
''
));
});
};
var
terms
=
casper
.
cli
.
args
;
terms
=
casper
.
cli
.
args
;
if
(
terms
.
length
<
2
)
{
casper
...
...
@@ -30,7 +33,7 @@ if (terms.length < 2) {
;
}
var
scores
=
[];
scores
=
[];
casper
.
echo
(
"Let the match begin between \""
+
(
terms
.
join
(
'", "'
))
+
"\"!"
);
...
...
@@ -54,10 +57,11 @@ casper.each(terms, function(self, term) {
});
casper
.
run
(
function
()
{
var
winner
;
scores
.
sort
(
function
(
a
,
b
)
{
return
b
.
score
-
a
.
score
;
});
var
winner
=
scores
[
0
];
winner
=
scores
[
0
];
this
.
echo
(
"Winner is \""
+
winner
.
term
+
"\" with "
+
winner
.
score
+
" results"
);
this
.
exit
();
});
...
...
samples/googlepagination.coffee
View file @
f44d37b
...
...
@@ -24,16 +24,16 @@ processPage = ->
if
@
exists
"#pnnext"
currentPage
++
@
echo
"requesting next page:
#{
currentPage
}
"
#@thenClick("#pnnext").then(processPage)
url
=
@
getCurrentUrl
()
@
thenClick
(
"#pnnext"
).
then
->
check
=
->
url
!=
@
getCurrentUrl
()
@
waitFor
check
,
processPage
@
waitFor
(
->
url
isnt
@
getCurrentUrl
()
),
processPage
else
@
echo
"that's all, folks."
casper
.
start
"http://google.fr/"
,
->
@
fill
'form[action="/search"]'
,
q
:
casper
.
cli
.
args
.
join
(
' '
),
true
@
fill
'form[action="/search"]'
,
q
:
casper
.
cli
.
args
.
join
(
" "
),
true
casper
.
then
processPage
...
...
samples/googlepagination.js
View file @
f44d37b
...
...
@@ -6,18 +6,19 @@ Usage: $ casperjs googlepagination.coffee my search terms
(all arguments will be used as the query)
*/
var
casper
=
require
(
"casper"
).
create
()
;
var
casper
,
currentPage
,
processPage
;
var
currentPage
=
1
;
casper
=
require
(
"casper"
).
create
();
currentPage
=
1
;
if
(
casper
.
cli
.
args
.
length
===
0
)
{
casper
.
echo
(
"Usage: $ casperjs googlepagination.
coffee
my search terms"
)
.
echo
(
"Usage: $ casperjs googlepagination.
js
my search terms"
)
.
exit
(
1
)
;
}
var
processPage
=
function
processPage
()
{
processPage
=
function
()
{
var
url
;
this
.
echo
(
"capturing page "
+
currentPage
);
this
.
capture
(
"google-results-p"
+
currentPage
+
".png"
);
...
...
@@ -28,8 +29,8 @@ var processPage = function processPage() {
currentPage
++
;
this
.
echo
(
"requesting next page: "
+
currentPage
);
url
=
this
.
getCurrentUrl
();
return
this
.
thenClick
(
"#pnnext"
).
then
(
function
()
{
return
this
.
waitFor
(
function
()
{
this
.
thenClick
(
"#pnnext"
).
then
(
function
()
{
this
.
waitFor
(
function
()
{
return
url
!==
this
.
getCurrentUrl
();
},
processPage
);
});
...
...
@@ -39,8 +40,8 @@ var processPage = function processPage() {
};
casper
.
start
(
"http://google.fr/"
,
function
()
{
return
this
.
fill
(
'form[action="/search"]'
,
{
q
:
casper
.
cli
.
args
.
join
(
' '
)
this
.
fill
(
'form[action="/search"]'
,
{
q
:
casper
.
cli
.
args
.
join
(
" "
)
},
true
);
});
...
...
samples/googletesting.coffee
View file @
f44d37b
casper
=
require
(
"casper"
).
create
logLevel
:
"debug"
casper
=
require
(
"casper"
).
create
logLevel
:
"debug"
casper
.
start
"http://www.google.fr/"
,
->
@
test
.
assertTitle
'Google'
,
'google homepage title is the one expected'
@
test
.
assertExists
'form[action="/search"]'
,
'main form is found'
@
fill
'form[action="/search"]'
,
q
:
'foo'
,
true
@
test
.
assertTitle
"Google"
,
"google homepage title is the one expected"
@
test
.
assertExists
'form[action="/search"]'
,
"main form is found"
@
fill
'form[action="/search"]'
,
q
:
"foo"
,
true
casper
.
then
->
@
test
.
assertTitle
'foo - Recherche Google'
,
'google title is ok'
@
test
.
assertUrlMatch
/q=foo/
,
'search term has been submitted'
test
=
->
__utils__
.
findAll
(
'h3.r'
).
length
>=
10
@
test
.
assertEval
test
,
'google search for "foo" retrieves 10 or more results'
@
test
.
assertTitle
"foo - Recherche Google"
,
"google title is ok"
@
test
.
assertUrlMatch
/q=foo/
,
"search term has been submitted"
@
test
.
assertEval
(
->
__utils__
.
findAll
(
"h3.r"
).
length
>=
10
),
"google search for
\"
foo
\"
retrieves 10 or more results"
casper
.
run
->
@
test
.
renderResults
true
casper
.
run
->
@
test
.
renderResults
true
...
...
samples/googletesting.js
View file @
f44d37b
var
casper
=
require
(
"casper"
).
create
({
var
casper
;
casper
=
require
(
"casper"
).
create
({
logLevel
:
"debug"
});
casper
.
start
(
"http://www.google.fr/"
,
function
(
self
)
{
self
.
test
.
assertTitle
(
'Google'
,
'google homepage title is the one expected'
);
self
.
test
.
assertExists
(
'form[action="/search"]'
,
'main form is found'
);
self
.
fill
(
'form[action="/search"]'
,
{
q
:
'foo'
casper
.
start
(
"http://www.google.fr/"
,
function
()
{
this
.
test
.
assertTitle
(
"Google"
,
"google homepage title is the one expected"
);
this
.
test
.
assertExists
(
'form[action="/search"]'
,
"main form is found"
);
this
.
fill
(
'form[action="/search"]'
,
{
q
:
"foo"
},
true
);
});
casper
.
then
(
function
(
self
)
{
self
.
test
.
assertTitle
(
'foo - Recherche Google'
,
'google title is ok'
);
self
.
test
.
assertUrlMatch
(
/q=foo/
,
'search term has been submitted'
);
self
.
test
.
assertEval
(
function
()
{
return
__utils__
.
findAll
(
'h3.r'
).
length
>=
10
;
}
,
'google search for "foo" retrieves 10 or more results'
);
casper
.
then
(
function
()
{
this
.
test
.
assertTitle
(
"foo - Recherche Google"
,
"google title is ok"
);
this
.
test
.
assertUrlMatch
(
/q=foo/
,
"search term has been submitted"
);
this
.
test
.
assertEval
(
(
function
()
{
return
__utils__
.
findAll
(
"h3.r"
).
length
>=
10
;
}
),
"google search for \"foo\" retrieves 10 or more results"
);
});
casper
.
run
(
function
(
self
)
{
self
.
test
.
renderResults
(
true
);
casper
.
run
(
function
()
{
this
.
test
.
renderResults
(
true
);
});
...
...
samples/logcolor.coffee
View file @
f44d37b
casper
=
require
(
"casper"
).
create
verbose
:
true
logLevel
:
'debug'
logLevel
:
"debug"
casper
.
log
"this is a debug message"
,
'debug'
casper
.
log
"and an informative one"
,
'info'
casper
.
log
"and a warning"
,
'warning'
casper
.
log
"and an error"
,
'error'
casper
.
log
"this is a debug message"
,
"debug"
casper
.
log
"and an informative one"
,
"info"
casper
.
log
"and a warning"
,
"warning"
casper
.
log
"and an error"
,
"error"
casper
.
exit
()
...
...
samples/logcolor.js
View file @
f44d37b
var
casper
=
require
(
"casper"
).
create
({
var
casper
;
casper
=
require
(
"casper"
).
create
({
verbose
:
true
,
logLevel
:
'debug'
logLevel
:
"debug"
});
casper
.
log
(
"this is a debug message"
,
'debug'
);
casper
.
log
(
"and an informative one"
,
'info'
);
casper
.
log
(
"and a warning"
,
'warning'
);
casper
.
log
(
"and an error"
,
'error'
);
casper
.
log
(
"this is a debug message"
,
"debug"
);
casper
.
log
(
"and an informative one"
,
"info"
);
casper
.
log
(
"and a warning"
,
"warning"
);
casper
.
log
(
"and an error"
,
"error"
);
casper
.
exit
();
...
...
samples/metaextract.coffee
View file @
f44d37b
...
...
@@ -4,8 +4,8 @@ metas = []
if
not
url
casper
.
echo
"Usage: casperjs [url]"
.
exit
(
1
)
.
echo
(
"Usage: $ casperjs metaextract.coffee <url>"
)
.
exit
1
casper
.
start
url
,
->
metas
=
@
evaluate
->
...
...
samples/metaextract.js
View file @
f44d37b
var
casper
=
require
(
"casper"
).
create
()
,
url
=
casper
.
cli
.
get
(
0
)
,
metas
=
[];
var
casper
,
metas
,
url
;
casper
=
require
(
"casper"
).
create
();
url
=
casper
.
cli
.
get
(
0
);
metas
=
[];
if
(
!
url
)
{
casper
.
echo
(
"Usage:
casperjs [url]
"
)
.
echo
(
"Usage:
$ casperjs metaextract.js <url>
"
)
.
exit
(
1
)
;
}
...
...
@@ -12,7 +14,7 @@ if (!url) {
casper
.
start
(
url
,
function
()
{
metas
=
this
.
evaluate
(
function
()
{
var
metas
=
[];
[].
forEach
.
call
(
document
.
querySelectorAll
(
'meta'
),
function
(
elem
)
{
[].
forEach
.
call
(
document
.
querySelectorAll
(
"meta"
),
function
(
elem
)
{
var
meta
=
{};
[].
slice
.
call
(
elem
.
attributes
).
forEach
(
function
(
attr
)
{
meta
[
attr
.
name
]
=
attr
.
value
;
...
...
samples/multirun.js
View file @
f44d37b
var
casper
=
require
(
"casper"
).
create
({
var
casper
,
check
,
countLinks
,
currentSuite
,
suites
;
casper
=
require
(
"casper"
).
create
({
verbose
:
true
});
var
countLinks
=
function
countLinks
()
{
countLinks
=
function
()
{
return
document
.
querySelectorAll
(
'a'
).
length
;
};
var
suites
=
[
suites
=
[
function
()
{
this
.
echo
(
"Suite 1"
);
this
.
start
(
"http://google.com/"
,
function
()
{
...
...
@@ -40,9 +42,9 @@ casper.then(function() {
this
.
echo
(
"Starting"
);
});
var
currentSuite
=
0
;
currentSuite
=
0
;
var
check
=
function
check
()
{
check
=
function
()
{
if
(
suites
[
currentSuite
])
{
suites
[
currentSuite
].
call
(
this
);
currentSuite
++
;
...
...
samples/screenshot.coffee
View file @
f44d37b
...
...
@@ -17,9 +17,12 @@ if not twitterAccount or not filename or not /\.(png|jpg|pdf)$/i.test filename
.
exit
(
1
)
casper
.
start
"https://twitter.com/#!/
#{
twitterAccount
}
"
,
->
capture
=
->
@
captureSelector
filename
,
'html'
@
waitForSelector
".tweet-row"
,
(
->
@
captureSelector
filename
,
"html"
@
echo
"Saved screenshot of
#{
@
getCurrentUrl
()
}
to
#{
filename
}
"
@
waitForSelector
'.tweet-row'
,
capture
,
null
,
12000
),
(
->
@
die
(
"Timeout reached. Fail whale?"
)
@
exit
()
),
12000
casper
.
run
()
...
...
samples/screenshot.js
View file @
f44d37b
...
...
@@ -3,15 +3,17 @@ This script will capture a screenshot of a twitter account page
Usage: $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>
*/
var
casper
=
require
(
"casper"
).
create
({
var
casper
,
filename
,
twitterAccount
;
casper
=
require
(
"casper"
).
create
({
viewportSize
:
{
width
:
1024
,
height
:
768
}
});
var
twitterAccount
=
casper
.
cli
.
get
(
0
);
var
filename
=
casper
.
cli
.
get
(
1
);
twitterAccount
=
casper
.
cli
.
get
(
0
);
filename
=
casper
.
cli
.
get
(
1
);
if
(
!
twitterAccount
||
!
filename
||
!
/
\.(
png|jpg|pdf
)
$/i
.
test
(
filename
))
{
casper
...
...
@@ -21,12 +23,13 @@ if (!twitterAccount || !filename || !/\.(png|jpg|pdf)$/i.test(filename)) {
}
casper
.
start
(
"https://twitter.com/#!/"
+
twitterAccount
,
function
()
{
this
.
waitForSelector
(
'.tweet-row'
,
function
()
{
this
.
captureSelector
(
filename
,
'html'
);
this
.
echo
(
"Saved screenshot of "
+
this
.
getCurrentUrl
()
+
" to "
+
filename
);
},
function
()
{
this
.
die
(
'Timeout reached. Fail whale?'
).
exit
();
},
12000
);
this
.
waitForSelector
(
".tweet-row"
,
(
function
()
{
this
.
captureSelector
(
filename
,
"html"
);
this
.
echo
(
"Saved screenshot of "
+
(
this
.
getCurrentUrl
())
+
" to "
+
filename
);
}),
(
function
()
{
this
.
die
(
"Timeout reached. Fail whale?"
);
this
.
exit
();
}),
12000
);
});
casper
.
run
();
...
...
samples/statushandlers.js
View file @
f44d37b
...
...
@@ -2,7 +2,9 @@
This script will add a custom HTTP status code handler, here for 404 pages.
*/
var
casper
=
require
(
"casper"
).
create
({
var
casper
;
casper
=
require
(
"casper"
).
create
({
httpStatusHandlers
:
{
404
:
function
(
self
,
resource
)
{
this
.
echo
(
"Resource at "
+
resource
.
url
+
" not found (404)"
,
"COMMENT"
);
...
...
samples/steptimeout.js
View file @
f44d37b
var
failed
=
[];
var
casper
,
failed
,
links
,
timeout
,
__indexOf
=
[].
indexOf
||
function
(
item
)
{
for
(
var
i
=
0
,
l
=
this
.
length
;
i
<
l
;
i
++
)
{
if
(
i
in
this
&&
this
[
i
]
===
item
)
return
i
;
}
return
-
1
;
};
var
casper
=
require
(
"casper"
).
create
({
failed
=
[];
casper
=
require
(
"casper"
).
create
({
onStepTimeout
:
function
()
{
failed
.
push
(
this
.
requestUrl
);
}
});
var
links
=
[
links
=
[
'http://google.com/'
,
'http://akei.com/'
,
'http://lemonde.fr/'
,
...
...
@@ -14,10 +17,12 @@ var links = [
'http://cdiscount.fr/'
];
var
timeout
=
~~
casper
.
cli
.
get
(
0
);
timeout
=
~~
casper
.
cli
.
get
(
0
);
if
(
timeout
<
1
)
{
timeout
=
1000
;
}
casper
.
options
.
stepTimeout
=
timeout
;
casper
.
echo
(
"Testing with timeout="
+
casper
.
options
.
stepTimeout
+
"ms."
);
...
...
@@ -25,13 +30,13 @@ casper.echo("Testing with timeout=" + casper.options.stepTimeout + "ms.");
casper
.
start
();
casper
.
each
(
links
,
function
(
self
,
link
)
{
self
.
test
.
comment
(
'Adding '
+
link
+
' to test suite'
);
self
.
thenOpen
(
link
,
function
(
self
)
{
var
testStatus
=
self
.
test
.
pass
;
if
(
failed
.
indexOf
(
self
.
requestUrl
)
>
-
1
)
{
self
.
test
.
fail
(
self
.
requestUrl
);
this
.
test
.
comment
(
"Adding "
+
link
+
" to test suite"
);
this
.
thenOpen
(
link
,
function
(
)
{
var
_ref
;
if
(
_ref
=
this
.
requestUrl
,
__indexOf
.
call
(
failed
,
_ref
)
>=
0
)
{
this
.
test
.
fail
(
""
+
this
.
requestUrl
+
" loaded in less than "
+
timeout
+
"ms."
);
}
else
{
self
.
test
.
pass
(
self
.
requestUrl
);
this
.
test
.
pass
(
""
+
this
.
requestUrl
+
" loaded in less than "
+
timeout
+
"ms."
);
}
});
});
...
...
samples/timeout.coffee
View file @
f44d37b
...
...
@@ -17,7 +17,7 @@ YES!
casper
=
require
(
"casper"
).
create
onTimeout
:
->
@
echo
"NOPE."
,
'RED_BAR'
@
echo
"NOPE."
,
"RED_BAR"
@
exit
()
timeout
=
~~
casper
.
cli
.
get
0
...
...
@@ -30,7 +30,7 @@ casper.echo "Will google.com load in less than #{timeout}ms?"
casper
.
options
.
timeout
=
timeout
casper
.
start
"http://www.google.com/"
,
->
@
echo
"YES!"
,
'GREEN_BAR'
@
echo
"YES!"
,
"GREEN_BAR"
@
exit
()
casper
.
run
()
\ No newline at end of file
...
...
samples/timeout.js
View file @
f44d37b
...
...
@@ -15,16 +15,18 @@ Will google.com load in less than 2000ms?
YES!
*/
var
casper
=
require
(
"casper"
).
create
({
var
casper
,
timeout
;
casper
=
require
(
"casper"
).
create
({
onTimeout
:
function
()
{
this
.
echo
(
"NOPE."
,
'RED_BAR'
)
.
echo
(
"NOPE."
,
"RED_BAR"
)
.
exit
()
;
}
});
var
timeout
=
~~
casper
.
cli
.
get
(
0
);
timeout
=
~~
casper
.
cli
.
get
(
0
);
if
(
timeout
<
1
)
{
casper
...
...
@@ -32,14 +34,13 @@ if (timeout < 1) {
.
exit
(
1
)
;
}
casper
.
echo
(
"Will google.com load in less than "
+
timeout
+
"ms?"
);
casper
.
options
.
timeout
=
timeout
;
casper
.
start
(
"http://www.google.com/"
,
function
()
{
this
.
echo
(
"YES!"
,
'GREEN_BAR'
)
.
exit
()
;
this
.
echo
(
"YES!"
,
'GREEN_BAR'
);
this
.
exit
();
});
casper
.
run
();
...
...
Please
register
or
sign in
to post a comment