diff --git a/src/bin/chrome-devtools-cli-options.ts b/src/bin/chrome-devtools-cli-options.ts index 6e317a333..4f0f1243f 100644 --- a/src/bin/chrome-devtools-cli-options.ts +++ b/src/bin/chrome-devtools-cli-options.ts @@ -710,7 +710,8 @@ export const commands: Commands = { filePath: { name: 'filePath', type: 'string', - description: 'The local path of the file to upload', + description: + 'The local path of the file to upload. For multiple files, pass a comma-separated list.', required: true, }, includeSnapshot: { diff --git a/src/bin/cliDefinitions.ts b/src/bin/cliDefinitions.ts index d32705617..e3f2485c0 100644 --- a/src/bin/cliDefinitions.ts +++ b/src/bin/cliDefinitions.ts @@ -691,7 +691,8 @@ export const commands: Commands = { filePath: { name: 'filePath', type: 'string', - description: 'The local path of the file to upload', + description: + 'The local path of the file to upload. For multiple files, pass a comma-separated list.', required: true, }, includeSnapshot: { diff --git a/src/tools/input.ts b/src/tools/input.ts index 492d55378..ec9b796cd 100644 --- a/src/tools/input.ts +++ b/src/tools/input.ts @@ -361,17 +361,29 @@ export const uploadFile = definePageTool({ .describe( 'The uid of the file input element or an element that will open file chooser on the page from the page content snapshot', ), - filePath: zod.string().describe('The local path of the file to upload'), + filePath: zod + .string() + .describe( + 'The local path of the file to upload. For multiple files, pass a comma-separated list.', + ), includeSnapshot: includeSnapshotSchema, }, handler: async (request, response) => { const {uid, filePath} = request.params; + const filePaths = filePath + .split(',') + .map(path => path.trim()) + .filter(Boolean); + + if (!filePaths.length) { + throw new Error('Provide filePath to upload.'); + } const handle = (await request.page.getElementByUid( uid, )) as ElementHandle; try { try { - await handle.uploadFile(filePath); + await handle.uploadFile(...filePaths); } catch { // Some sites use a proxy element to trigger file upload instead of // a type=file element. In this case, we want to default to @@ -381,7 +393,7 @@ export const uploadFile = definePageTool({ request.page.pptrPage.waitForFileChooser({timeout: 3000}), handle.asLocator().click(), ]); - await fileChooser.accept([filePath]); + await fileChooser.accept(filePaths); } catch { throw new Error( `Failed to upload file. The element could not accept the file directly, and clicking it did not trigger a file chooser.`, @@ -391,7 +403,11 @@ export const uploadFile = definePageTool({ if (request.params.includeSnapshot) { response.includeSnapshot(); } - response.appendResponseLine(`File uploaded from ${filePath}.`); + response.appendResponseLine( + filePaths.length === 1 + ? `File uploaded from ${filePaths[0]}.` + : `Files uploaded from ${filePaths.join(', ')}.`, + ); } finally { void handle.dispose(); }