Skip to content

Commit 5f4d2cb

Browse files
authored
fix: Replace path with parentPath (#39275)
1 parent cd36dab commit 5f4d2cb

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

packages/playwright-core/src/client/elementHandle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ export async function convertInputFiles(platform: Platform, files: string | File
289289
context._checkFileAccess(localDirectory);
290290

291291
if (context._connection.isRemote()) {
292-
const files = localDirectory ? (await platform.fs().promises.readdir(localDirectory, { withFileTypes: true, recursive: true })).filter(f => f.isFile()).map(f => platform.path().join(f.path, f.name)) : localPaths!;
292+
const files = localDirectory ? (await platform.fs().promises.readdir(localDirectory, { withFileTypes: true, recursive: true })).filter(f => f.isFile()).map(f => platform.path().join(f.parentPath, f.name)) : localPaths!;
293293
const { writableStreams, rootDir } = await context._wrapApiCall(async () => context._channel.createTempFiles({
294294
rootDirName: localDirectory ? platform.path().basename(localDirectory) : undefined,
295295
items: await Promise.all(files.map(async file => {

tests/library/browsertype-connect.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,44 @@ for (const kind of ['launchServer', 'run-server'] as const) {
733733
await Promise.all([uploadFile, file1.filepath].map(fs.promises.unlink));
734734
});
735735

736+
test('should upload a folder', async ({ connect, startRemoteServer, server }, testInfo) => {
737+
test.slow();
738+
const remoteServer = await startRemoteServer(kind);
739+
const browser = await connect(remoteServer.wsEndpoint());
740+
const context = await browser.newContext();
741+
const page = await context.newPage();
742+
743+
await page.goto(server.PREFIX + '/input/folderupload.html');
744+
const input = await page.$('input');
745+
const folderName = 'folder-upload-test';
746+
const dir = testInfo.outputPath(folderName);
747+
{
748+
await fs.promises.mkdir(dir, { recursive: true });
749+
await fs.promises.writeFile(path.join(dir, 'file1.txt'), 'file1 content');
750+
await fs.promises.writeFile(path.join(dir, 'file2'), 'file2 content');
751+
await fs.promises.mkdir(path.join(dir, 'sub-dir'));
752+
await fs.promises.writeFile(path.join(dir, 'sub-dir', 'really.txt'), 'sub-dir file content');
753+
}
754+
await input.setInputFiles(dir);
755+
756+
const webkitRelativePaths = await page.evaluate(e => [...e.files].map(f => f.webkitRelativePath), input);
757+
expect(new Set(webkitRelativePaths)).toEqual(new Set([
758+
`${folderName}/file1.txt`,
759+
`${folderName}/file2`,
760+
`${folderName}/sub-dir/really.txt`,
761+
]));
762+
763+
for (let i = 0; i < webkitRelativePaths.length; i++) {
764+
const content = await input.evaluate((e, i) => {
765+
const reader = new FileReader();
766+
const promise = new Promise(fulfill => reader.onload = fulfill);
767+
reader.readAsText(e.files[i]);
768+
return promise.then(() => reader.result);
769+
}, i);
770+
expect(content).toEqual(fs.readFileSync(path.join(dir, '..', webkitRelativePaths[i])).toString());
771+
}
772+
});
773+
736774
test('setInputFiles should preserve lastModified timestamp', async ({ connect, startRemoteServer, asset }) => {
737775
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/27452' });
738776
const remoteServer = await startRemoteServer(kind);

0 commit comments

Comments
 (0)