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
75755c0f
...
75755c0f90bf6abe25b872e4638f48613daf846d
authored
2012-05-13 08:10:02 +0200
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
refs #54 - added helper function to use XPath selectors
1 parent
241f53da
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
25 deletions
modules/casper.js
tests/suites/casper/clientutils.js
tests/suites/casper/xpath.js
modules/casper.js
View file @
75755c0
...
...
@@ -41,6 +41,16 @@ exports.create = function create(options) {
return
new
Casper
(
options
);
};
exports
.
selectXPath
=
function
selectXPath
(
expression
)
{
return
{
type
:
'xpath'
,
path
:
expression
,
toString
:
function
()
{
return
this
.
type
+
' selector: '
+
this
.
selector
;
}
};
};
/**
* Main Casper object.
*
...
...
@@ -479,9 +489,6 @@ Casper.prototype.fetchText = function fetchText(selector) {
*/
Casper
.
prototype
.
fill
=
function
fill
(
selector
,
vals
,
submit
)
{
submit
=
submit
===
true
?
submit
:
false
;
if
(
!
utils
.
isString
(
selector
)
||
!
selector
.
length
)
{
throw
new
CasperError
(
"Form selector must be a non-empty string"
);
}
if
(
!
utils
.
isObject
(
vals
))
{
throw
new
CasperError
(
"Form values must be provided as an object"
);
}
...
...
@@ -507,12 +514,17 @@ Casper.prototype.fill = function fill(selector, vals, submit) {
}
// File uploads
if
(
fillResults
.
files
&&
fillResults
.
files
.
length
>
0
)
{
(
function
_each
(
self
)
{
fillResults
.
files
.
forEach
(
function
_forEach
(
file
)
{
var
fileFieldSelector
=
[
selector
,
'input[name="'
+
file
.
name
+
'"]'
].
join
(
' '
);
self
.
page
.
uploadFile
(
fileFieldSelector
,
file
.
path
);
});
})(
this
);
if
(
utils
.
isObject
(
selector
)
&&
selector
.
type
===
'xpath'
)
{
this
.
echo
(
'⚠ Filling file upload fields is currently not supported using'
,
'COMMENT'
);
this
.
echo
(
' XPath selectors; Please use a CSS selector instead.'
,
'COMMENT'
);
}
else
{
(
function
_each
(
self
)
{
fillResults
.
files
.
forEach
(
function
_forEach
(
file
)
{
var
fileFieldSelector
=
[
selector
,
'input[name="'
+
file
.
name
+
'"]'
].
join
(
' '
);
self
.
page
.
uploadFile
(
fileFieldSelector
,
file
.
path
);
});
})(
this
);
}
}
// Form submission?
if
(
submit
)
{
...
...
tests/suites/casper/clientutils.js
View file @
75755c0
var
fs
=
require
(
'fs'
);
var
clientutils
=
require
(
'clientutils'
).
create
();
var
testCases
=
{
'an empty string'
:
''
,
'a word'
:
'plop'
,
...
...
@@ -20,19 +21,4 @@ for (var what in testCases) {
casper
.
test
.
assertEquals
(
clientutils
.
decode
(
encoded
),
source
,
'ClientUtils can encode and decode '
+
what
);
}
casper
.
test
.
comment
(
'XPath'
);
casper
.
start
(
'tests/site/index.html'
,
function
()
{
this
.
test
.
assertExists
({
type
:
'xpath'
,
path
:
'/html/body/ul/li[2]'
},
'XPath selector can find an element'
);
this
.
test
.
assertDoesntExist
({
type
:
'xpath'
,
path
:
'/html/body/ol/li[2]'
},
'XPath selector does not retrieve an unexistent element'
);
});
casper
.
run
(
function
()
{
this
.
test
.
done
();
});
casper
.
test
.
done
();
...
...
tests/suites/casper/xpath.js
0 → 100644
View file @
75755c0
var
x
=
require
(
'casper'
).
selectXPath
;
casper
.
test
.
comment
(
'XPath'
);
casper
.
start
(
'tests/site/index.html'
,
function
()
{
this
.
test
.
assertExists
({
type
:
'xpath'
,
path
:
'/html/body/ul/li[2]'
},
'XPath selector can find an element'
);
this
.
test
.
assertDoesntExist
({
type
:
'xpath'
,
path
:
'/html/body/ol/li[2]'
},
'XPath selector does not retrieve an unexistent element'
);
this
.
test
.
assertExists
(
x
(
'/html/body/ul/li[2]'
),
'selectXPath() shortcut can find an element as well'
);
this
.
test
.
assertEvalEquals
(
function
()
{
return
__utils__
.
findAll
({
type
:
'xpath'
,
path
:
'/html/body/ul/li'
}).
length
;
},
3
,
'Correct number of elements are found'
);
});
casper
.
thenClick
(
x
(
'/html/body/a[2]'
),
function
()
{
this
.
test
.
assertTitle
(
'CasperJS test form'
,
'Clicking XPath works as expected'
);
this
.
fill
(
x
(
'/html/body/form'
),
{
email
:
'chuck@norris.com'
});
this
.
test
.
assertEvalEquals
(
function
()
{
return
document
.
querySelector
(
'input[name="email"]'
).
value
;
},
'chuck@norris.com'
,
'Casper.fill() can fill an input[type=text] form field'
);
});
casper
.
run
(
function
()
{
this
.
test
.
done
();
});
Please
register
or
sign in
to post a comment