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
e3d59675
...
e3d59675d13f9bde63f2796d7acec72aa70921b7
authored
2011-12-29 16:20:14 +0100
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
added Casper.mouseClick() method for native mouse click emulation using native events
1 parent
7a2f3420
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
82 additions
and
15 deletions
modules/casper.js
modules/clientutils.js
modules/utils.js
tests/suites/utils.js
modules/casper.js
View file @
e3d5967
...
...
@@ -178,17 +178,7 @@ Casper.prototype = {
*/
captureSelector
:
function
(
targetFile
,
selector
)
{
return
this
.
capture
(
targetFile
,
this
.
evaluate
(
function
(
selector
)
{
try
{
var
clipRect
=
document
.
querySelector
(
selector
).
getBoundingClientRect
();
return
{
top
:
clipRect
.
top
,
left
:
clipRect
.
left
,
width
:
clipRect
.
width
,
height
:
clipRect
.
height
};
}
catch
(
e
)
{
__utils__
.
log
(
"Unable to fetch bounds for element "
+
selector
,
"warning"
);
}
return
__utils__
.
getElementBounds
();
},
{
selector
:
selector
}));
},
...
...
@@ -597,6 +587,25 @@ Casper.prototype = {
},
/**
* Emulates a click on an HTML element matching a given CSS3 selector,
* using the mouse pointer.
*
* @param String selector A DOM CSS3 compatible selector
* @return Casper
*/
mouseClick
:
function
(
selector
)
{
var
bounds
=
this
.
evaluate
(
function
(
selector
)
{
return
__utils__
.
getElementBounds
(
selector
);
},
{
selector
:
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
);
}
return
this
;
},
/**
* Opens a page. Takes only one argument, the url to open (using the
* callback argument would defeat the whole purpose of Casper
* actually).
...
...
modules/clientutils.js
View file @
e3d5967
...
...
@@ -351,6 +351,27 @@
};
/**
* Retrieves bounding rect coordinates of the HTML element matching the
* provided CSS3 selector
*
* @param String selector
* @return Object or null
*/
this
.
getElementBounds
=
function
(
selector
)
{
try
{
var
clipRect
=
document
.
querySelector
(
selector
).
getBoundingClientRect
();
return
{
top
:
clipRect
.
top
,
left
:
clipRect
.
left
,
width
:
clipRect
.
width
,
height
:
clipRect
.
height
};
}
catch
(
e
)
{
this
.
log
(
"Unable to fetch bounds for element "
+
selector
,
"warning"
);
}
};
/**
* Logs a message. Will format the message a way CasperJS will be able
* to log phantomjs side.
*
...
...
modules/utils.js
View file @
e3d5967
...
...
@@ -85,6 +85,11 @@ function fillBlanks(text, pad) {
}
exports
.
fillBlanks
=
fillBlanks
;
function
isArray
(
value
)
{
return
isType
(
value
,
"array"
);
}
exports
.
isArray
=
isArray
;
/**
* Checks if passed argument is an instance of Capser object.
*
...
...
@@ -96,6 +101,22 @@ function isCasperObject(value) {
}
exports
.
isCasperObject
=
isCasperObject
;
function
isClipRect
(
value
)
{
return
isType
(
value
,
"cliprect"
)
||
(
isType
(
value
,
"object"
)
&&
isType
(
value
.
top
,
"number"
)
&&
isType
(
value
.
left
,
"number"
)
&&
isType
(
value
.
width
,
"number"
)
&&
isType
(
value
.
height
,
"number"
)
);
}
exports
.
isClipRect
=
isClipRect
;
function
isFunction
(
value
)
{
return
isType
(
value
,
"function"
);
}
exports
.
isFunction
=
isFunction
;
/**
* Checks if a file is apparently javascript compatible (.js or .coffee).
*
...
...
@@ -108,6 +129,11 @@ function isJsFile(file) {
}
exports
.
isJsFile
=
isJsFile
;
function
isObject
(
value
)
{
return
isType
(
value
,
"object"
);
}
exports
.
isObject
=
isObject
;
/**
* Shorthands for checking if a value is of the given type. Can check for
* arrays.
...
...
tests/suites/utils.js
View file @
e3d5967
...
...
@@ -2,7 +2,6 @@
var
utils
=
require
(
'utils'
);
t
.
comment
(
'fileExt()'
);
(
function
()
{
var
testCases
=
{
'foo.ext'
:
'ext'
,
...
...
@@ -19,7 +18,6 @@
})();
t
.
comment
(
'fillBlanks()'
);
(
function
()
{
testCases
=
{
'foo'
:
'foo '
,
...
...
@@ -32,8 +30,22 @@
}
})();
t
.
comment
(
'isJsFile()'
);
t
.
comment
(
'isClipRect()'
);
(
function
()
{
testCases
=
[
[{},
false
],
[{
top
:
2
},
false
],
[{
top
:
2
,
left
:
2
,
width
:
2
,
height
:
2
},
true
],
[{
top
:
2
,
left
:
2
,
height
:
2
,
width
:
2
},
true
],
[{
top
:
2
,
left
:
2
,
width
:
2
,
height
:
new
Date
()},
false
]
];
testCases
.
forEach
(
function
(
testCase
)
{
t
.
assertEquals
(
utils
.
isClipRect
(
testCase
[
0
]),
testCase
[
1
],
'isClipRect() checks for a ClipRect'
);
});
})();
t
.
comment
(
'isJsFile()'
);
(
function
()
{
testCases
=
{
''
:
false
,
...
...
@@ -49,7 +61,6 @@
})();
t
.
comment
(
'mergeObjects()'
);
(
function
()
{
testCases
=
[
{
...
...
Please
register
or
sign in
to post a comment