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
ae6f9edf
...
ae6f9edf2a872e2e7f3f07a21d385b6245d02b47
authored
2012-05-20 22:15:24 +0200
by
Brikou CARRE
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
wip revised code (based on coffee revision)
1 parent
6853db45
Hide whitespace changes
Inline
Side-by-side
Showing
23 changed files
with
207 additions
and
161 deletions
samples/cliplay.js
samples/customevents.coffee
samples/customevents.js
samples/customlogging.js
samples/download.coffee
samples/download.js
samples/each.js
samples/googlelinks.coffee
samples/googlelinks.js
samples/googlematch.coffee
samples/googlematch.js
samples/googlepagination.coffee
samples/googlepagination.js
samples/logcolor.coffee
samples/logcolor.js
samples/screenshot.coffee
samples/screenshot.js
samples/statushandlers.coffee
samples/statushandlers.js
samples/steptimeout.coffee
samples/steptimeout.js
samples/timeout.coffee
samples/timeout.js
samples/cliplay.js
View file @
ae6f9ed
...
...
@@ -18,4 +18,4 @@ dump(casper.cli.args);
casper
.
echo
(
"Casper CLI passed options:"
);
dump
(
casper
.
cli
.
options
);
casper
.
exit
();
\ No newline at end of file
casper
.
exit
();
...
...
samples/customevents.coffee
View file @
ae6f9ed
...
...
@@ -8,4 +8,4 @@ casper.start 'http://google.com/', ->
# emitting a custom event
@
emit
'google.loaded'
,
@
getTitle
()
casper
.
run
()
\ No newline at end of file
casper
.
run
()
...
...
samples/customevents.js
View file @
ae6f9ed
...
...
@@ -2,12 +2,12 @@ var casper = require('casper').create();
// listening to a custom event
casper
.
on
(
'google.loaded'
,
function
(
title
)
{
casper
.
echo
(
'Google page title is '
+
title
);
casper
.
echo
(
"Google page title is "
+
title
);
});
casper
.
start
(
'http://google.com/'
,
function
(
self
)
{
casper
.
start
(
'http://google.com/'
,
function
()
{
// emitting a custom event
self
.
emit
(
'google.loaded'
,
self
.
getTitle
());
this
.
emit
(
'google.loaded'
,
this
.
getTitle
());
});
casper
.
run
();
\ No newline at end of file
casper
.
run
();
...
...
samples/customlogging.js
View file @
ae6f9ed
/*
*
*
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
...
...
samples/download.coffee
View file @
ae6f9ed
### download the google logo image as base64 ###
###
Download the google logo image as base64
###
casper
=
require
(
'casper'
).
create
verbose
:
true
casper
.
start
'http://www.google.fr/'
,
->
@
echo
@
base64encode
'http://www.google.fr/images/srpr/logo3w.png'
casper
.
run
()
\ No newline at end of file
casper
.
run
()
...
...
samples/download.js
View file @
ae6f9ed
// download the google logo image as base64
/*
Download the google logo image as base64
*/
var
casper
=
require
(
'casper'
).
create
({
verbose
:
true
});
casper
.
start
(
'http://www.google.fr/'
,
function
(
self
)
{
self
.
echo
(
self
.
base64encode
(
'http://www.google.fr/images/srpr/logo3w.png'
));
casper
.
start
(
'http://www.google.fr/'
,
function
()
{
this
.
echo
(
this
.
base64encode
(
'http://www.google.fr/images/srpr/logo3w.png'
));
});
casper
.
run
();
...
...
samples/each.js
View file @
ae6f9ed
...
...
@@ -6,9 +6,11 @@ var links = [
'http://bing.com/'
];
casper
.
start
().
each
(
links
,
function
(
self
,
link
)
{
self
.
thenOpen
(
link
,
function
(
self
)
{
self
.
echo
(
self
.
getTitle
()
+
' - '
+
link
);
casper
.
start
();
casper
.
each
(
links
,
function
(
self
,
link
)
{
this
.
thenOpen
(
link
,
function
()
{
this
.
echo
(
""
+
(
this
.
getTitle
())
+
" - "
+
link
);
});
});
...
...
samples/googlelinks.coffee
View file @
ae6f9ed
getLinks
=
->
links
=
document
.
querySelectorAll
"h3.r a"
Array
::
map
.
call
links
,
(
e
)
->
e
.
getAttribute
"href"
links
=
document
.
querySelectorAll
"h3.r a"
Array
::
map
.
call
links
,
(
e
)
->
e
.
getAttribute
"href"
links
=
[]
casper
=
require
(
'casper'
).
create
()
casper
.
start
"http://google.fr/"
,
->
# search for 'casperjs' from google form
@
fill
'form[action="/search"]'
,
q
:
"casperjs"
,
true
# search for 'casperjs' from google form
@
fill
'form[action="/search"]'
,
q
:
"casperjs"
,
true
casper
.
then
->
# aggregate results for the 'casperjs' search
links
=
@
evaluate
getLinks
# search for 'phantomjs' from google form
@
fill
'form[action="/search"]'
,
q
:
"phantomjs"
,
true
# aggregate results for the 'casperjs' search
links
=
@
evaluate
getLinks
# search for 'phantomjs' from google form
@
fill
'form[action="/search"]'
,
q
:
"phantomjs"
,
true
casper
.
then
->
# concat results for the 'phantomjs' search
links
=
links
.
concat
@
evaluate
(
getLinks
)
# concat results for the 'phantomjs' search
links
=
links
.
concat
@
evaluate
(
getLinks
)
casper
.
run
->
# display results
@
echo
"
#{
links
.
length
}
links found:"
@
echo
" - "
+
links
.
join
"
\n
- "
@
exit
()
\ No newline at end of file
# display results
@
echo
"
#{
links
.
length
}
links found:"
@
echo
" - "
+
links
.
join
"
\n
- "
@
exit
()
...
...
samples/googlelinks.js
View file @
ae6f9ed
var
links
=
[];
var
casper
=
require
(
'casper'
).
create
();
function
getLinks
()
{
var
links
=
document
.
querySelectorAll
(
'h3.r a'
);
var
getLinks
=
function
()
{
var
links
;
links
=
document
.
querySelectorAll
(
"h3.r a"
);
return
Array
.
prototype
.
map
.
call
(
links
,
function
(
e
)
{
return
e
.
getAttribute
(
'href'
);
return
e
.
getAttribute
(
"href"
);
});
}
};
var
links
=
[];
var
casper
=
require
(
'casper'
).
create
();
casper
.
start
(
'http://google.fr/'
,
function
(
self
)
{
casper
.
start
(
"http://google.fr/"
,
function
(
)
{
// search for 'casperjs' from google form
self
.
fill
(
'form[action="/search"]'
,
{
q
:
'casperjs'
},
true
);
this
.
fill
(
'form[action="/search"]'
,
{
q
:
"casperjs"
},
true
);
});
casper
.
then
(
function
(
self
)
{
casper
.
then
(
function
()
{
// aggregate results for the 'casperjs' search
links
=
self
.
evaluate
(
getLinks
);
// now search for 'phantomjs' by fillin the form again
self
.
fill
(
'form[action="/search"]'
,
{
q
:
'phantomjs'
},
true
);
links
=
this
.
evaluate
(
getLinks
);
// search for 'phantomjs' from google form
this
.
fill
(
'form[action="/search"]'
,
{
q
:
"phantomjs"
},
true
);
});
casper
.
then
(
function
(
self
)
{
//
aggregate
results for the 'phantomjs' search
links
=
links
.
concat
(
self
.
evaluate
(
getLinks
));
casper
.
then
(
function
()
{
//
concat
results for the 'phantomjs' search
links
=
links
.
concat
(
this
.
evaluate
(
getLinks
));
});
casper
.
run
(
function
(
self
)
{
// echo results in some pretty fashion
self
.
echo
(
links
.
length
+
' links found:'
);
self
.
echo
(
' - '
+
links
.
join
(
'\n - '
)).
exit
();
casper
.
run
(
function
()
{
// display results
this
.
echo
(
""
+
links
.
length
+
" links found:"
);
this
.
echo
(
" - "
+
links
.
join
(
"\n - "
));
this
.
exit
();
});
...
...
samples/googlematch.coffee
View file @
ae6f9ed
...
...
@@ -3,11 +3,11 @@ Takes provided terms passed as arguments and query google for the number of
estimated results each have.
Usage:
$ casperjs samples/
googlematch.js nicolas chuck borris
nicolas: 69600000
chuck: 49500000
borris: 2370000
winner is "nicolas" with 69600000 results
$ casperjs
googlematch.js nicolas chuck borris
nicolas: 69600000
chuck: 49500000
borris: 2370000
winner is "nicolas" with 69600000 results
###
casper
=
require
(
'casper'
).
create
verbose
:
true
...
...
@@ -21,7 +21,7 @@ terms = casper.cli.args # terms are passed through command-line arguments
if
terms
.
length
<
3
casper
.
echo
'Usage: casperjs googlematch.js term1, term2 [, term3]...'
.
echo
(
"Usage: $ casperjs googlematch.js term1, term2 [, term3]..."
)
.
exit
(
1
)
scores
=
[]
...
...
samples/googlematch.js
View file @
ae6f9ed
/**
* Takes provided terms passed as arguments and query google for the number of
* estimated results each have.
*
* Usage:
* $ casperjs samples/googlematch.js nicolas chuck borris
* nicolas: 69600000
* chuck: 49500000
* borris: 2370000
* winner is "nicolas" with 69600000 results
*/
var
casper
=
new
require
(
'casper'
).
create
({
/*
Takes provided terms passed as arguments and query google for the number of
estimated results each have.
Usage:
$ casperjs googlematch.js nicolas chuck borris
nicolas: 69600000
chuck: 49500000
borris: 2370000
winner is "nicolas" with 69600000 results
*/
var
casper
=
require
(
'casper'
).
create
({
verbose
:
true
})
,
terms
=
casper
.
cli
.
args
,
scores
=
[],
i
=
0
;
});
casper
.
fetchScore
=
function
()
{
return
this
.
evaluate
(
function
()
{
...
...
@@ -20,14 +21,18 @@ casper.fetchScore = function() {
});
};
if
(
terms
.
length
<
2
)
{
var
terms
=
casper
.
cli
.
args
;
if
(
terms
.
length
<
3
)
{
casper
.
echo
(
'Usage: casperjs googlematch.js term1, term2 [, term3]...'
)
.
echo
(
"Usage: $ casperjs googlematch.js term1, term2 [, term3]..."
)
.
exit
(
1
)
;
}
casper
.
echo
(
'Let the match begin!'
);
var
scores
=
[];
casper
.
echo
(
"Let the match begin between \""
+
(
terms
.
join
(
'", "'
))
+
"\"!"
);
casper
.
start
(
"http://google.fr/"
);
...
...
@@ -46,7 +51,7 @@ casper.each(terms, function(self, term, i) {
casper
.
run
(
function
(
self
)
{
scores
.
sort
(
function
(
a
,
b
)
{
return
b
.
score
-
a
.
score
;
return
b
.
score
-
a
.
score
;
});
var
winner
=
scores
[
0
];
self
.
echo
(
'winner is "'
+
winner
.
term
+
'" with '
+
winner
.
score
+
' results'
);
...
...
samples/googlepagination.coffee
View file @
ae6f9ed
# Capture multiple pages of google search results
#
# usage: casperjs googlepagination.coffee my search terms
#
# (all arguments will be used as the query)
###
Capture multiple pages of google search results
usage: casperjs googlepagination.coffee my search terms
(all arguments will be used as the query)
###
casper
=
require
(
'casper'
).
create
()
currentPage
=
1
if
casper
.
cli
.
args
.
length
==
0
if
casper
.
cli
.
args
.
length
is
0
casper
.
echo
"usage: $ casperjs my search terms"
.
echo
(
"Usage: $ casperjs my search terms"
)
.
exit
(
1
)
processPage
=
->
...
...
samples/googlepagination.js
View file @
ae6f9ed
var
casper
,
currentPage
,
processPage
;
/*
Capture multiple pages of google search results
casper
=
require
(
'casper'
).
create
();
currentPage
=
1
;
Usage: casperjs googlepagination.coffee my search terms
(all arguments will be used as the query)
*/
var
casper
=
require
(
'casper'
).
create
();
var
currentPage
=
1
;
if
(
casper
.
cli
.
args
.
length
===
0
)
{
casper
.
echo
(
"u
sage: $ casperjs my search terms"
)
.
exit
(
1
)
;
casper
.
echo
(
"U
sage: $ casperjs my search terms"
)
.
exit
(
1
)
;
}
processPage
=
function
()
{
var
url
;
this
.
echo
(
"capturing page "
+
currentPage
);
this
.
capture
(
"google-results-p"
+
currentPage
+
".png"
);
if
(
currentPage
>=
5
)
{
return
;
}
if
(
this
.
exists
(
"#pnnext"
))
{
currentPage
++
;
this
.
echo
(
"requesting next page: "
+
currentPage
);
url
=
this
.
getCurrentUrl
();
return
this
.
thenClick
(
"#pnnext"
).
then
(
function
()
{
return
this
.
waitFor
(
function
()
{
return
url
!==
this
.
getCurrentUrl
();
},
processPage
);
});
}
else
{
return
this
.
echo
(
"that's all, folks."
);
}
var
processPage
=
function
()
{
var
url
;
this
.
echo
(
"capturing page "
+
currentPage
);
this
.
capture
(
"google-results-p"
+
currentPage
+
".png"
);
if
(
currentPage
>=
5
)
{
return
;
}
if
(
this
.
exists
(
"#pnnext"
))
{
currentPage
++
;
this
.
echo
(
"requesting next page: "
+
currentPage
);
url
=
this
.
getCurrentUrl
();
return
this
.
thenClick
(
"#pnnext"
).
then
(
function
()
{
return
this
.
waitFor
(
function
()
{
return
url
!==
this
.
getCurrentUrl
();
},
processPage
);
});
}
else
{
return
this
.
echo
(
"that's all, folks."
);
}
};
casper
.
start
(
'http://google.fr/'
,
function
()
{
return
this
.
fill
(
'form[action="/search"]'
,
{
q
:
casper
.
cli
.
args
.
join
(
' '
)
},
true
);
return
this
.
fill
(
'form[action="/search"]'
,
{
q
:
casper
.
cli
.
args
.
join
(
' '
)
},
true
);
});
casper
.
then
(
processPage
);
...
...
samples/logcolor.coffee
View file @
ae6f9ed
casper
=
require
(
'casper'
).
create
verbose
:
true
,
logLevel
:
'debug'
casper
=
require
(
'casper'
).
create
verbose
:
true
logLevel
:
'debug'
casper
.
log
'this is a debug message'
,
'debug'
casper
.
log
'and an informative one'
,
'info'
...
...
samples/logcolor.js
View file @
ae6f9ed
...
...
@@ -7,4 +7,5 @@ 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/screenshot.coffee
View file @
ae6f9ed
### This script will capture a screenshot of a twitter account page
Usage $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>
###
This script will capture a screenshot of a twitter account page
Usage: $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>
###
casper
=
require
(
'casper'
).
create
viewportSize
:
width
:
1024
...
...
@@ -12,7 +13,7 @@ filename = casper.cli.get 1
if
not
twitterAccount
or
not
filename
or
not
/\.(png|jpg|pdf)$/i
.
test
filename
casper
.
echo
"Usage $ casperjs samples/screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>"
.
echo
(
"Usage: $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>"
)
.
exit
(
1
)
casper
.
start
"https://twitter.com/#!/
#{
twitterAccount
}
"
,
->
...
...
samples/screenshot.js
View file @
ae6f9ed
/*
*
*
This script will capture a screenshot of a twitter account page
*
* Usage $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>
*/
/*
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
({
viewportSize
:
{
width
:
1024
,
...
...
@@ -15,7 +15,7 @@ var filename = casper.cli.get(1);
if
(
!
twitterAccount
||
!
filename
||
!
/
\.(
png|jpg|pdf
)
$/i
.
test
(
filename
))
{
casper
.
echo
(
"Usage
$ casperjs samples/
screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>"
)
.
echo
(
"Usage
: $ casperjs
screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>"
)
.
exit
(
1
)
;
}
...
...
samples/statushandlers.coffee
View file @
ae6f9ed
###
This script will add a custom HTTP status code handler, here for 404 pages.
###
casper
=
require
(
'casper'
).
create
httpStatusHandlers
:
404
:
(
self
,
resource
)
->
@
echo
"Resource at
#{
resource
.
url
}
not found (404)"
,
"COMMENT"
verbose
:
true
casper
.
start
'http://www.google.com/plop'
,
->
@
echo
'Done.'
@
echo
"Done."
@
exit
()
casper
.
run
()
...
...
samples/statushandlers.js
View file @
ae6f9ed
/*
*
*
This script will add a custom HTTP status code handler, here for 404 pages.
*
*/
/*
This script will add a custom HTTP status code handler, here for 404 pages.
*/
var
casper
=
require
(
'casper'
).
create
({
httpStatusHandlers
:
{
404
:
function
(
self
,
resource
)
{
self
.
echo
(
'Resource at '
+
resource
.
url
+
' not found (404)'
,
'COMMENT'
);
this
.
echo
(
"Resource at "
+
resource
.
url
+
" not found (404)"
,
"COMMENT"
);
}
},
verbose
:
true
});
casper
.
start
(
'http://www.google.com/plop'
,
function
(
self
)
{
self
.
echo
(
'Done.'
).
exit
();
casper
.
start
(
'http://www.google.com/plop'
,
function
()
{
this
.
echo
(
"Done."
);
this
.
exit
();
});
casper
.
run
();
...
...
samples/steptimeout.coffee
View file @
ae6f9ed
...
...
@@ -27,4 +27,5 @@ casper.each links, (self, link) ->
else
@
test
.
pass
"
#{
@
requestUrl
}
loaded in less than
#{
timeout
}
ms."
casper
.
run
->
@
test
.
renderResults
true
casper
.
run
->
@
test
.
renderResults
true
...
...
samples/steptimeout.js
View file @
ae6f9ed
var
failed
=
[];
var
casper
=
require
(
'casper'
).
create
({
onStepTimeout
:
function
(
self
)
{
failed
.
push
(
self
.
requestUrl
);
onStepTimeout
:
function
()
{
failed
.
push
(
this
.
requestUrl
);
}
});
...
...
@@ -15,9 +15,12 @@ var links = [
];
var
timeout
=
~~
casper
.
cli
.
get
(
0
);
casper
.
options
.
stepTimeout
=
timeout
>
0
?
timeout
:
1000
;
if
(
timeout
<
1
)
{
timeout
=
1000
;
}
casper
.
options
.
stepTimeout
=
timeout
;
casper
.
echo
(
'Testing with timeout='
+
casper
.
options
.
stepTimeout
+
'ms.'
);
casper
.
echo
(
"Testing with timeout="
+
casper
.
options
.
stepTimeout
+
"ms."
);
casper
.
start
();
...
...
@@ -33,6 +36,6 @@ casper.each(links, function(self, link) {
});
});
casper
.
run
(
function
(
self
)
{
self
.
test
.
renderResults
(
true
);
casper
.
run
(
function
()
{
this
.
test
.
renderResults
(
true
);
});
...
...
samples/timeout.coffee
View file @
ae6f9ed
...
...
@@ -14,6 +14,7 @@ $ casperjs samples/timeout.js 2000
Will google.com load in less than 2000ms?
YES!
###
casper
=
require
(
'casper'
).
create
onTimeout
:
->
@
echo
'NOPE.'
,
'RED_BAR'
...
...
@@ -22,7 +23,7 @@ casper = require('casper').create
timeout
=
~~
casper
.
cli
.
get
0
if
timeout
<
1
casper
.
echo
"You must pass a valid timeout value"
.
echo
(
"You must pass a valid timeout value"
)
.
exit
(
1
)
casper
.
echo
"Will google.com load in less than
#{
timeout
}
ms?"
...
...
samples/timeout.js
View file @
ae6f9ed
/**
* Just a silly game.
*
* $ casperjs samples/timeout.js 500
* Will google.com load in less than 500ms?
* NOPE.
* $ casperjs samples/timeout.js 1000
* Will google.com load in less than 1000ms?
* NOPE.
* $ casperjs samples/timeout.js 1500
* Will google.com load in less than 1500ms?
* NOPE.
* $ casperjs samples/timeout.js 2000
* Will google.com load in less than 2000ms?
* YES!
*/
/*
Just a silly game.
$ casperjs samples/timeout.js 500
Will google.com load in less than 500ms?
NOPE.
$ casperjs samples/timeout.js 1000
Will google.com load in less than 1000ms?
NOPE.
$ casperjs samples/timeout.js 1500
Will google.com load in less than 1500ms?
NOPE.
$ casperjs samples/timeout.js 2000
Will google.com load in less than 2000ms?
YES!
*/
var
casper
=
require
(
'casper'
).
create
({
onTimeout
:
function
(
self
)
{
self
.
echo
(
'NOPE.'
,
'RED_BAR'
).
exit
();
onTimeout
:
function
()
{
this
.
echo
(
'NOPE.'
,
'RED_BAR'
)
.
exit
()
;
}
});
var
timeout
=
~~
casper
.
cli
.
get
(
0
);
if
(
timeout
<
1
)
{
casper
.
echo
(
'You must pass a valid timeout value'
)
.
echo
(
"You must pass a valid timeout value"
)
.
exit
(
1
)
;
}
casper
.
echo
(
'Will google.com load in less than '
+
timeout
+
'ms?'
);
casper
.
echo
(
"Will google.com load in less than "
+
timeout
+
"ms?"
);
casper
.
options
.
timeout
=
timeout
;
casper
.
start
(
'http://www.google.com/'
,
function
(
self
)
{
self
.
echo
(
'YES!'
,
'GREEN_BAR'
).
exit
();
casper
.
start
(
'http://www.google.com/'
,
function
()
{
this
.
echo
(
'YES!'
,
'GREEN_BAR'
)
.
exit
()
;
});
casper
.
run
();
...
...
Please
register
or
sign in
to post a comment