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
9ca9c9e8
...
9ca9c9e80106dbadaf998abe6512c564457f4900
authored
2011-10-03 17:55:24 +0200
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
added Casper#click() method to emulate a click event on any element behind a CSS selector
1 parent
f6f0ff70
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
19 deletions
README.md
casper.js
example.js
README.md
View file @
9ca9c9e
...
...
@@ -256,6 +256,32 @@ casper.start('http://www.google.fr/', function(self) {
});
```
### Casper#click(String selector)
Emulates a click on the element from the provided selector, if possible. In case of success,
`true`
is returned.
Example:
```
javascript
casper
.
start
(
'http://google.fr/'
)
.
thenEvaluate
(
function
()
{
document
.
querySelector
(
'input[name="q"]'
).
setAttribute
(
'value'
,
'%term%'
);
document
.
querySelector
(
'form[name="f"]'
).
submit
();
},
{
term
:
'CasperJS'
})
.
then
(
function
(
self
)
{
// Click on 1st result link
if
(
self
.
click
(
'h3.r a'
))
{
console
.
log
(
'clicked ok'
)
}
})
.
run
(
function
(
self
)
{
self
.
debugPage
();
})
;
```
### Casper#capture(String targetFilepath, Object clipRect)
Proxy method for PhantomJS'
`WebPage#render`
. Adds a clipRect parameter for automatically setting page clipRect setting values and sets it back once done.
...
...
casper.js
View file @
9ca9c9e
...
...
@@ -172,6 +172,41 @@
},
/**
* Emulates a click on the element from the provided selector, if
* possible. In case of success, `true` is returned.
*
* @param String selector A DOM CSS3 compatible selector
* @return Boolean
*/
click
:
function
(
selector
)
{
this
.
log
(
"click on selector: "
+
selector
,
"debug"
);
return
this
.
evaluate
(
function
()
{
var
s
=
'%selector%'
;
try
{
var
elem
=
document
.
querySelector
(
s
);
}
catch
(
e
)
{
console
.
log
(
'invalid selector: '
+
s
);
return
false
;
}
if
(
!
elem
)
{
console
.
log
(
'selector "'
+
s
+
'" did not find any matching element'
);
return
false
;
}
var
evt
=
document
.
createEvent
(
"MouseEvents"
);
evt
.
initMouseEvent
(
"click"
,
true
,
true
,
window
,
1
,
1
,
1
,
1
,
1
,
false
,
false
,
false
,
false
,
0
,
elem
);
if
(
elem
.
dispatchEvent
(
evt
))
{
return
true
;
}
if
(
elem
.
hasAttribute
(
'href'
))
{
document
.
location
=
elem
.
getAttribute
(
'href'
);
return
true
;
}
},
{
selector
:
selector
.
replace
(
"'"
,
"\'"
)
});
},
/**
* Logs the HTML code of the current page.
*
* @return Casper
...
...
@@ -581,23 +616,6 @@
}
/**
* Checks if the provided var is a WebPage instance
*
* @param mixed what
* @return Boolean
*/
function
isWebPage
(
what
)
{
if
(
!
what
||
typeof
(
what
)
!==
"object"
)
{
return
false
;
}
if
(
phantom
.
version
.
major
<=
1
&&
phantom
.
version
.
minor
<
3
)
{
return
what
instanceof
WebPage
;
}
else
{
return
what
.
toString
().
indexOf
(
'WebPage('
)
===
0
;
}
}
/**
* Object recursive merging utility.
*
* @param object obj1 the destination object
...
...
@@ -639,4 +657,21 @@
}
return
fn
;
}
/**
* Checks if the provided var is a WebPage instance
*
* @param mixed what
* @return Boolean
*/
function
isWebPage
(
what
)
{
if
(
!
what
||
typeof
(
what
)
!==
"object"
)
{
return
false
;
}
if
(
phantom
.
version
.
major
<=
1
&&
phantom
.
version
.
minor
<
3
)
{
return
what
instanceof
WebPage
;
}
else
{
return
what
.
toString
().
indexOf
(
'WebPage('
)
===
0
;
}
}
})(
phantom
);
...
...
example.js
View file @
9ca9c9e
...
...
@@ -6,14 +6,15 @@ function q() {
}
function
getLinks
()
{
return
Array
.
prototype
.
map
.
call
(
document
.
querySelectorAll
(
'h3.r a'
),
function
(
e
)
{
var
links
=
document
.
querySelectorAll
(
'h3.r a'
);
return
[].
map
.
call
(
links
,
function
(
e
)
{
return
e
.
getAttribute
(
'href'
);
});
}
var
links
=
[];
var
casper
=
new
phantom
.
Casper
({
logLevel
:
"
info
"
,
logLevel
:
"
debug
"
,
verbose
:
true
})
.
start
(
'http://google.fr/'
)
...
...
@@ -28,6 +29,10 @@ var casper = new phantom.Casper({
})
.
then
(
function
(
self
)
{
links
=
links
.
concat
(
self
.
evaluate
(
getLinks
));
self
.
log
(
"Click on 1st result link"
).
click
(
'h3.r a'
);
})
.
then
(
function
(
self
)
{
self
.
debugPage
();
})
.
run
(
function
(
self
)
{
self
.
echo
(
JSON
.
stringify
({
...
...
Please
register
or
sign in
to post a comment