From 7cbef691ae1bbe5b3ae6f8b8b24b14e5ac77870a Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Thu, 14 Mar 2024 20:27:33 +0100 Subject: [PATCH] fix: throw error if setInputFile does not exist (#29944) Fixes https://github.com/microsoft/playwright/issues/29941 --- packages/playwright-core/src/server/dom.ts | 9 +++++++-- tests/page/page-set-input-files.spec.ts | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/playwright-core/src/server/dom.ts b/packages/playwright-core/src/server/dom.ts index f2be757de4..b413018b2c 100644 --- a/packages/playwright-core/src/server/dom.ts +++ b/packages/playwright-core/src/server/dom.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import fs from 'fs'; import type * as channels from '@protocol/channels'; import * as injectedScriptSource from '../generated/injectedScriptSource'; import { isSessionClosedError } from './protocolError'; @@ -642,10 +643,14 @@ export class ElementHandle extends js.JSHandle { await progress.beforeInputAction(this); await this._page._frameManager.waitForSignalsCreatedBy(progress, options.noWaitAfter, async () => { progress.throwIfAborted(); // Avoid action that has side-effects. - if (localPaths) + if (localPaths) { + await Promise.all(localPaths.map(localPath => ( + fs.promises.access(localPath, fs.constants.F_OK) + ))); await this._page._delegate.setInputFilePaths(retargeted, localPaths); - else + } else { await this._page._delegate.setInputFiles(retargeted, filePayloads!); + } }); return 'done'; } diff --git a/tests/page/page-set-input-files.spec.ts b/tests/page/page-set-input-files.spec.ts index b43239596c..eb7aacaa97 100644 --- a/tests/page/page-set-input-files.spec.ts +++ b/tests/page/page-set-input-files.spec.ts @@ -105,6 +105,14 @@ it('should upload large file', async ({ page, server, browserName, isMac, isAndr await Promise.all([uploadFile, file1.filepath].map(fs.promises.unlink)); }); +it('should throw an error if the file does not exist', async ({ page, server, asset }) => { + await page.goto(server.PREFIX + '/input/fileupload.html'); + const input = await page.$('input'); + const error = await input.setInputFiles('i actually do not exist.txt').catch(e => e); + expect(error.message).toContain('ENOENT: no such file or directory'); + expect(error.message).toContain('i actually do not exist.txt'); +}); + it('should upload multiple large files', async ({ page, server, browserName, isMac, isAndroid, isWebView2, mode }, testInfo) => { it.skip(browserName === 'webkit' && isMac && parseInt(os.release(), 10) < 20, 'WebKit for macOS 10.15 is frozen and does not have corresponding protocol features.'); it.skip(isAndroid);