Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make uploadFile throw for non-existent files #5733

Merged
merged 2 commits into from Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/JSHandle.ts
Expand Up @@ -291,7 +291,24 @@ export class ElementHandle extends JSHandle {
// the cost unnecessarily.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
const files = filePaths.map(filePath => path.resolve(filePath));
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {promisify} = require('util');
const access = promisify(fs.access);
paullewis marked this conversation as resolved.
Show resolved Hide resolved

// Locate all files and confirm that they exist.
const files = await Promise.all(filePaths.map(async filePath => {
const resolvedPath: string = path.resolve(filePath);
try {
await access(resolvedPath, fs.constants.R_OK);
} catch (error) {
if (error.code === 'ENOENT')
throw new Error(`${filePath} does not exist or is not readable`);
}

return resolvedPath;
}));
const {objectId} = this._remoteObject;
const {node} = await this._client.send('DOM.describeNode', {objectId});
const {backendNodeId} = node;
Expand Down
12 changes: 12 additions & 0 deletions test/input.spec.js
Expand Up @@ -184,6 +184,18 @@ describe('input tests', function() {
]).catch(e => error = e);
expect(error).not.toBe(null);
});
it('should fail for non-existent files', async() => {
const {page} = getTestState();

await page.setContent(`<input type=file>`);
const [chooser] = await Promise.all([
page.waitForFileChooser(),
page.click('input'),
]);
let error = null;
await chooser.accept(['file-does-not-exist.txt']).catch(e => error = e);
expect(error).not.toBe(null);
});
it('should fail when accepting file chooser twice', async() => {
const {page} = getTestState();

Expand Down