Read Clipboard Text In Cypress

Mohd Jeeshan
Jul 23, 2022

--

In this article I will be talking about reading the Clipboard text from browser using Cypress.

Problem Statement

We need to read the text from the clipboard. Navigate to npm registry and search for any node modules.

If you click npm i uuid, text will be copied to clipboard. We need to print this text from the clipboard using Cypress.

Solution

Open the Devtools in chrome and type navigator.clipboard

We will be using readText to read the copied text from Clipboard.

Code

cy.window().its(‘navigator.clipboard’).invoke(‘readText’).then(cy.log);

We are using cy.window() to access the window object and use its() to call navigator.clipboard and then using invoke() we call readText method 💡.

clipboard.cy.js
execution.png

Whoaaaah !!!! You can see we are able to see the text in test runner.

That’s all for this article 😁 !!!

--

--