question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] how to simulate a paste event

See original GitHub issue

Hi,

I tried the following code to trigger a paste event

    const browser = await chromium.launch(options);
    const context = await browser.newContext();
    await context.grantPermissions(["clipboard-read", "clipboard-write"]);
    const page = await context.newPage();
    await page.goto('http://example.com/');
    await page.evaluate(async (text) => {
        await navigator.clipboard.writeText(text);
        document.execCommand("paste");
    }, "123");
    await page.screenshot({ path: `example.png` });

But i got a rejection

(node:29654) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: __awaiter is not defined
    at __playwright_evaluation_script__:1:20
    at CRExecutionContext.evaluate (/home/project/node_modules/playwright-core/lib/chromium/crExecutionContext.js:82:23)
    at FrameManager.waitForSignalsCreatedBy (/home/project/node_modules/playwright-core/lib/frames.js:81:28)
    at FrameExecutionContext._doEvaluateInternal (/home/project/node_modules/playwright-core/lib/dom.js:42:16)
  -- ASYNC --
    at Frame.<anonymous> (/home/project/node_modules/playwright-core/lib/helper.js:63:23)
    at Page.evaluate (/home/project/node_modules/playwright-core/lib/page.js:257:33)
    at Page.<anonymous> (/home/project/node_modules/playwright-core/lib/helper.js:64:31)
    at /home/project/test/end2end/plop.spec.ts:16:16
    at step (/home/project/test/end2end/plop.spec.ts:33:23)
    at Object.next (/home/project/test/end2end/plop.spec.ts:14:53)
    at fulfilled (/home/project/test/end2end/plop.spec.ts:5:58)

I am not sure if it is an issue or if i am doing something wrong

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
pavelfeldmancommented, Jun 9, 2020

A couple of issues here:

  • navigator.clipboard is only available to content from the secure origins, replace url with https://example.com and it’ll be there. Secure origins include localhost.
  • document.execCommand("paste") will trigger the paste event, but won’t trigger the default behavior.
  • note that you are operating with the system clipboard, so other apps will have access to it and you can’t run tests like this simultaneously. We are working on resolving this last item and isolating clipboards of the browser contexts.
const { chromium } = require('playwright');

(async () => {
    const browser = await chromium.launch({ headless: false });
    const context = await browser.newContext();
    await context.grantPermissions(["clipboard-read", "clipboard-write"]);
    const page = await context.newPage();
    await page.goto('https://chromium.org');
    await page.evaluate(async (text) => {
        await navigator.clipboard.writeText(text);
        const clipText = await navigator.clipboard.readText();
        document.querySelector('#jot-ui-searchInput').value = clipText;
    }, "123");
    await page.screenshot({ path: `example.png` });
})();
0reactions
fzembowcommented, Nov 20, 2022

In case this helps anyone, I was able to emulate a text paste event using evaluate:

await page.locator('form').evaluate((formEl) => {
  const text = `Text to paste`;
  const clipboardData = new DataTransfer();
  clipboardData.setData('text/plain', text);
  const clipboardEvent = new ClipboardEvent('paste', {
    clipboardData
  });
  formEl.dispatchEvent(clipboardEvent);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to trigger `paste` event manually in javascript?
My question is how to trigger a paste event programmatically. You can't, without the browser having a special setting allowing it (I'm not ......
Read more >
Element: paste event - Web APIs | MDN
Element: paste event. The paste event is fired when the user has initiated a "paste" action through the browser's user interface.
Read more >
Programatically trigger paste event (ctrl+v) | SpreadJS
Programatically trigger paste event (ctrl+v) ... Hello,. I am trying to achieve the same results as ctrl+v but upon clicking a button. ......
Read more >
2 Ways to Trigger the onPaste Event with Testing-Library
Two examples (you only need one) showing how we render the paste component, trigger the on paste event and then test that it...
Read more >
onpaste Event - W3Schools
The onpaste event occurs when the user pastes some content into an element. The onpaste event is mostly used on <input> elements with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found