2018-03-20 03:00:12 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
import path from 'path';
|
|
|
|
import expect from 'expect';
|
|
|
|
import {
|
2020-05-07 10:54:55 +00:00
|
|
|
getTestState,
|
|
|
|
setupTestBrowserHooks,
|
|
|
|
setupTestPageAndContextHooks,
|
2020-06-23 05:18:46 +00:00
|
|
|
describeFailsFirefox,
|
2020-07-13 09:22:26 +00:00
|
|
|
} from './mocha-utils'; // eslint-disable-line import/extensions
|
2018-03-20 03:00:12 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
const FILE_TO_UPLOAD = path.join(__dirname, '/assets/file-to-upload.txt');
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('input tests', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('input', function () {
|
|
|
|
it('should upload the file', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/fileupload.html');
|
2019-07-23 04:30:49 +00:00
|
|
|
const filePath = path.relative(process.cwd(), FILE_TO_UPLOAD);
|
2018-03-20 03:00:12 +00:00
|
|
|
const input = await page.$('input');
|
2020-07-10 10:52:13 +00:00
|
|
|
await page.evaluate((e: HTMLElement) => {
|
2020-06-23 05:18:46 +00:00
|
|
|
globalThis._inputEvents = [];
|
|
|
|
e.addEventListener('change', (ev) =>
|
|
|
|
globalThis._inputEvents.push(ev.type)
|
|
|
|
);
|
|
|
|
e.addEventListener('input', (ev) =>
|
|
|
|
globalThis._inputEvents.push(ev.type)
|
|
|
|
);
|
2020-03-12 16:26:03 +00:00
|
|
|
}, input);
|
2018-03-20 03:00:12 +00:00
|
|
|
await input.uploadFile(filePath);
|
2020-07-10 10:52:13 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate((e: HTMLInputElement) => e.files[0].name, input)
|
|
|
|
).toBe('file-to-upload.txt');
|
|
|
|
expect(
|
|
|
|
await page.evaluate((e: HTMLInputElement) => e.files[0].type, input)
|
|
|
|
).toBe('text/plain');
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis._inputEvents)).toEqual([
|
2020-05-07 10:54:55 +00:00
|
|
|
'input',
|
|
|
|
'change',
|
|
|
|
]);
|
|
|
|
expect(
|
2020-07-10 10:52:13 +00:00
|
|
|
await page.evaluate((e: HTMLInputElement) => {
|
2020-05-07 10:54:55 +00:00
|
|
|
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');
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
|
|
|
});
|
2019-07-23 04:30:49 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Page.waitForFileChooser', function () {
|
|
|
|
it('should work when file input is attached to DOM', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
await page.setContent(`<input type=file>`);
|
|
|
|
const [chooser] = await Promise.all([
|
|
|
|
page.waitForFileChooser(),
|
|
|
|
page.click('input'),
|
|
|
|
]);
|
|
|
|
expect(chooser).toBeTruthy();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work when file input is not attached to DOM', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
const [chooser] = await Promise.all([
|
|
|
|
page.waitForFileChooser(),
|
|
|
|
page.evaluate(() => {
|
|
|
|
const el = document.createElement('input');
|
|
|
|
el.type = 'file';
|
|
|
|
el.click();
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
expect(chooser).toBeTruthy();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should respect timeout', async () => {
|
|
|
|
const { page, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.waitForFileChooser({ timeout: 1 })
|
|
|
|
.catch((error_) => (error = error_));
|
2019-07-23 04:30:49 +00:00
|
|
|
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should respect default timeout when there is no custom timeout', async () => {
|
|
|
|
const { page, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
page.setDefaultTimeout(1);
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.waitForFileChooser().catch((error_) => (error = error_));
|
2019-07-23 04:30:49 +00:00
|
|
|
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should prioritize exact timeout over default timeout', async () => {
|
|
|
|
const { page, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
page.setDefaultTimeout(0);
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.waitForFileChooser({ timeout: 1 })
|
|
|
|
.catch((error_) => (error = error_));
|
2019-07-23 04:30:49 +00:00
|
|
|
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with no timeout', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
const [chooser] = await Promise.all([
|
2020-05-07 10:54:55 +00:00
|
|
|
page.waitForFileChooser({ timeout: 0 }),
|
|
|
|
page.evaluate(() =>
|
|
|
|
setTimeout(() => {
|
|
|
|
const el = document.createElement('input');
|
|
|
|
el.type = 'file';
|
|
|
|
el.click();
|
|
|
|
}, 50)
|
|
|
|
),
|
2019-07-23 04:30:49 +00:00
|
|
|
]);
|
|
|
|
expect(chooser).toBeTruthy();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return the same file chooser when there are many watchdogs simultaneously', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
await page.setContent(`<input type=file>`);
|
|
|
|
const [fileChooser1, fileChooser2] = await Promise.all([
|
|
|
|
page.waitForFileChooser(),
|
|
|
|
page.waitForFileChooser(),
|
2020-07-02 09:09:34 +00:00
|
|
|
page.$eval('input', (input: HTMLInputElement) => input.click()),
|
2019-07-23 04:30:49 +00:00
|
|
|
]);
|
|
|
|
expect(fileChooser1 === fileChooser2).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('FileChooser.accept', function () {
|
|
|
|
it('should accept single file', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setContent(
|
|
|
|
`<input type=file oninput='javascript:console.timeStamp()'>`
|
|
|
|
);
|
2019-07-23 04:30:49 +00:00
|
|
|
const [chooser] = await Promise.all([
|
|
|
|
page.waitForFileChooser(),
|
|
|
|
page.click('input'),
|
|
|
|
]);
|
|
|
|
await Promise.all([
|
|
|
|
chooser.accept([FILE_TO_UPLOAD]),
|
2020-05-07 10:54:55 +00:00
|
|
|
new Promise((x) => page.once('metrics', x)),
|
2019-07-23 04:30:49 +00:00
|
|
|
]);
|
2020-07-02 09:09:34 +00:00
|
|
|
expect(
|
|
|
|
await page.$eval(
|
|
|
|
'input',
|
|
|
|
(input: HTMLInputElement) => input.files.length
|
|
|
|
)
|
|
|
|
).toBe(1);
|
|
|
|
expect(
|
|
|
|
await page.$eval(
|
|
|
|
'input',
|
|
|
|
(input: HTMLInputElement) => input.files[0].name
|
|
|
|
)
|
|
|
|
).toBe('file-to-upload.txt');
|
2019-07-23 04:30:49 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should be able to read selected file', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
await page.setContent(`<input type=file>`);
|
2020-05-07 10:54:55 +00:00
|
|
|
page
|
|
|
|
.waitForFileChooser()
|
|
|
|
.then((chooser) => chooser.accept([FILE_TO_UPLOAD]));
|
|
|
|
expect(
|
2020-07-02 09:09:34 +00:00
|
|
|
await page.$eval('input', async (picker: HTMLInputElement) => {
|
2020-05-07 10:54:55 +00:00
|
|
|
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');
|
2019-07-23 04:30:49 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should be able to reset selected files with empty file list', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
await page.setContent(`<input type=file>`);
|
2020-05-07 10:54:55 +00:00
|
|
|
page
|
|
|
|
.waitForFileChooser()
|
|
|
|
.then((chooser) => chooser.accept([FILE_TO_UPLOAD]));
|
|
|
|
expect(
|
2020-07-02 09:09:34 +00:00
|
|
|
await page.$eval('input', async (picker: HTMLInputElement) => {
|
2020-05-07 10:54:55 +00:00
|
|
|
picker.click();
|
|
|
|
await new Promise((x) => (picker.oninput = x));
|
|
|
|
return picker.files.length;
|
|
|
|
})
|
|
|
|
).toBe(1);
|
|
|
|
page.waitForFileChooser().then((chooser) => chooser.accept([]));
|
|
|
|
expect(
|
2020-07-02 09:09:34 +00:00
|
|
|
await page.$eval('input', async (picker: HTMLInputElement) => {
|
2020-05-07 10:54:55 +00:00
|
|
|
picker.click();
|
|
|
|
await new Promise((x) => (picker.oninput = x));
|
|
|
|
return picker.files.length;
|
|
|
|
})
|
|
|
|
).toBe(0);
|
2019-07-23 04:30:49 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should not accept multiple files for single-file input', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
await page.setContent(`<input type=file>`);
|
|
|
|
const [chooser] = await Promise.all([
|
|
|
|
page.waitForFileChooser(),
|
|
|
|
page.click('input'),
|
|
|
|
]);
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await chooser
|
|
|
|
.accept([
|
|
|
|
path.relative(
|
|
|
|
process.cwd(),
|
|
|
|
__dirname + '/assets/file-to-upload.txt'
|
|
|
|
),
|
|
|
|
path.relative(process.cwd(), __dirname + '/assets/pptr.png'),
|
|
|
|
])
|
|
|
|
.catch((error_) => (error = error_));
|
2019-07-23 04:30:49 +00:00
|
|
|
expect(error).not.toBe(null);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fail for non-existent files', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-24 11:36:46 +00:00
|
|
|
|
|
|
|
await page.setContent(`<input type=file>`);
|
|
|
|
const [chooser] = await Promise.all([
|
|
|
|
page.waitForFileChooser(),
|
|
|
|
page.click('input'),
|
|
|
|
]);
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await chooser
|
|
|
|
.accept(['file-does-not-exist.txt'])
|
|
|
|
.catch((error_) => (error = error_));
|
2020-04-24 11:36:46 +00:00
|
|
|
expect(error).not.toBe(null);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fail when accepting file chooser twice', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
await page.setContent(`<input type=file>`);
|
|
|
|
const [fileChooser] = await Promise.all([
|
|
|
|
page.waitForFileChooser(),
|
2020-07-02 09:09:34 +00:00
|
|
|
page.$eval('input', (input: HTMLInputElement) => input.click()),
|
2019-07-23 04:30:49 +00:00
|
|
|
]);
|
|
|
|
await fileChooser.accept([]);
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await fileChooser.accept([]).catch((error_) => (error = error_));
|
|
|
|
expect(error.message).toBe(
|
|
|
|
'Cannot accept FileChooser which is already handled!'
|
|
|
|
);
|
2019-07-23 04:30:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('FileChooser.cancel', function () {
|
|
|
|
it('should cancel dialog', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
// 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(),
|
2020-07-02 09:09:34 +00:00
|
|
|
page.$eval('input', (input: HTMLInputElement) => input.click()),
|
2019-07-23 04:30:49 +00:00
|
|
|
]);
|
|
|
|
await fileChooser1.cancel();
|
|
|
|
// If this resolves, than we successfully canceled file chooser.
|
|
|
|
await Promise.all([
|
|
|
|
page.waitForFileChooser(),
|
2020-07-02 09:09:34 +00:00
|
|
|
page.$eval('input', (input: HTMLInputElement) => input.click()),
|
2019-07-23 04:30:49 +00:00
|
|
|
]);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fail when canceling file chooser twice', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
await page.setContent(`<input type=file>`);
|
|
|
|
const [fileChooser] = await Promise.all([
|
|
|
|
page.waitForFileChooser(),
|
2020-07-02 09:09:34 +00:00
|
|
|
page.$eval('input', (input: HTMLInputElement) => input.click()),
|
2019-07-23 04:30:49 +00:00
|
|
|
]);
|
|
|
|
await fileChooser.cancel();
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await fileChooser.cancel().catch((error_) => (error = error_));
|
|
|
|
expect(error.message).toBe(
|
|
|
|
'Cannot cancel FileChooser which is already handled!'
|
|
|
|
);
|
2019-07-23 04:30:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
describeFailsFirefox('FileChooser.isMultiple', () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work for single file pick', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
await page.setContent(`<input type=file>`);
|
|
|
|
const [chooser] = await Promise.all([
|
|
|
|
page.waitForFileChooser(),
|
|
|
|
page.click('input'),
|
|
|
|
]);
|
|
|
|
expect(chooser.isMultiple()).toBe(false);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work for "multiple"', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
await page.setContent(`<input multiple type=file>`);
|
|
|
|
const [chooser] = await Promise.all([
|
|
|
|
page.waitForFileChooser(),
|
|
|
|
page.click('input'),
|
|
|
|
]);
|
|
|
|
expect(chooser.isMultiple()).toBe(true);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work for "webkitdirectory"', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-23 04:30:49 +00:00
|
|
|
await page.setContent(`<input multiple webkitdirectory type=file>`);
|
|
|
|
const [chooser] = await Promise.all([
|
|
|
|
page.waitForFileChooser(),
|
|
|
|
page.click('input'),
|
|
|
|
]);
|
|
|
|
expect(chooser.isMultiple()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|