From ce142edc2e20d7a2981ca11a112459271063328e Mon Sep 17 00:00:00 2001 From: Randolf J Date: Sat, 4 Jun 2022 01:21:57 +0200 Subject: [PATCH] fix: don't throw on bad access --- src/common/JSHandle.ts | 28 ++++++++++++---------------- test/input.spec.ts | 14 -------------- 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/src/common/JSHandle.ts b/src/common/JSHandle.ts index 59c9a17ef409d..d44adb28113fb 100644 --- a/src/common/JSHandle.ts +++ b/src/common/JSHandle.ts @@ -801,9 +801,12 @@ export class ElementHandle< /** * This method expects `elementHandle` to point to an * {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input | input element}. + * * @param filePaths - Sets the value of the file input to these paths. - * If some of the `filePaths` are relative paths, then they are resolved - * relative to the {@link https://nodejs.org/api/process.html#process_process_cwd | current working directory} + * If a path is relative, then they it is resolved relative to the + * {@link https://nodejs.org/api/process.html#process_process_cwd | current working directory}. + * Note for locals script connecting to remote chrome environments, + * paths must be absolute. */ async uploadFile(...filePaths: string[]): Promise { const isMultiple = await this.evaluate<(element: Element) => boolean>( @@ -829,21 +832,14 @@ export class ElementHandle< avoid paying the cost unnecessarily. */ const path = await import('path'); - const fs = await import('fs'); // 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 fs.promises.access(resolvedPath, fs.constants.R_OK); - } catch (error) { - if (error && (error as NodeJS.ErrnoException).code === 'ENOENT') - throw new Error(`${filePath} does not exist or is not readable`); - } - - return resolvedPath; - }) - ); + const files = filePaths.map((filePath) => { + if (path.isAbsolute(filePath)) { + return filePath; + } else { + return path.resolve(filePath); + } + }); const { objectId } = this._remoteObject; const { node } = await this._client.send('DOM.describeNode', { objectId }); const { backendNodeId } = node; diff --git a/test/input.spec.ts b/test/input.spec.ts index 9244356333a34..a7e772ac1e45f 100644 --- a/test/input.spec.ts +++ b/test/input.spec.ts @@ -235,20 +235,6 @@ describe('input tests', function () { .catch((error_) => (error = error_)); 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((error_) => (error = error_)); - expect(error).not.toBe(null); - }); it('should fail when accepting file chooser twice', async () => { const { page } = getTestState();