Paste event
<textarea id="txt"></textarea>
<script>
document
.getElementById('txt')
.addEventListener('paste', (e) => {
if (window.Cypress) {
e.target.value = e.clipboardData.getData('text')
}
})
</script>
Cypress.Commands.add(
'paste',
{ prevSubject: true, element: true },
($element, data) => {
const clipboardData = new DataTransfer()
clipboardData.setData('text', data)
const pasteEvent = new ClipboardEvent('paste', {
bubbles: true,
cancelable: true,
data,
clipboardData,
})
cy.get($element).then(() => {
$element[0].dispatchEvent(pasteEvent)
})
},
)
const greeting = `hello ${Cypress._.random(1, 1e6)}`
cy.get('#txt').paste(greeting).should('have.value', greeting)