cypress: document.execCommand("copy") does not work in cypress
Hi there,
I’m curious as to why a document.execCommand("copy") fails when being run during a cypress test (as a side effect to a cy.get().click() ). Curiously it works fine once I’m manually clicking the same element after the test has run.
The failing test in question can be reproduced by running the following code:
describe("menuBar", function() {
beforeEach(() => {
cy.visit("http://teselagen.github.io/openVectorEditor/#/Editor");
});
it("select range, copy, cut works", function() {
cy.clock()
cy.get('.tg-menu-bar').contains("Edit").click()
cy.get('.tg-menu-bar-popover').contains("Select").click()
cy.get(`[label="From:"]`).clear().type("10")
cy.get(`[label="To:"]`).clear().type("20")
cy.get(`.dialog-buttons`).contains("OK").click()
cy.get(".veStatusBar").contains(`10 to 20`)
cy.get(".veStatusBar").contains(`5299`)
cy.get('.tg-menu-bar').contains("Edit").click().tick(200)
cy.get('.tg-menu-bar-popover').contains("Copy").click()
cy.contains("Selection Copied")
cy.get('.tg-menu-bar').contains("Edit").click()
cy.get('.tg-menu-bar-popover').contains("Cut").click()
cy.contains("Selection Cut")
cy.get(".veStatusBar").contains(`5288`)
});
});
If you run the above example, the test will fail to find “Selection Copied”. This is because
const worked = document.execCommand(type);
evaluates to false so the “Selection Copied” popover is not shown.
You can add a breakpoint manually to the commands/index.js file and see it not working.

Strangely, after the test has run if you manually click “File > Edit > Copy” then the document.execCommand("copy") works just fine. Not sure what is going on here to stop it from working when cypress is running its tests.


Thanks!
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 21
- Comments: 20 (12 by maintainers)
@Bkucera ship it! I’d love to use your suggested
Is there any update on this?
This issue is still in the ‘ready for work’ stage, which means no work has been done on this issue as of today, so we do not have an estimate on when this will be delivered.
There is a way we can make this testable. We can overwrite
document.execCommandand store the copied text in a “fake” clipboard object that exists during the lifetime of the test. That way you could do something like:This is just one possible version of the API. I would appreciate feedback
@jennifer-shehane Any news on copy/pasting feature in Cypress?
Is there any update on this?
@dwelle I believe we can check the stack for a cypress click command, using rules that mimic the rules that the browser follows for user interaction. So when execCommand is called we can return true/false and push to clipboard conditionally