Last active
January 25, 2023 11:42
-
-
Save mbinic/e75a8910ec51a27a041f967e5b3a5345 to your computer and use it in GitHub Desktop.
Cypress - get navigator.clipboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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