microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
64.05k stars 3.47k forks source link

[Bug] writing to browser clipboard throws a DOMException: Write permission denied when running headless #29472

Closed marcusNumminen closed 5 months ago

marcusNumminen commented 5 months ago

Version

1.41.2

Steps to reproduce

Run this test both headed and headless;

test('has title', async ({ page }) => {
  await page.goto('https://playwright.dev/');
  await expect(page).toHaveTitle(/Playwright/);

  await page.evaluate('navigator.clipboard.writeText("XXXYYYZZZXXXYYYZZZ")');
});

In headed mode this works fine but in headless mode I get the error: Error: page.evaluate: DOMException: Write permission denied.

Expected behavior

I expect the same behavior in headed and headless mode

Actual behavior

I get different behavior in headed and headless mode

Additional context

No response

Environment

System:
    OS: Windows 10 10.0.19045
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
    Memory: 21.29 GB / 31.85 GB
  Binaries:
    Node: 18.18.0 - C:\Program Files\nodejs\node.EXE
    npm: 9.8.1 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.10.2 - C:\Program Files\nodejs\pnpm.CMD
  IDEs:
    VSCode: 1.86.1 - C:\Users\marcus.numminen\AppData\Local\Programs\Microsoft VS Code\bin\code.CMD
  Languages:
    Bash: 5.2.21 - C:\Program Files\Git\usr\bin\bash.EXE
  npmPackages:
    @playwright/test: ^1.41.2 => 1.41.2
mxschmitt commented 5 months ago

In order to use clipboard, you need to give it clipboard permissions like that:

test.use({
    permissions: ['clipboard-write']
})

test('has title', async ({ page }) => {
  await page.goto('https://playwright.dev/');
  await expect(page).toHaveTitle(/Playwright/);

  await page.evaluate('navigator.clipboard.writeText("XXXYYYZZZXXXYYYZZZ")');
});

in headed/headless the concept of permissions is implement different on Chromium since, hence its working. So far there's no good clipboard support in browsers for automation. Even the clipboard that works in headful version of browsers is not well isolated from the OS, so other things can interfere with running tests.

There's a request for better clipboard isolation: https://github.com/microsoft/playwright/issues/13097

I'll close this in favor of that request.