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
66093a30
...
66093a30a44f2ccefff3e36ea639abb9b4232c62
authored
2012-03-30 16:26:20 -0700
by
nrabinowitz
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Adding support for the full range of mouse events
1 parent
f2d7dd2e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
15 deletions
modules/casper.js
modules/clientutils.js
modules/mouse.js
modules/casper.js
View file @
66093a3
...
...
@@ -252,22 +252,36 @@ Casper.prototype.clear = function clear() {
* @return Boolean
*/
Casper
.
prototype
.
click
=
function
click
(
selector
)
{
this
.
log
(
"Click on selector: "
+
selector
,
"debug"
);
if
(
arguments
.
length
>
1
)
{
this
.
emit
(
"deprecated"
,
"The click() method does not process the fallbackToHref argument since 0.6"
);
}
return
this
.
mouseEvent
(
'click'
,
selector
);
};
/**
* Emulates an event on the element from the provided selector using the mouse
* pointer, if possible.
*
* In case of success, `true` is returned, `false` otherwise.
*
* @param String type Type of event to emulate
* @param String selector A DOM CSS3 compatible selector
* @return Boolean
*/
Casper
.
prototype
.
mouseEvent
=
function
mouseEvent
(
type
,
selector
)
{
this
.
log
(
"Mouse event '"
+
type
+
"' on selector: "
+
selector
,
"debug"
);
if
(
!
this
.
exists
(
selector
))
{
throw
new
CasperError
(
"Cannot
click on u
nexistent selector: "
+
selector
);
throw
new
CasperError
(
"Cannot
dispath an event on no
nexistent selector: "
+
selector
);
}
var
clicked
=
this
.
evaluate
(
function
(
selector
)
{
return
__utils__
.
click
(
selector
);
},
{
selector
:
selector
});
if
(
!
clicked
)
{
var
eventSuccess
=
this
.
evaluate
(
function
(
type
,
selector
)
{
return
__utils__
.
mouseEvent
(
type
,
selector
);
},
{
type
:
type
,
selector
:
selector
});
if
(
!
eventSuccess
)
{
// fallback onto native QtWebKit mouse events
try
{
this
.
mouse
.
click
(
selector
);
this
.
mouse
.
processEvent
(
type
,
selector
);
}
catch
(
e
)
{
this
.
log
(
f
(
"Error while trying to
click on selector %s: %s"
,
selector
,
e
),
"error"
);
this
.
log
(
f
(
"Error while trying to
emulate event %s on selector %s: %s"
,
type
,
selector
,
e
),
"error"
);
return
false
;
}
}
...
...
modules/clientutils.js
View file @
66093a3
...
...
@@ -49,25 +49,36 @@
);
/**
*
Clicks on
the DOM element behind the provided selector.
*
Dispatches a mouse event to
the DOM element behind the provided selector.
*
* @param String type Type of event to dispatch
* @param String selector A CSS3 selector to the element to click
* @return Boolean
*/
this
.
click
=
function
(
selector
)
{
this
.
mouseEvent
=
function
(
type
,
selector
)
{
var
elem
=
this
.
findOne
(
selector
);
if
(
!
elem
)
{
this
.
log
(
"
click
(): Couldn't find any element matching '"
+
selector
+
"' selector"
,
"error"
);
this
.
log
(
"
mouseEvent
(): Couldn't find any element matching '"
+
selector
+
"' selector"
,
"error"
);
return
false
;
}
var
evt
=
document
.
createEvent
(
"MouseEvents"
);
evt
.
initMouseEvent
(
"click"
,
true
,
true
,
window
,
1
,
1
,
1
,
1
,
1
,
false
,
false
,
false
,
false
,
0
,
elem
);
evt
.
initMouseEvent
(
type
,
true
,
true
,
window
,
1
,
1
,
1
,
1
,
1
,
false
,
false
,
false
,
false
,
0
,
elem
);
// dispatchEvent return value is false if at least one of the event
// handlers which handled this event called preventDefault
return
elem
.
dispatchEvent
(
evt
);
};
/**
* Clicks on the DOM element behind the provided selector.
*
* @param String selector A CSS3 selector to the element to click
* @return Boolean
*/
this
.
click
=
function
(
selector
)
{
return
this
.
mouseEvent
(
'click'
,
selector
);
};
/**
* Decodes a base64 encoded string. Succeeds where window.atob() fails.
*
* @param String str The base64 encoded contents
...
...
modules/mouse.js
View file @
66093a3
...
...
@@ -78,6 +78,8 @@ var Mouse = function(casper) {
throw
new
CasperError
(
'Mouse.processEvent(): Too many arguments'
);
}
}
this
.
processEvent
=
processEvent
;
this
.
click
=
function
click
()
{
processEvent
(
'click'
,
arguments
);
...
...
Please
register
or
sign in
to post a comment