Some websites allow users to copy a value, like a URL or some text, to the clipboard by clicking a button (e.g., “Copy to clipboard” in the screenshot below). The following snippet allows you to check that the correct value was actually copied.
How to set it up
Add a Playwright code step after the step that clicks the “Copy” button.
Paste the following code into the Playwright step:
// Grant clipboard permissions for reading and writing
context.grantPermissions([
'clipboard-write',
'clipboard-read',
]);
// Read the clipboard content
const result = await page.evaluate(async () => {
return await navigator.clipboard.readText();
});
// Verify that the clipboard contains the expected text
expect(result).toContain('expected_copied_text_value');
// Optional: Log the clipboard value to the browser console
await page.evaluate(r => console.log(r), result);
Replace
expected_copied_text_value
with the expected value you want to assert against.
The code in this step may capture other content if you copy/paste something else on your computer when it’s being executed