question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

document.execCommand("copy") does not work in cypress

See original GitHub issue

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. image

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.

image

image

Thanks!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:18
  • Comments:20 (12 by maintainers)

github_iconTop GitHub Comments

16reactions
tnrichcommented, Jan 28, 2020

@Bkucera ship it! I’d love to use your suggested

cy.get('button').click().then(()=>{
  expect(cy.clipboard()).to.contain('sometext')
})
9reactions
agugutcommented, Mar 4, 2021

Is there any update on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

cypress-io/cypress - Gitter
I have a function to copy some input but not sure how to test it was copied ... execCommand("copy") doesn't even work in...
Read more >
Handling Copy and Paste in Cypress - Egghead.io
Let's look at the first example. I have a very simple app here that has two input fields. The first input field will...
Read more >
Copying to clipboard with document.execCommand('copy ...
execCommand to copy it to the clipboard. This usually works but fails (returns false) when the text is large. In Chrome v55, it...
Read more >
Spy On Clipboard Copy Method Call - Gleb Bahmutov
Calling the clipboard.writeText method is the problem. We can stub it from a Cypress test using the included cy.stub command.
Read more >
Interact with the clipboard - Mozilla - MDN Web Docs
On Firefox, it will not work, and you'll see a message like this in your console: document.execCommand('cut'/'copy') was denied because it was ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found