formfill.js
8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*global casper, __utils__*/
/*jshint strict:false*/
var fs = require('fs');
function testFormValues(test) {
test.assertField('email', 'chuck@norris.com',
'can fill an input[type=text] form field');
test.assertField('password', 'chuck',
'can fill an input[type=password] form field')
test.assertField('content', 'Am watching thou',
'can fill a textarea form field');
test.assertField('topic', 'bar',
'can pick a value from a select form field');
test.assertField('check', true,
'can check a form checkbox');
test.assertEvalEquals(function() {
return __utils__.findOne('input[name="choice"][value="no"]').checked;
}, true, 'can check a form radio button 1/2');
test.assertEvalEquals(function() {
return __utils__.findOne('input[name="choice"][value="yes"]').checked;
}, false, 'can check a form radio button 2/2');
test.assertEvalEquals(function() {
return (__utils__.findOne('input[name="checklist[]"][value="1"]').checked &&
!__utils__.findOne('input[name="checklist[]"][value="2"]').checked &&
__utils__.findOne('input[name="checklist[]"][value="3"]').checked);
}, true, 'can fill a list of checkboxes');
}
function testUrl(test) {
test.assertUrlMatch(/email=chuck@norris.com/, 'input[type=email] field was submitted');
test.assertUrlMatch(/password=chuck/, 'input[type=password] field was submitted');
test.assertUrlMatch(/content=Am\+watching\+thou/, 'textarea field was submitted');
test.assertUrlMatch(/check=on/, 'input[type=checkbox] field was submitted');
test.assertUrlMatch(/choice=no/, 'input[type=radio] field was submitted');
test.assertUrlMatch(/topic=bar/, 'select field was submitted');
test.assertUrlMatch(/strange=very/, 'strangely typed input field was submitted');
}
casper.test.begin('fill() & fillNames() tests', 16, function(test) {
var fpath = fs.pathJoin(phantom.casperPath, 'README.md');
casper.start('tests/site/form.html', function() {
this.fill('form[action="result.html"]', {
email: 'chuck@norris.com',
password: 'chuck',
content: 'Am watching thou',
check: true,
choice: 'no',
topic: 'bar',
file: fpath,
'checklist[]': ['1', '3'],
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', 16, function(test) {
var fpath = fs.pathJoin(phantom.casperPath, 'README.md');
casper.start('tests/site/form.html', function() {
this.fillSelectors('form[action="result.html"]', {
"input[name='email']": 'chuck@norris.com',
"input[name='password']": 'chuck',
"textarea[name='content']": 'Am watching thou',
"input[name='check']": true,
"input[name='choice']": 'no',
"select[name='topic']": 'bar',
"input[name='file']": fpath,
"input[name='checklist[]']": ['1', '3'],
"input[name='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('fillXPath() tests', 15, function(test) {
casper.start('tests/site/form.html', function() {
this.fillXPath('form[action="result.html"]', {
'//input[@name="email"]': 'chuck@norris.com',
'//input[@name="password"]': 'chuck',
'//textarea[@name="content"]': 'Am watching thou',
'//input[@name="check"]': true,
'//input[@name="choice"]': 'no',
'//select[@name="topic"]': 'bar',
'//input[@name="checklist[]"]': ['1', '3'],
'//input[@name="strange"]': "very"
});
testFormValues(test);
// note: file inputs cannot be filled using XPath
});
casper.thenClick('input[type="submit"]', function() {
testUrl(test);
});
casper.run(function() {
test.done();
});
});
casper.test.begin('nonexistent fields', 1, function(test) {
casper.start('tests/site/form.html', function() {
test.assertRaises(this.fill, ['form[action="result.html"]', {
nonexistent: 42
}, true], 'Casper.fill() raises an exception when unable to fill a form');
}).run(function() {
test.done();
});
});
casper.test.begin('multiple forms', 2, function(test) {
casper.start('tests/site/multiple-forms.html', function() {
this.fill('form[name="f2"]', {
yo: "ok"
}, true);
}).then(function() {
test.assertUrlMatch(/\?f=f2&yo=ok$/, 'Casper.fill() handles multiple forms');
}).then(function() {
this.fill('form[name="f2"]', {
yo: "ok"
});
test.assertEquals(this.getFormValues('form[name="f2"]'), {
f: "f2",
yo: "ok"
}, 'Casper.getFormValues() retrieves filled values when multiple forms have same field names');
}).run(function() {
test.done();
});
});
casper.test.begin('field array', 1, function(test) {
// issue #267: array syntax field names
casper.start('tests/site/field-array.html', function() {
this.fill('form', {
'foo[bar]': "bar",
'foo[baz]': "baz"
}, true);
}).then(function() {
test.assertUrlMatch('?foo[bar]=bar&foo[baz]=baz',
'Casper.fill() handles array syntax field names');
}).run(function() {
test.done();
});
});
casper.test.begin('getFormValues() tests', 2, function(test) {
var fpath = fs.pathJoin(phantom.casperPath, 'README.md');
var fileValue = 'README.md';
if (phantom.casperEngine === 'phantomjs') {
fileValue = 'C:\\fakepath\\README.md'; // phantomjs/webkit sets that;
}
casper.start('tests/site/form.html', function() {
this.fill('form[action="result.html"]', {
email: 'chuck@norris.com',
password: 'chuck',
language: 'english',
content: 'Am watching thou',
check: true,
choice: 'no',
topic: 'bar',
file: fpath,
'checklist[]': ['1', '3'],
strange: "very"
});
});
casper.then(function() {
test.assertEquals(this.getFormValues('form'), {
"check": true,
"checklist[]": ["1", "3"],
"choice": "no",
"content": "Am watching thou",
"email": "chuck@norris.com",
"file": fileValue,
"password": "chuck",
"submit": "submit",
"language": "english",
"topic": "bar",
"strange": "very"
}, 'Casper.getFormValues() retrieves filled values');
});
casper.then(function() {
this.fill('form[action="result.html"]', {
email: 'chuck@norris.com',
password: 'chuck',
language: 'english',
content: 'Am watching thou',
check: true,
choice: 'yes',
topic: 'bar',
file: fpath,
'checklist[]': ['1', '3'],
strange: "very"
});
});
casper.then(function() {
test.assertEquals(this.getFormValues('form'), {
"check": true,
"checklist[]": ["1", "3"],
"choice": "yes",
"content": "Am watching thou",
"email": "chuck@norris.com",
"file": fileValue,
"password": "chuck",
"language": "english",
"submit": "submit",
"topic": "bar",
"strange": "very"
}, 'Casper.getFormValues() correctly retrieves values from radio inputs regardless of order');
});
casper.run(function() {
test.done();
});
});