Skip to content
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

Closed
done3t opened this issue Aug 10, 2021 · 24 comments
Closed

[Feature]: Copy & Paste Clipboard emulation #8114

done3t opened this issue Aug 10, 2021 · 24 comments
Assignees

Comments

@done3t
Copy link

done3t commented Aug 10, 2021

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:

  1. use document.execCommand('copy'), document.execCommand('paste'),
  2. use 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

  const modifier = isMac ? 'Meta' : 'Control';
  await page.setContent(`<div contenteditable>123</div>`);
  await page.focus('div');
  await page.keyboard.press(`${modifier}+KeyA`);
  await page.keyboard.press(`${modifier}+KeyC`);
  await page.keyboard.press(`${modifier}+KeyV`);
  await page.keyboard.press(`${modifier}+KeyV`);
  expect(await page.evaluate(() => document.querySelector('div').textContent)).toBe('123123');
@aslushnikov
Copy link
Contributor

As of now, there's no copy-paste support. But IIUC Joel has some in-flight browser patches to make it possible in future!

@done3t
Copy link
Author

done3t commented Aug 11, 2021

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

@Westbrook
Copy link
Contributor

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.

@mxschmitt mxschmitt added v1.16 and removed v1.15 labels Sep 7, 2021
@mxschmitt mxschmitt changed the title [Question]: How to simulate Copy&Paste by playwright? [Feature]: Copy & Paste Clipboard emulation Sep 20, 2021
@xuanzhaopeng
Copy link

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

@akgupta0777
Copy link

@aslushnikov
If we cannot use copy paste in playwright. Do we have clipboard access ? can I access the contents of clipboard ?
Please Help.

@ChristofFritz
Copy link

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'

@cscheffauer
Copy link

Upvote for this 👍🏻

@adimit
Copy link

adimit commented Feb 21, 2022

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!

@kingmatte
Copy link

Adding my voice to the chorus! We have multiple tests that require access to the clipboard.

@PedroDivergence
Copy link

+1 👍

@mrmckeb
Copy link

mrmckeb commented Apr 20, 2022

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.

@kiramoonsun
Copy link

+1

@aslushnikov
Copy link
Contributor

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!

@chris13524
Copy link

chris13524 commented May 16, 2023

The note from the maintainers above has this snippet:

const modifier = isMac ? 'Meta' : 'Control';

What's the best way to obtain isMac?

Right now I'm doing:

const isMac = process.env.HOME?.startsWith('/Users/')

@kevin940726
Copy link
Contributor

kevin940726 commented May 17, 2023

@chris13524 You can use os.platform() or process.platform and check if it is darwin.

@WesleyYue
Copy link

WesleyYue commented Jun 9, 2023

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"]);
    ....
}

@AlexGalays
Copy link

Tried every variation of things shown here and it still won't copy+paste specifically for MacOS + chromium.

@WesleyYue
Copy link

@AlexGalays you tried my code above? That works for me on macOS + chromium

@AlexGalays
Copy link

AlexGalays commented Jun 15, 2023

@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 :)

@Mmallineni
Copy link

How does it work with Playwright Java ?

@Overdrivr
Copy link

Does this technique works in headless mode ? So far I could only get it working in --headed

@skogsmaskin
Copy link

skogsmaskin commented May 2, 2024

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 😢 ).

@gladykov
Copy link

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);
    }

@steven-the-qa
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests