From 79e82e5b654f5c799ab52a3cb2b17775bf86afd3 Mon Sep 17 00:00:00 2001 From: Paul Lewis Date: Fri, 24 Apr 2020 12:36:46 +0100 Subject: [PATCH] fix: make uploadFile throw for non-existent files (#5733) --- src/JSHandle.ts | 19 ++++++++++++++++++- test/input.spec.js | 12 ++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/JSHandle.ts b/src/JSHandle.ts index fea9f49108b9c..f433eb46e5999 100644 --- a/src/JSHandle.ts +++ b/src/JSHandle.ts @@ -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); + + // 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; diff --git a/test/input.spec.js b/test/input.spec.js index e1aef5e7b38e8..6291cc97436c5 100644 --- a/test/input.spec.js +++ b/test/input.spec.js @@ -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(``); + 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();