From 7ce9b7e56be482cda33195b0694c8dffcf4075f3 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Tue, 16 Jul 2024 15:55:35 +0200 Subject: [PATCH] fix(setInputFiles): throw when uploading file in directory upload (#31706) --- CONTRIBUTING.md | 2 +- packages/playwright-core/src/server/dom.ts | 2 ++ tests/page/page-set-input-files.spec.ts | 12 ++++++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5b71ccde04..30e89e6bee 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -125,7 +125,7 @@ All API classes, methods, and events should have a description in [`docs/src`](h To run the documentation linter, use: ```bash -npm run doclint +npm run doc ``` To build the documentation site locally and test how your changes will look in practice: diff --git a/packages/playwright-core/src/server/dom.ts b/packages/playwright-core/src/server/dom.ts index 30eb04d3f6..52e648abe0 100644 --- a/packages/playwright-core/src/server/dom.ts +++ b/packages/playwright-core/src/server/dom.ts @@ -639,6 +639,8 @@ export class ElementHandle extends js.JSHandle { throw injected.createStacklessError('Non-multiple file input can only accept single file'); if (directoryUpload && !inputElement.webkitdirectory) throw injected.createStacklessError('File input does not support directories, pass individual files instead'); + if (!directoryUpload && inputElement.webkitdirectory) + throw injected.createStacklessError('[webkitdirectory] input requires passing a path to a directory'); return inputElement; }, { multiple, directoryUpload: !!localDirectory }); if (result === 'error:notconnected' || !result.asElement()) diff --git a/tests/page/page-set-input-files.spec.ts b/tests/page/page-set-input-files.spec.ts index 76a53fa571..a32db203bd 100644 --- a/tests/page/page-set-input-files.spec.ts +++ b/tests/page/page-set-input-files.spec.ts @@ -120,6 +120,15 @@ it('should throw when uploading a folder in a normal file upload input', async ( await expect(input.setInputFiles(dir)).rejects.toThrow('File input does not support directories, pass individual files instead'); }); +it('should throw when uploading a file in a directory upload input', async ({ page, server, isAndroid, asset }) => { + it.skip(isAndroid); + it.skip(os.platform() === 'darwin' && parseInt(os.release().split('.')[0], 10) <= 21, 'WebKit on macOS-12 is frozen'); + + await page.goto(server.PREFIX + '/input/folderupload.html'); + const input = await page.$('input'); + await expect(input.setInputFiles(asset('file to upload.txt'))).rejects.toThrow('[webkitdirectory] input requires passing a path to a directory'); +}); + it('should upload a file after popup', async ({ page, server, asset }) => { it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/29923' }); await page.goto(server.PREFIX + '/input/fileupload.html'); @@ -341,8 +350,7 @@ it('should emit event via prepend', async ({ page, server }) => { expect(chooser).toBeTruthy(); }); -it('should emit event for iframe', async ({ page, server, browserName }) => { - it.skip(browserName === 'firefox'); +it('should emit event for iframe', async ({ page, server }) => { const frame = await attachFrame(page, 'frame1', server.EMPTY_PAGE); await frame.setContent(``); const [chooser] = await Promise.all([