Skip to content

Instantly share code, notes, and snippets.

@mbinic
Last active January 25, 2023 11:42
Show Gist options
  • Save mbinic/e75a8910ec51a27a041f967e5b3a5345 to your computer and use it in GitHub Desktop.
Save mbinic/e75a8910ec51a27a041f967e5b3a5345 to your computer and use it in GitHub Desktop.
Cypress - get navigator.clipboard
// defined as: clipboard(): Chainable<Clipboard>;
Cypress.Commands.add('clipboard', () => {
cy.wrap(
Cypress.automation('remote:debugger:protocol', {
command: 'Browser.grantPermissions',
params: {
permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
origin: window.location.origin,
},
}).catch((error) =>
// Electron (v106) will land here, but that's ok, cause the permissions will be granted anyway
Cypress.log({ message: `Permission request failed: ${error.message}` })
)
);
return cy.window().then(async (win) => {
win.focus();
return win.navigator.clipboard;
});
});
// usage for e.g. writing text
// cy.clipboard().then(clipboard => clipboard.writeText('some text'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment