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
8999c3b0
...
8999c3b0b4ac62c50fdaaf35e9d45ed0f01ef29d
authored
2011-12-30 15:08:35 +0100
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
introducing the 'mouse' module
1 parent
988307f6
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
100 additions
and
9 deletions
modules/casper.js
modules/mouse.js
modules/utils.js
tests/suites/casper/mouseclick.js
modules/casper.js
View file @
8999c3b
...
...
@@ -87,6 +87,7 @@ var Casper = function(options) {
warning
:
'COMMENT'
,
error
:
'ERROR'
};
this
.
mouse
=
require
(
'mouse'
).
create
(
this
);
this
.
options
=
utils
.
mergeObjects
(
this
.
defaults
,
options
);
this
.
page
=
null
;
this
.
pendingWait
=
false
;
...
...
@@ -521,10 +522,10 @@ Casper.prototype = {
throw
new
Error
(
"No element matching selector found: "
+
selector
);
}
var
clipRect
=
this
.
evaluate
(
function
(
selector
)
{
return
__utils__
.
getElementBounds
();
return
__utils__
.
getElementBounds
(
selector
);
},
{
selector
:
selector
});
if
(
!
utils
.
isClipRect
(
clipRect
))
{
th
is
.
log
(
'Could not fetch boundaries for element matching selector: '
+
selector
,
"error"
);
th
row
new
Error
(
'Could not fetch boundaries for element matching selector: '
+
selector
);
}
return
clipRect
;
},
...
...
@@ -611,12 +612,7 @@ Casper.prototype = {
* @return Casper
*/
mouseClick
:
function
(
selector
)
{
var
bounds
=
this
.
getElementBounds
(
selector
);
if
(
utils
.
isClipRect
(
bounds
))
{
var
x
=
bounds
.
left
+
Math
.
floor
(
bounds
.
width
/
2
);
var
y
=
bounds
.
top
+
Math
.
floor
(
bounds
.
height
/
2
);
this
.
page
.
sendEvent
(
'click'
,
x
,
y
);
}
this
.
mouse
.
click
(
selector
);
return
this
;
},
...
...
modules/mouse.js
0 → 100644
View file @
8999c3b
var
utils
=
require
(
'utils'
);
exports
.
create
=
function
(
casper
)
{
return
new
Mouse
(
casper
);
};
var
Mouse
=
function
(
casper
)
{
if
(
!
utils
.
isCasperObject
(
casper
))
{
throw
new
Error
(
'Mouse() needs a Casper instance'
);
}
var
supportedEvents
=
[
'mouseup'
,
'mousedown'
,
'click'
,
'mousemove'
];
var
computeCenter
=
function
(
selector
)
{
var
bounds
=
casper
.
getElementBounds
(
selector
);
if
(
utils
.
isClipRect
(
bounds
))
{
var
x
=
Math
.
round
(
bounds
.
left
+
bounds
.
width
/
2
);
var
y
=
Math
.
round
(
bounds
.
top
+
bounds
.
height
/
2
);
return
[
x
,
y
];
}
};
var
processEvent
=
function
(
type
,
args
)
{
if
(
!
utils
.
isString
(
type
)
||
supportedEvents
.
indexOf
(
type
)
===
-
1
)
{
throw
new
Error
(
'Unsupported mouse event type: '
+
type
);
}
args
=
Array
.
prototype
.
slice
.
call
(
args
);
// cast Arguments -> Array
switch
(
args
.
length
)
{
case
0
:
throw
new
Error
(
'Too few arguments'
);
case
1
:
// selector
var
selector
=
args
[
0
];
if
(
!
utils
.
isString
(
selector
))
{
throw
new
Error
(
'No valid CSS selector passed: '
+
selector
);
}
casper
.
page
.
sendEvent
.
apply
(
casper
.
page
,
[
type
].
concat
(
computeCenter
(
selector
)))
break
;
case
2
:
// coordinates
if
(
!
utils
.
isNumber
(
args
[
1
])
||
!
utils
.
isNumber
(
args
[
2
]))
{
throw
new
Error
(
'No valid coordinates passed'
);
}
casper
.
page
.
sendEvent
(
type
,
args
[
0
],
args
[
1
])
break
;
default
:
throw
new
Error
(
'Too many arguments'
);
}
};
this
.
click
=
function
()
{
processEvent
(
'click'
,
arguments
);
},
this
.
down
=
function
()
{
processEvent
(
'mousedown'
,
arguments
);
},
this
.
move
=
function
()
{
processEvent
(
'mousemove'
,
arguments
);
},
this
.
up
=
function
()
{
processEvent
(
'mouseup'
,
arguments
);
}
};
exports
.
Mouse
=
Mouse
;
modules/utils.js
View file @
8999c3b
...
...
@@ -137,7 +137,7 @@ exports.isObject = isObject;
function
isString
(
value
)
{
return
isType
(
value
,
"string"
);
}
exports
.
is
Function
=
isFunction
;
exports
.
is
String
=
isString
;
/**
* Shorthands for checking if a value is of the given type. Can check for
...
...
tests/suites/casper/mouseclick.js
0 → 100644
View file @
8999c3b
(
function
(
t
)
{
casper
.
start
(
'tests/site/index.html'
,
function
(
self
)
{
self
.
mouseClick
(
'a[href="test.html"]'
);
});
casper
.
then
(
function
(
self
)
{
t
.
comment
(
'Casper.mouseClick()'
);
t
.
assertTitle
(
'CasperJS test target'
,
'Casper.mouseClick() can click on a link'
);
});
// onclick variants tests
casper
.
thenOpen
(
'tests/site/click.html'
,
function
(
self
)
{
t
.
comment
(
'Casper.mouseClick()'
);
self
.
mouseClick
(
'#test1'
);
self
.
mouseClick
(
'#test2'
);
self
.
mouseClick
(
'#test3'
);
self
.
mouseClick
(
'#test4'
);
var
results
=
self
.
getGlobal
(
'results'
);
self
.
test
.
assert
(
results
.
test1
,
'Casper.mouseClick() has clicked an `href="javascript:` link'
);
self
.
test
.
assert
(
results
.
test2
,
'Casper.mouseClick() has clicked an `href="#"` link'
);
self
.
test
.
assert
(
results
.
test3
,
'Casper.mouseClick() has clicked an `onclick=".*; return false"` link'
);
self
.
test
.
assert
(
results
.
test4
,
'Casper.mouseClick() has clicked an unobstrusive js handled link'
);
});
casper
.
run
(
function
(
self
)
{
t
.
done
();
});
})(
casper
.
test
);
\ No newline at end of file
Please
register
or
sign in
to post a comment