Skip to content

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

@tnrich

Description

@tnrich

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!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions