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
941ba94c
...
941ba94c28e30f72da3a85582971a1248f90b668
authored
2012-05-28 00:45:13 +0200
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
revamped many samples
1 parent
1fcccc63
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
133 additions
and
112 deletions
samples/customevents.js
samples/customlogging.js
samples/download.js
samples/dynamic.js
samples/googlelinks.js
samples/googlematch.coffee
samples/googlematch.js
samples/googletesting.js
samples/multirun.js
samples/steptimeout.coffee
samples/steptimeout.js
samples/timeout.js
samples/customevents.js
View file @
941ba94
...
...
@@ -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
);
this
.
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 @
941ba94
...
...
@@ -31,6 +31,6 @@ var casper = require('casper').create({
casper
.
logLevels
=
[
'verbose'
].
concat
(
casper
.
logLevels
);
// test our new logger with google
casper
.
start
(
'http://www.google.com/'
).
run
(
function
(
self
)
{
self
.
exit
();
casper
.
start
(
'http://www.google.com/'
).
run
(
function
()
{
this
.
exit
();
});
...
...
samples/download.js
View file @
941ba94
...
...
@@ -4,8 +4,8 @@ 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
(
self
.
base64encode
(
'http://www.google.fr/images/srpr/logo3w.png'
));
});
casper
.
run
();
...
...
samples/dynamic.js
View file @
941ba94
...
...
@@ -4,6 +4,7 @@ if (phantom.casperArgs.args.length !== 1) {
}
var
casper
=
require
(
'casper'
).
create
({
logLevel
:
"debug"
,
verbose
:
true
});
...
...
@@ -30,38 +31,38 @@ var links = [
];
// Just opens the page and prints the title
var
start
=
function
(
self
,
link
)
{
self
.
start
(
link
,
function
(
self
)
{
self
.
echo
(
'Page title: '
+
self
.
getTitle
());
var
start
=
function
(
link
)
{
this
.
start
(
link
,
function
(
)
{
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
(
link
)
{
this
.
then
(
function
(
self
)
{
var
found
=
self
.
evaluate
(
searchLinks
);
self
.
echo
(
found
.
length
+
" links found on "
+
link
);
this
.
then
(
function
()
{
var
found
=
this
.
evaluate
(
searchLinks
);
this
.
echo
(
found
.
length
+
" links found on "
+
link
);
links
=
links
.
concat
(
found
);
});
};
casper
.
start
().
then
(
function
(
self
)
{
self
.
echo
(
'Starting'
);
casper
.
start
().
then
(
function
()
{
this
.
echo
(
'Starting'
);
});
var
currentLink
=
0
;
// As long as it has a next link, and is under the maximum limit, will keep running
function
check
(
self
)
{
function
check
()
{
if
(
links
[
currentLink
]
&&
currentLink
<
upTo
)
{
self
.
echo
(
'--- Link '
+
currentLink
+
' ---'
);
start
(
self
,
links
[
currentLink
]);
addLinks
.
call
(
self
,
links
[
currentLink
]);
this
.
echo
(
'--- Link '
+
currentLink
+
' ---'
);
start
.
call
(
this
,
links
[
currentLink
]);
addLinks
.
call
(
this
,
links
[
currentLink
]);
currentLink
++
;
self
.
run
(
check
);
this
.
run
(
check
);
}
else
{
self
.
echo
(
'All done.'
).
exit
();
this
.
echo
(
'All done.'
).
exit
();
}
}
...
...
samples/googlelinks.js
View file @
941ba94
...
...
@@ -8,25 +8,25 @@ function getLinks() {
});
}
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
);
links
=
this
.
evaluate
(
getLinks
);
// now search for 'phantomjs' by fillin the form again
self
.
fill
(
'form[action="/search"]'
,
{
q
:
'phantomjs'
},
true
);
this
.
fill
(
'form[action="/search"]'
,
{
q
:
'phantomjs'
},
true
);
});
casper
.
then
(
function
(
self
)
{
casper
.
then
(
function
()
{
// aggregate results for the 'phantomjs' search
links
=
links
.
concat
(
self
.
evaluate
(
getLinks
));
links
=
links
.
concat
(
this
.
evaluate
(
getLinks
));
});
casper
.
run
(
function
(
self
)
{
casper
.
run
(
function
()
{
// echo results in some pretty fashion
self
.
echo
(
links
.
length
+
' links found:'
);
self
.
echo
(
' - '
+
links
.
join
(
'\n - '
)).
exit
();
this
.
echo
(
links
.
length
+
' links found:'
);
this
.
echo
(
' - '
+
links
.
join
(
'\n - '
)).
exit
();
});
...
...
samples/googlematch.coffee
View file @
941ba94
...
...
@@ -35,7 +35,7 @@ casper.each terms, (self, term) ->
@
then
->
score
=
@
fetchScore
()
scores
.
push
term
:
term
,
score
:
score
self
.
echo
"
#{
term
}
:
#{
score
}
"
@
echo
"
#{
term
}
:
#{
score
}
"
casper
.
run
->
winner
=
scores
[
0
]
...
...
samples/googlematch.js
View file @
941ba94
...
...
@@ -16,7 +16,7 @@ var casper = new require('casper').create({
casper
.
fetchScore
=
function
()
{
return
this
.
evaluate
(
function
()
{
var
result
=
document
.
querySelector
(
'#resultStats'
).
innerText
;
return
parseInt
(
/Environ
([
0-9
\s]{1,})
.*/
.
exec
(
result
)[
1
].
replace
(
/
\s
/g
,
''
));
return
parseInt
(
/Environ
([
0-9
\s]{1,})
.*/
.
exec
(
result
)[
1
].
replace
(
/
\s
/g
,
''
)
,
10
);
});
};
...
...
@@ -31,24 +31,31 @@ casper.echo('Let the match begin!');
casper
.
start
(
"http://google.fr/"
);
casper
.
each
(
terms
,
function
(
self
,
term
,
i
)
{
self
.
then
(
function
(
self
)
{
self
.
fill
(
'form[action="/search"]'
,
{
q
:
term
},
true
);
}).
then
(
function
(
self
)
{
var
score
=
self
.
fetchScore
();
casper
.
each
(
terms
,
function
(
casper
,
term
,
i
)
{
this
.
echo
(
'Fecthing score for '
+
term
);
this
.
then
(
function
()
{
this
.
fill
(
'form[action="/search"]'
,
{
q
:
term
},
true
);
});
this
.
then
(
function
()
{
var
score
=
this
.
fetchScore
();
scores
.
push
({
term
:
term
,
score
:
score
});
self
.
echo
(
term
+
': '
+
score
);
this
.
echo
(
term
+
': '
+
score
);
});
});
casper
.
run
(
function
(
self
)
{
casper
.
run
(
function
()
{
if
(
scores
.
length
===
0
)
{
this
.
echo
(
'No result found'
).
exit
();
}
var
winner
=
scores
[
0
];
for
(
var
i
=
0
,
len
=
scores
.
length
;
i
<
len
;
i
++
)
if
(
scores
[
i
].
score
>
winner
.
score
)
winner
=
scores
[
i
];
self
.
echo
(
'winner is "'
+
winner
.
term
+
'" with '
+
winner
.
score
+
' results'
);
self
.
exit
();
scores
.
forEach
(
function
(
score
)
{
if
(
score
.
score
>
winner
.
score
)
{
winner
=
score
.
score
;
}
});
this
.
echo
(
'winner is "'
+
winner
.
term
+
'" with '
+
winner
.
score
+
' results'
);
this
.
exit
();
});
...
...
samples/googletesting.js
View file @
941ba94
...
...
@@ -2,22 +2,22 @@ var 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"]'
,
{
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
()
{
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/multirun.js
View file @
941ba94
...
...
@@ -7,45 +7,45 @@ function countLinks() {
}
var
suites
=
[
function
(
self
)
{
self
.
echo
(
'Suite 1'
);
self
.
start
(
'http://google.com/'
,
function
(
self
)
{
self
.
echo
(
'Page title: '
+
self
.
getTitle
());
}).
then
(
function
(
self
)
{
self
.
echo
(
self
.
evaluate
(
countLinks
)
+
' links'
);
function
()
{
this
.
echo
(
'Suite 1'
);
this
.
start
(
'http://google.com/'
,
function
(
)
{
this
.
echo
(
'Page title: '
+
this
.
getTitle
());
}).
then
(
function
()
{
this
.
echo
(
this
.
evaluate
(
countLinks
)
+
' links'
);
});
},
function
(
self
)
{
self
.
echo
(
'Suite 2'
);
self
.
start
(
'http://yahoo.com/'
,
function
(
self
)
{
self
.
echo
(
'Page title: '
+
self
.
getTitle
());
}).
then
(
function
(
self
)
{
self
.
echo
(
self
.
evaluate
(
countLinks
)
+
' links'
);
function
()
{
this
.
echo
(
'Suite 2'
);
this
.
start
(
'http://yahoo.com/'
,
function
(
)
{
this
.
echo
(
'Page title: '
+
this
.
getTitle
());
}).
then
(
function
()
{
this
.
echo
(
this
.
evaluate
(
countLinks
)
+
' links'
);
});
},
function
(
self
)
{
self
.
echo
(
'Suite 3'
);
self
.
start
(
'http://bing.com/'
,
function
(
self
)
{
self
.
echo
(
'Page title: '
+
self
.
getTitle
());
}).
then
(
function
(
self
)
{
self
.
echo
(
self
.
evaluate
(
countLinks
)
+
' links'
);
function
()
{
this
.
echo
(
'Suite 3'
);
this
.
start
(
'http://bing.com/'
,
function
(
)
{
this
.
echo
(
'Page title: '
+
this
.
getTitle
());
}).
then
(
function
()
{
this
.
echo
(
this
.
evaluate
(
countLinks
)
+
' links'
);
});
}
];
casper
.
start
().
then
(
function
(
self
)
{
self
.
echo
(
'Starting'
);
casper
.
start
().
then
(
function
()
{
this
.
echo
(
'Starting'
);
});
var
currentSuite
=
0
;
function
check
(
self
)
{
function
check
()
{
if
(
suites
[
currentSuite
])
{
suites
[
currentSuite
]
(
casper
);
suites
[
currentSuite
]
.
call
(
this
);
currentSuite
++
;
casper
.
run
(
check
);
}
else
{
self
.
echo
(
'All done.'
).
exit
();
this
.
echo
(
'All done.'
).
exit
();
}
}
...
...
samples/steptimeout.coffee
View file @
941ba94
failed
=
[];
casper
=
require
(
'casper'
).
create
onStepTimeout
:
->
failed
.
push
@
requestUrl
failed
=
[]
start
=
null
links
=
[
'http://google.com/'
'http://akei.com/'
...
...
@@ -11,20 +8,29 @@ links = [
'http://cdiscount.fr/'
]
casper
=
require
(
'casper'
).
create
onStepTimeout
:
->
failed
.
push
@
requestUrl
@
test
.
fail
"
#{
@
requestUrl
}
loads in less than
#{
timeout
}
ms."
casper
.
on
'load.finished'
,
->
@
echo
"
#{
@
requestUrl
}
loaded in
#{
new
Date
()
-
start
}
ms"
,
'PARAMETER'
timeout
=
~~
casper
.
cli
.
get
(
0
)
timeout
=
1000
if
timeout
<
1
casper
.
options
.
stepTimeout
=
timeout
casper
.
echo
"Testing with timeout=
#{
casper
.
options
.
stepTimeout
}
ms
."
casper
.
echo
"Testing with timeout=
#{
timeout
}
ms, please be patient
."
casper
.
start
()
casper
.
each
links
,
(
self
,
link
)
->
@
test
.
comment
"Adding
#{
link
}
to test suite"
@
thenOpen
link
,
->
if
@
requestUrl
in
failed
@
test
.
fail
"
#{
@
requestUrl
}
loaded in less than
#{
timeout
}
ms."
else
@
then
->
@
test
.
comment
"Loading
#{
link
}
"
start
=
new
Date
()
@
open
link
@
then
->
if
@
requestUrl
not
in
failed
@
test
.
pass
"
#{
@
requestUrl
}
loaded in less than
#{
timeout
}
ms."
casper
.
run
->
@
test
.
renderResults
true
...
...
samples/steptimeout.js
View file @
941ba94
var
failed
=
[];
var
casper
=
require
(
'casper'
).
create
({
onStepTimeout
:
function
(
self
)
{
failed
.
push
(
self
.
requestUrl
);
}
});
var
start
=
null
;
var
links
=
[
'http://google.com/'
,
'http://akei.com/'
,
...
...
@@ -14,25 +8,38 @@ var links = [
'http://cdiscount.fr/'
];
var
casper
=
require
(
'casper'
).
create
({
onStepTimeout
:
function
()
{
failed
.
push
(
this
.
requestUrl
);
this
.
test
.
fail
(
this
.
requestUrl
+
" loads in less than "
+
timeout
+
"ms."
);
}
});
casper
.
on
(
'load.finished'
,
function
()
{
this
.
echo
(
this
.
requestUrl
+
' loaded in '
+
(
new
Date
()
-
start
)
+
'ms'
,
'PARAMETER'
);
});
var
timeout
=
~~
casper
.
cli
.
get
(
0
);
casper
.
options
.
stepTimeout
=
timeout
>
0
?
timeout
:
1000
;
casper
.
echo
(
'Testing with timeout='
+
casper
.
options
.
stepTimeout
+
'ms.'
);
casper
.
echo
(
'Testing with timeout='
+
casper
.
options
.
stepTimeout
+
'ms
, please be patient
.'
);
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
);
}
else
{
self
.
test
.
pass
(
self
.
requestUrl
);
casper
.
each
(
links
,
function
(
casper
,
link
)
{
this
.
then
(
function
()
{
this
.
test
.
comment
(
'Loading '
+
link
);
start
=
new
Date
();
this
.
open
(
link
);
});
this
.
then
(
function
()
{
var
message
=
this
.
requestUrl
+
" loads in less than "
+
timeout
+
"ms."
;
if
(
failed
.
indexOf
(
this
.
requestUrl
)
===
-
1
)
{
this
.
test
.
pass
(
message
);
}
});
});
casper
.
run
(
function
(
self
)
{
self
.
test
.
renderResults
(
true
);
casper
.
run
(
function
()
{
this
.
test
.
renderResults
(
true
);
});
...
...
samples/timeout.js
View file @
941ba94
...
...
@@ -15,8 +15,8 @@
* YES!
*/
var
casper
=
require
(
'casper'
).
create
({
onTimeout
:
function
(
self
)
{
self
.
echo
(
'NOPE.'
,
'RED_BAR'
).
exit
();
onTimeout
:
function
()
{
this
.
echo
(
'NOPE.'
,
'RED_BAR'
).
exit
();
}
});
...
...
@@ -30,8 +30,8 @@ if (timeout < 1) {
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