forked from puppeteer/puppeteer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.spec.js
223 lines (216 loc) · 8.98 KB
/
input.spec.js
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
/**
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const path = require('path');
const FILE_TO_UPLOAD = path.join(__dirname, '/assets/file-to-upload.txt');
module.exports.addTests = function({testRunner, expect, puppeteer}) {
const {describe, xdescribe, fdescribe, describe_fails_ffox} = testRunner;
const {it, fit, xit, it_fails_ffox} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
describe('input', function() {
it('should upload the file', async({page, server}) => {
await page.goto(server.PREFIX + '/input/fileupload.html');
const filePath = path.relative(process.cwd(), FILE_TO_UPLOAD);
const input = await page.$('input');
await input.uploadFile(filePath);
expect(await page.evaluate(e => e.files[0].name, input)).toBe('file-to-upload.txt');
expect(await page.evaluate(e => {
const reader = new FileReader();
const promise = new Promise(fulfill => reader.onload = fulfill);
reader.readAsText(e.files[0]);
return promise.then(() => reader.result);
}, input)).toBe('contents of the file');
});
});
describe_fails_ffox('Page.waitForFileChooser', function() {
it('should work when file input is attached to DOM', async({page, server}) => {
await page.setContent(`<input type=file>`);
const [chooser] = await Promise.all([
page.waitForFileChooser(),
page.click('input'),
]);
expect(chooser).toBeTruthy();
});
it('should work when file input is not attached to DOM', async({page, server}) => {
const [chooser] = await Promise.all([
page.waitForFileChooser(),
page.evaluate(() => {
const el = document.createElement('input');
el.type = 'file';
el.click();
}),
]);
expect(chooser).toBeTruthy();
});
it('should respect timeout', async({page, server}) => {
let error = null;
await page.waitForFileChooser({timeout: 1}).catch(e => error = e);
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
});
it('should respect default timeout when there is no custom timeout', async({page, server}) => {
page.setDefaultTimeout(1);
let error = null;
await page.waitForFileChooser().catch(e => error = e);
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
});
it('should prioritize exact timeout over default timeout', async({page, server}) => {
page.setDefaultTimeout(0);
let error = null;
await page.waitForFileChooser({timeout: 1}).catch(e => error = e);
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
});
it('should work with no timeout', async({page, server}) => {
const [chooser] = await Promise.all([
page.waitForFileChooser({timeout: 0}),
page.evaluate(() => setTimeout(() => {
const el = document.createElement('input');
el.type = 'file';
el.click();
}, 50))
]);
expect(chooser).toBeTruthy();
});
it('should return the same file chooser when there are many watchdogs simultaneously', async({page, server}) => {
await page.setContent(`<input type=file>`);
const [fileChooser1, fileChooser2] = await Promise.all([
page.waitForFileChooser(),
page.waitForFileChooser(),
page.$eval('input', input => input.click()),
]);
expect(fileChooser1 === fileChooser2).toBe(true);
});
});
describe_fails_ffox('FileChooser.accept', function() {
it('should accept single file', async({page, server}) => {
await page.setContent(`<input type=file oninput='javascript:console.timeStamp()'>`);
const [chooser] = await Promise.all([
page.waitForFileChooser(),
page.click('input'),
]);
await Promise.all([
chooser.accept([FILE_TO_UPLOAD]),
new Promise(x => page.once('metrics', x)),
]);
expect(await page.$eval('input', input => input.files.length)).toBe(1);
expect(await page.$eval('input', input => input.files[0].name)).toBe('file-to-upload.txt');
});
it('should be able to read selected file', async({page, server}) => {
await page.setContent(`<input type=file>`);
page.waitForFileChooser().then(chooser => chooser.accept([FILE_TO_UPLOAD]));
expect(await page.$eval('input', async picker => {
picker.click();
await new Promise(x => picker.oninput = x);
const reader = new FileReader();
const promise = new Promise(fulfill => reader.onload = fulfill);
reader.readAsText(picker.files[0]);
return promise.then(() => reader.result);
})).toBe('contents of the file');
});
it('should be able to reset selected files with empty file list', async({page, server}) => {
await page.setContent(`<input type=file>`);
page.waitForFileChooser().then(chooser => chooser.accept([FILE_TO_UPLOAD]));
expect(await page.$eval('input', async picker => {
picker.click();
await new Promise(x => picker.oninput = x);
return picker.files.length;
})).toBe(1);
page.waitForFileChooser().then(chooser => chooser.accept([]));
expect(await page.$eval('input', async picker => {
picker.click();
await new Promise(x => picker.oninput = x);
return picker.files.length;
})).toBe(0);
});
it('should not accept multiple files for single-file input', async({page, server}) => {
await page.setContent(`<input type=file>`);
const [chooser] = await Promise.all([
page.waitForFileChooser(),
page.click('input'),
]);
let error = null;
await chooser.accept([
path.relative(process.cwd(), __dirname + '/assets/file-to-upload.txt'),
path.relative(process.cwd(), __dirname + '/assets/pptr.png'),
]).catch(e => error = e);
expect(error).not.toBe(null);
});
it('should fail when accepting file chooser twice', async({page, server}) => {
await page.setContent(`<input type=file>`);
const [fileChooser] = await Promise.all([
page.waitForFileChooser(),
page.$eval('input', input => input.click()),
]);
await fileChooser.accept([]);
let error = null;
await fileChooser.accept([]).catch(e => error = e);
expect(error.message).toBe('Cannot accept FileChooser which is already handled!');
});
});
describe_fails_ffox('FileChooser.cancel', function() {
it('should cancel dialog', async({page, server}) => {
// Consider file chooser canceled if we can summon another one.
// There's no reliable way in WebPlatform to see that FileChooser was
// canceled.
await page.setContent(`<input type=file>`);
const [fileChooser1] = await Promise.all([
page.waitForFileChooser(),
page.$eval('input', input => input.click()),
]);
await fileChooser1.cancel();
// If this resolves, than we successfully canceled file chooser.
await Promise.all([
page.waitForFileChooser(),
page.$eval('input', input => input.click()),
]);
});
it('should fail when canceling file chooser twice', async({page, server}) => {
await page.setContent(`<input type=file>`);
const [fileChooser] = await Promise.all([
page.waitForFileChooser(),
page.$eval('input', input => input.click()),
]);
await fileChooser.cancel();
let error = null;
await fileChooser.cancel().catch(e => error = e);
expect(error.message).toBe('Cannot cancel FileChooser which is already handled!');
});
});
describe_fails_ffox('FileChooser.isMultiple', () => {
it('should work for single file pick', async({page, server}) => {
await page.setContent(`<input type=file>`);
const [chooser] = await Promise.all([
page.waitForFileChooser(),
page.click('input'),
]);
expect(chooser.isMultiple()).toBe(false);
});
it('should work for "multiple"', async({page, server}) => {
await page.setContent(`<input multiple type=file>`);
const [chooser] = await Promise.all([
page.waitForFileChooser(),
page.click('input'),
]);
expect(chooser.isMultiple()).toBe(true);
});
it('should work for "webkitdirectory"', async({page, server}) => {
await page.setContent(`<input multiple webkitdirectory type=file>`);
const [chooser] = await Promise.all([
page.waitForFileChooser(),
page.click('input'),
]);
expect(chooser.isMultiple()).toBe(true);
});
});
};