Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/bin/chrome-devtools-cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
3 changes: 2 additions & 1 deletion src/bin/cliDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
24 changes: 20 additions & 4 deletions src/tools/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
zerone0x marked this conversation as resolved.
.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<HTMLInputElement>;
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
Expand All @@ -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.`,
Expand All @@ -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();
}
Expand Down