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
16a44136
...
16a4413619e3964a61bb7c3ec87e86525ab03457
authored
2012-01-08 11:04:50 +0100
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
marked Casper.extend() as obsolete; updated samples/extends.(js|coffee) samples
1 parent
4c048072
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
80 additions
and
105 deletions
modules/casper.js
modules/utils.js
samples/extends.coffee
samples/extends.js
modules/casper.js
View file @
16a4413
...
...
@@ -693,7 +693,6 @@ Casper.prototype.open = function(location, settings) {
}
}
this
.
emit
(
'open'
,
this
.
requestUrl
,
settings
);
//this.page.open(this.requestUrl, settings.method, settings.data);
this
.
page
.
openUrl
(
this
.
requestUrl
,
{
operation
:
settings
.
method
,
data
:
settings
.
data
...
...
@@ -759,7 +758,7 @@ Casper.prototype.run = function(onComplete, time) {
}
this
.
log
(
f
(
"Running suite: %d step%s"
,
this
.
steps
.
length
,
this
.
steps
.
length
>
1
?
"s"
:
""
),
"info"
);
this
.
emit
(
'run.start'
);
this
.
checker
=
setInterval
(
this
.
checkStep
,
(
time
?
time
:
25
0
),
this
,
onComplete
);
this
.
checker
=
setInterval
(
this
.
checkStep
,
(
time
?
time
:
10
0
),
this
,
onComplete
);
return
this
;
};
...
...
@@ -939,8 +938,8 @@ Casper.prototype.thenClick = function(selector, then, fallbackToHref) {
if
(
arguments
.
length
>
2
)
{
this
.
emit
(
"deprecated"
,
"The thenClick() method does not process the fallbackToHref argument since 0.6"
);
}
this
.
then
(
function
(
self
)
{
self
.
click
(
selector
);
this
.
then
(
function
()
{
this
.
click
(
selector
);
});
return
utils
.
isFunction
(
then
)
?
this
.
then
(
then
)
:
this
;
};
...
...
@@ -955,8 +954,8 @@ Casper.prototype.thenClick = function(selector, then, fallbackToHref) {
* @see Casper#evaluate
*/
Casper
.
prototype
.
thenEvaluate
=
function
(
fn
,
context
)
{
return
this
.
then
(
function
(
self
)
{
self
.
evaluate
(
fn
,
context
);
return
this
.
then
(
function
()
{
this
.
evaluate
(
fn
,
context
);
});
};
...
...
@@ -969,8 +968,8 @@ Casper.prototype.thenEvaluate = function(fn, context) {
* @see Casper#open
*/
Casper
.
prototype
.
thenOpen
=
function
(
location
,
then
)
{
this
.
then
(
this
.
createStep
(
function
(
self
)
{
self
.
open
(
location
);
this
.
then
(
this
.
createStep
(
function
()
{
this
.
open
(
location
);
},
{
skipLog
:
true
}));
...
...
@@ -1030,15 +1029,15 @@ Casper.prototype.wait = function(timeout, then) {
if
(
then
&&
!
utils
.
isFunction
(
then
))
{
this
.
die
(
"wait() a step definition must be a function"
);
}
return
this
.
then
(
function
(
self
)
{
self
.
waitStart
();
setTimeout
(
function
()
{
return
this
.
then
(
function
()
{
this
.
waitStart
();
setTimeout
(
function
(
self
)
{
self
.
log
(
f
(
"wait() finished wating for %dms."
,
timeout
),
"info"
);
if
(
then
)
{
then
.
call
(
self
,
self
);
}
self
.
waitDone
();
},
timeout
);
},
timeout
,
this
);
});
};
...
...
@@ -1069,8 +1068,8 @@ Casper.prototype.waitFor = function(testFx, then, onTimeout, timeout) {
if
(
then
&&
!
utils
.
isFunction
(
then
))
{
this
.
die
(
"waitFor() next step definition must be a function"
);
}
return
this
.
then
(
function
(
self
)
{
self
.
waitStart
();
return
this
.
then
(
function
()
{
this
.
waitStart
();
var
start
=
new
Date
().
getTime
();
var
condition
=
false
;
var
interval
=
setInterval
(
function
(
self
,
testFx
,
timeout
,
onTimeout
)
{
...
...
@@ -1095,7 +1094,7 @@ Casper.prototype.waitFor = function(testFx, then, onTimeout, timeout) {
clearInterval
(
interval
);
}
}
},
100
,
self
,
testFx
,
timeout
,
onTimeout
);
},
100
,
this
,
testFx
,
timeout
,
onTimeout
);
});
};
...
...
@@ -1187,8 +1186,11 @@ Casper.prototype.waitWhileVisible = function(selector, then, onTimeout, timeout)
* Extends Casper's prototype with provided one.
*
* @param Object proto Prototype methods to add to Casper
* @deprecated
* @since 0.6
*/
Casper
.
extend
=
function
(
proto
)
{
console
.
warn
(
'Casper.extend() has been deprecated since 0.6; check the docs'
);
if
(
!
utils
.
isObject
(
proto
))
{
throw
new
Error
(
"extends() only accept objects as prototypes"
);
}
...
...
modules/utils.js
View file @
16a4413
...
...
@@ -131,28 +131,27 @@ function format(f) {
exports
.
format
=
format
;
/**
* Inherit the prototype methods from one constructor into another.
* Inherit the prototype methods from one constructor into another, also
* exposes the `__super__` property to child class.
*
* The Function.prototype.inherits from lang.js rewritten as a standalone
* function (not on Function.prototype). NOTE: If this file is to be loaded
* during bootstrapping this function needs to be revritten using some native
* functions as prototype setup using normal JavaScript does not work as
* expected during bootstrapping (see mirror.js in r114903).
*
* @param {function} ctor Constructor function which needs to inherit the
* prototype.
* @param {function} superCtor Constructor function to inherit prototype from.
* @param Function child Constructor function which needs to inherit the
* prototype.
* @param Function parent Constructor function to inherit prototype from.
* @return Function The child class
*/
function
inherits
(
ctor
,
superCtor
)
{
ctor
.
super_
=
superCtor
;
ctor
.
prototype
=
Object
.
create
(
superCtor
.
prototype
,
{
constructor
:
{
value
:
ctor
,
enumerable
:
false
,
writable
:
true
,
configurable
:
true
function
inherits
(
child
,
parent
)
{
for
(
var
key
in
parent
)
{
if
(
Object
.
prototype
.
hasOwnProperty
.
call
(
parent
,
key
))
{
child
[
key
]
=
parent
[
key
];
}
});
}
function
ctor
()
{
this
.
constructor
=
child
;
}
ctor
.
prototype
=
parent
.
prototype
;
child
.
prototype
=
new
ctor
();
child
.
__super__
=
parent
.
prototype
;
return
child
;
}
exports
.
inherits
=
inherits
;
...
...
samples/extends.coffee
View file @
16a4413
Casper
=
require
(
'casper'
).
Casper
links
=
'http://edition.cnn.com/'
:
0
'http://www.nytimes.com/'
:
0
'http://www.bbc.co.uk/'
:
0
'http://www.guardian.co.uk/'
:
0
articles
=
[]
class
Fantomas
extends
require
(
'casper'
).
Casper
countLinks
:
->
@
evaluate
->
__utils__
.
findAll
(
'a'
).
length
###
Adds two new methods to the Casper prototype: fetchTexts and renderJSON.
###
Casper
.
extend
# Adds a new navigation step for casper; basically it will:
#
# 1. open an url,
# 2. on loaded, will fetch all contents retrieved through the provided
# CSS3 selector and return them in a formatted object.
fetchTexts
:
(
location
,
selector
)
->
@
thenOpen
location
,
->
fetch
=
(
selector
)
->
elements
=
document
.
querySelectorAll
(
selector
)
Array
::
map
.
call
elements
,
(
e
)
->
e
.
innerText
texts
=
@
evaluate
fetch
,
selector
:
selector
articles
=
articles
.
concat
texts
# Echoes a JSON output of the fetched results and exits phantomjs.
renderJSON
:
(
what
)
->
@
echo
JSON
.
stringify
what
,
null
,
' '
@
exit
()
casper
=
new
Casper
fantomas
=
new
Fantomas
loadImages
:
false
loadPlugins
:
false
logLevel
:
"debug"
verbose
:
true
casper
.
start
()
# all article titles are stored in <h3>
casper
.
fetchTexts
'http://www.liberation.fr/'
,
'h3'
fantomas
.
start
()
# all article titles are stored in <h2 class="article">
casper
.
fetchTexts
'http://www.lemonde.fr/'
,
'h2.article'
for
url
of
links
do
(
url
)
->
fantomas
.
thenOpen
url
,
->
links
[
url
]
=
@
countLinks
()
casper
.
run
->
@
renderJSON
articles
fantomas
.
run
->
@
renderJSON
links
@
exit
()
...
...
samples/extends.js
View file @
16a4413
var
Casper
=
require
(
"casper"
).
Casper
;
var
casper
=
require
(
'casper'
).
create
({
loadImages
:
false
,
logLevel
:
"debug"
,
verbose
:
true
});
/**
* Adds two new methods to the Casper prototype: fetchTexts and renderJSON.
*/
Casper
.
extend
({
/**
* Adds a new navigation step for casper; basically it will:
*
* 1. open an url,
* 2. on loaded, will fetch all contents retrieved through the provided
* CSS3 selector and return them in a formatted object.
*/
fetchTexts
:
function
(
selector
)
{
return
this
.
evaluate
(
function
(
selector
)
{
var
elements
=
document
.
querySelectorAll
(
selector
);
return
Array
.
prototype
.
map
.
call
(
elements
,
function
(
e
)
{
return
e
.
innerText
;
});
},
{
selector
:
selector
});
},
var
links
=
{
'http://edition.cnn.com/'
:
0
,
'http://www.nytimes.com/'
:
0
,
'http://www.bbc.co.uk/'
:
0
,
'http://www.guardian.co.uk/'
:
0
};
/**
* Echoes a JSON output of the fetched results and exits phantomjs.
*/
renderJSON
:
function
(
what
)
{
return
this
.
echo
(
JSON
.
stringify
(
what
,
null
,
' '
)).
exit
();
}
});
var
fantomas
=
Object
.
create
(
casper
);
var
casper
=
new
Casper
({
loadImages
:
false
,
loadPlugins
:
false
,
logLevel
:
"debug"
,
verbose
:
true
});
fantomas
.
countLinks
=
function
(
selector
)
{
return
this
.
evaluate
(
function
()
{
return
__utils__
.
findAll
(
'a[href]'
).
length
;
});
};
var
articles
=
[];
fantomas
.
renderJSON
=
function
(
what
)
{
return
this
.
echo
(
JSON
.
stringify
(
what
,
null
,
' '
));
};
casper
.
start
(
'http://www.liberation.fr/'
,
function
()
{
articles
=
this
.
fetchTexts
(
'h3'
);
});
fantomas
.
start
();
casper
.
thenOpen
(
'http://www.lemonde.fr/'
,
function
()
{
articles
.
concat
(
this
.
fetchTexts
(
'h2.article'
));
Object
.
keys
(
links
).
forEach
(
function
(
url
)
{
fantomas
.
thenOpen
(
url
,
function
()
{
links
[
url
]
=
this
.
countLinks
();
});
});
casper
.
run
(
function
(
self
)
{
self
.
renderJSON
(
articles
);
fantomas
.
run
(
function
(
)
{
this
.
renderJSON
(
links
).
exit
(
);
});
...
...
Please
register
or
sign in
to post a comment