-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature]: Copy & Paste Clipboard emulation #8114
Comments
As of now, there's no copy-paste support. But IIUC Joel has some in-flight browser patches to make it possible in future! |
Thank you, I'm look forward to this feature |
Oooh, this totally looks like it's working in Firefox, right now! It'll be very cool to see this get into Chromium and Webkit, soon. |
Hey @mxschmitt , i noticed we added it into 1.15 and removed, then to 1.16 and then removed. Do you think this one can be supported in 1.17? Cheers |
@aslushnikov |
It would be really helpful to be able to emulate the copy & paste events on document level. Currently we can't test this behavior in our app because playwright won't trigger the copy & paste events on document level when pressing 'Meta+C' / 'Meta+V' |
Upvote for this 👍🏻 |
Sorry for the me-too comment, but this is a big adoption hurdle for us, as one of our users' main workflows depends on copy & paste. We'd appreciate the keyboard event simulation, as that is what our users will most often use. Triggering DOM events directly isn't our primary focus, as we already have UI elements that trigger these—but we do rely on their working correctly under the hood as our site fires them during several crucial workflows. Thanks! |
Adding my voice to the chorus! We have multiple tests that require access to the clipboard. |
+1 👍 |
We've recently hit something around this issue. We have a test that we want to check that a link is copied to the clipboard, and then follow that link later. Right now, we can't do this reliably as the clipboard context seems to be the OS clipboard - so I assume this would create issues with test parallelisation. We also want to run this in Chromium, Firefox, and Webkit. |
…#15390) This is what WebPageProxy::executeEditCommand does via WebPageProxy::willPerformPasteCommand. Refs: #8114, #12000 Pretty-diff: yury-s/WebKit@4213f02
+1 |
This issue has three parts in it that we'd like to track separately:
The shortcuts work now across the board and will be shipped in 1.24, so I'm closing this issue. Please upvote dedicated issues if you need them to help us prioritize accordingly! |
The note from the maintainers above has this snippet:
What's the best way to obtain Right now I'm doing: const isMac = process.env.HOME?.startsWith('/Users/') |
@chris13524 You can use |
For reference, here is the complete code needed for copy + paste export async function ctrlC(page: Page): Promise<void> {
const isMac = os.platform() === "darwin";
const modifier = isMac ? "Meta" : "Control";
await page.keyboard.press(`${modifier}+KeyC`);
}
export async function ctrlV(page: Page): Promise<void> {
const isMac = os.platform() === "darwin";
const modifier = isMac ? "Meta" : "Control";
await page.keyboard.press(`${modifier}+KeyV`);
} You also need to grant clipboard permissions with this: test("<test description>", async ({ page, context }) => {
await context.grantPermissions(["clipboard-read", "clipboard-write"]);
....
} |
Tried every variation of things shown here and it still won't copy+paste specifically for MacOS + chromium. |
@AlexGalays you tried my code above? That works for me on macOS + chromium |
Yes I did! After further tests, the difference with webkit is that the effects of await page.keyboard.press(`${modifier}+KeyV`) are not immediately visible, it's not synchronous: The content is not yet modified. I have to add a timeout wait of about 100ms. Works totally fine in webkit. Edit: Nevermind... proseMirror has a 50ms timeout when handling pastes. Thanks for your help :) |
How does it work with Playwright Java ? |
Does this technique works in headless mode ? So far I could only get it working in --headed |
It works on Linux (Chromium) in headless mode, but not on Mac (where it only works in headed mode for me 😢 ). |
For those who as me, wanted to put string into clipboard, those 2 npm packages didn't work under Linux X11 in headless mode: clipboardy (based on xsel), copy-paste (based on xclip) What finally worked was this:
async copy(text: string) {
const command = `navigator.clipboard.writeText(${JSON.stringify(text)})`
await executeJS(command)
}
async executeJS(js: string) {
return page.evaluate((javascript) => {
// eslint-disable-next-line no-eval
eval(javascript);
}, js);
} |
I made a blog post about how to do this with a copy-to-clipboard button https://blog.stevenboutcher.com/paste-text-from-your-clipboard-in-playwright-typescript?showSharer=true |
Your question
Is there any function to simulate Copy&Paste in playwright? like Ctrl+C Ctrl+V in my PC.
I had tried to make e2e tests for a product just like Google Sheets, I wanted to copy a cell and then paste it to another.
I had tried these way to do that:
document.execCommand('copy'), document.execCommand('paste')
,page.keyboard.press('Control+c'), page.keyboard.press('Control+v')
but these are didn't work. (I had get permissions: ['clipboard-read', 'clipboard-write'] for the context.)
It's any another way to simulate Copy&Paste?
Thanks for reading and hope to get your answer
Note from maintainers
Following works in all browsers
The text was updated successfully, but these errors were encountered: