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
f5c8d437
...
f5c8d437317d07738a78007c95113cc77984f4ee
authored
2014-06-12 11:43:32 -0700
by
Micah Geisel
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Introduce fillLabels method to fill out a form based on label text.
1 parent
5b3bbd5c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
91 additions
and
17 deletions
modules/casper.js
modules/clientutils.js
tests/site/form.html
tests/suites/casper/formfill.js
modules/casper.js
View file @
f5c8d43
...
...
@@ -821,7 +821,7 @@ Casper.prototype.fillForm = function fillForm(selector, vals, options) {
var
fileFieldSelector
;
if
(
file
.
type
===
"names"
)
{
fileFieldSelector
=
[
selector
,
'input[name="'
+
file
.
selector
+
'"]'
].
join
(
' '
);
}
else
if
(
file
.
type
===
"css"
)
{
}
else
if
(
file
.
type
===
"css"
||
file
.
type
===
"labels"
)
{
fileFieldSelector
=
[
selector
,
file
.
selector
].
join
(
' '
);
}
this
.
page
.
uploadFile
(
fileFieldSelector
,
file
.
path
);
...
...
@@ -867,6 +867,21 @@ Casper.prototype.fillNames = function fillNames(formSelector, vals, submit) {
};
/**
* Fills a form with provided field values using associated label text.
*
* @param String formSelector A DOM CSS3/XPath selector to the target form to fill
* @param Object vals Field values
* @param Boolean submit Submit the form?
*/
Casper
.
prototype
.
fillLabels
=
function
fillLabels
(
formSelector
,
vals
,
submit
)
{
"use strict"
;
return
this
.
fillForm
(
formSelector
,
vals
,
{
submit
:
submit
,
selectorType
:
'labels'
});
};
/**
* Fills a form with provided field values using CSS3 selectors.
*
* @param String formSelector A DOM CSS3/XPath selector to the target form to fill
...
...
modules/clientutils.js
View file @
f5c8d43
...
...
@@ -273,6 +273,12 @@
css
:
function
(
inputSelector
,
formSelector
)
{
return
this
.
findAll
(
inputSelector
,
form
);
},
labels
:
function
(
labelText
,
formSelector
,
value
)
{
var
label
=
this
.
findOne
({
type
:
"xpath"
,
path
:
'//label[text()="'
+
labelText
+
'"]'
},
form
);
if
(
label
&&
label
.
htmlFor
)
{
return
this
.
findAll
(
'#'
+
label
.
htmlFor
,
form
);
}
},
names
:
function
(
elementName
,
formSelector
)
{
return
this
.
findAll
(
'[name="'
+
elementName
+
'"]'
,
form
);
},
...
...
@@ -295,9 +301,15 @@
out
.
fields
[
fieldSelector
]
=
this
.
setField
(
field
,
value
);
}
catch
(
err
)
{
if
(
err
.
name
===
"FileUploadError"
)
{
var
selector
;
if
(
findType
===
"labels"
)
{
selector
=
'#'
+
field
[
0
].
id
;
}
else
{
selector
=
fieldSelector
;
}
out
.
files
.
push
({
type
:
findType
,
selector
:
fieldS
elector
,
selector
:
s
elector
,
path
:
err
.
path
});
}
else
if
(
err
.
name
===
"FieldNotFound"
)
{
...
...
@@ -851,9 +863,13 @@
};
case
"radio"
:
if
(
fields
)
{
Array
.
prototype
.
forEach
.
call
(
fields
,
function
_forEach
(
e
)
{
e
.
checked
=
(
e
.
value
===
value
);
});
if
(
fields
.
length
>
1
)
{
Array
.
prototype
.
forEach
.
call
(
fields
,
function
_forEach
(
e
)
{
e
.
checked
=
(
e
.
value
===
value
);
});
}
else
{
field
.
checked
=
value
?
true
:
false
;
}
}
else
{
out
=
'Provided radio elements are empty'
;
}
...
...
tests/site/form.html
View file @
f5c8d43
...
...
@@ -6,27 +6,40 @@
</head>
<body>
<form
action=
"result.html"
enctype=
"multipart/form-data"
>
<label
for=
"email"
>
Email
</label>
<input
type=
"email"
name=
"email"
placeholder=
"email"
id=
"email"
>
<input
type=
"password"
name=
"password"
placeholder=
"password"
>
<label
for=
"password"
>
Password
</label>
<input
type=
"password"
name=
"password"
placeholder=
"password"
id=
"password"
>
<input
type=
"text"
name=
"language"
placeholder=
"language"
>
<input
type=
"whatever"
name=
"strange"
>
<textarea
name=
"content"
></textarea>
<select
name=
"topic"
>
<label
for=
"strange"
>
Strange
</label>
<input
type=
"whatever"
name=
"strange"
id=
"strange"
>
<label
for=
"content"
>
Content
</label>
<textarea
name=
"content"
id=
"content"
></textarea>
<label
for=
"topic"
>
Topic
</label>
<select
name=
"topic"
id=
"topic"
>
<option>
foo
</option>
<option
value=
"bar"
>
baz
</option>
</select>
<select
multiple
name=
"multitopic"
>
<label
for=
"multitopic"
>
Multitopic
</label>
<select
multiple
name=
"multitopic"
id=
"multitopic"
>
<option>
foo
</option>
<option
value=
"bar"
>
baz
</option>
<option
value=
"car"
>
caz
</option>
</select>
<input
type=
"checkbox"
name=
"check"
>
<input
type=
"radio"
name=
"choice"
value=
"yes"
>
<input
type=
"radio"
name=
"choice"
value=
"no"
>
<input
type=
"file"
name=
"file"
>
<input
type=
"checkbox"
name=
"checklist[]"
value=
"1"
>
<input
type=
"checkbox"
name=
"checklist[]"
value=
"2"
>
<input
type=
"checkbox"
name=
"checklist[]"
value=
"3"
>
<label
for=
"check"
>
Check
</label>
<input
type=
"checkbox"
name=
"check"
id=
"check"
>
<label
for=
"choice_yes"
>
Yes
</label>
<input
type=
"radio"
name=
"choice"
value=
"yes"
id=
"choice_yes"
>
<label
for=
"choice_no"
>
No
</label>
<input
type=
"radio"
name=
"choice"
value=
"no"
id=
"choice_no"
>
<label
for=
"file"
>
File
</label>
<input
type=
"file"
name=
"file"
id=
"file"
>
<label
for=
"checklist_1"
>
1
</label>
<input
type=
"checkbox"
name=
"checklist[]"
value=
"1"
id=
"checklist_1"
>
<label
for=
"checklist_2"
>
2
</label>
<input
type=
"checkbox"
name=
"checklist[]"
value=
"2"
id=
"checklist_2"
>
<label
for=
"checklist_3"
>
3
</label>
<input
type=
"checkbox"
name=
"checklist[]"
value=
"3"
id=
"checklist_3"
>
<input
type=
"submit"
name=
"submit"
value=
"submit"
>
</form>
<form
id=
"no-type-test-form"
action=
"#"
enctype=
"multipart/form-data"
>
...
...
tests/suites/casper/formfill.js
View file @
f5c8d43
...
...
@@ -67,6 +67,36 @@ casper.test.begin('fill() & fillNames() tests', 17, function(test) {
});
});
casper
.
test
.
begin
(
'fillLabels() tests'
,
17
,
function
(
test
)
{
var
fpath
=
fs
.
pathJoin
(
phantom
.
casperPath
,
'README.md'
);
casper
.
start
(
'tests/site/form.html'
,
function
()
{
this
.
fillLabels
(
'form[action="result.html"]'
,
{
Email
:
'chuck@norris.com'
,
Password
:
'chuck'
,
Content
:
'Am watching thou'
,
Check
:
true
,
No
:
true
,
Topic
:
'bar'
,
Multitopic
:
[
'bar'
,
'car'
],
File
:
fpath
,
"1"
:
true
,
"3"
:
true
,
Strange
:
"very"
});
testFormValues
(
test
);
test
.
assertEvalEquals
(
function
()
{
return
__utils__
.
findOne
(
'input[name="file"]'
).
files
.
length
===
1
;
},
true
,
'can select a file to upload'
);
});
casper
.
thenClick
(
'input[type="submit"]'
,
function
()
{
testUrl
(
test
);
});
casper
.
run
(
function
()
{
test
.
done
();
});
});
casper
.
test
.
begin
(
'fillSelectors() tests'
,
17
,
function
(
test
)
{
var
fpath
=
fs
.
pathJoin
(
phantom
.
casperPath
,
'README.md'
);
...
...
Please
register
or
sign in
to post a comment