Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(recorder): skip click action on <input type=file> (#40861)

Yury Semikhatsky2cf08d073de1
diff --git a/packages/playwright-core/src/server/recorder/recorderUtils.ts b/packages/playwright-core/src/server/recorder/recorderUtils.ts
index fca4472208..ed23d59b6d 100644
--- a/packages/playwright-core/src/server/recorder/recorderUtils.ts
+++ b/packages/playwright-core/src/server/recorder/recorderUtils.ts
@@ -97,6 +97,12 @@ export function shouldMergeAction(action: actions.ActionInContext, lastAction: a
return isSameAction(action, lastAction);
case 'click':
return isSameAction(action, lastAction) && isSameSelector(action, lastAction) && isShortlyAfter(action, lastAction) && action.action.clickCount > (lastAction.action as actions.ClickAction).clickCount;
+ case 'setInputFiles':
+ // Drop the click that opens the file picker — setInputFiles already does it,
+ // and on replay the leftover click() opens an extra picker that never closes.
+ // No time check: picking a file from the OS dialog can take many seconds and
+ // no other recorder actions can run while the dialog is open.
+ return lastAction.action.name === 'click' && isSameSelector(action, lastAction);
}
return false;
}
diff --git a/tests/library/inspector/cli-codegen-2.spec.ts b/tests/library/inspector/cli-codegen-2.spec.ts
index c1d09f7181..1bf8bf09b2 100644
--- a/tests/library/inspector/cli-codegen-2.spec.ts
+++ b/tests/library/inspector/cli-codegen-2.spec.ts
@@ -110,14 +110,21 @@ await page.CloseAsync();`);
</form>
`);
- await page.focus('input[type=file]');
- await page.setInputFiles('input[type=file]', asset('file-to-upload.txt'));
- await page.click('input[type=file]');
+ await recorder.hoverOverElement('input[type=file]');
+ const [chooser] = await Promise.all([
+ page.waitForEvent('filechooser'),
+ recorder.trustedClick(),
+ ]);
+ // Simulate the time a real user takes to pick a file from the OS dialog
+ // so the merge logic cannot rely on a short delay between click and setInputFiles.
+ await new Promise(f => setTimeout(f, 1000));
+ await chooser.setFiles(asset('file-to-upload.txt'));
const sources = await recorder.waitForOutput('JavaScript', 'setInputFiles');
expect(sources.get('JavaScript')!.text).toContain(`
await page.getByRole('button', { name: 'Choose File' }).setInputFiles('file-to-upload.txt');`);
+ expect(sources.get('JavaScript')!.text).not.toContain(`click()`);
expect(sources.get('Java')!.text).toContain(`
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Choose File")).setInputFiles(Paths.get("file-to-upload.txt"));`);