Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix: throw error if setInputFile does not exist (#29944)

Max Schmitt7cbef691ae1b
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<T extends Node = Node> extends js.JSHandle<T> {
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);