cypressheadless

Running cypress headless .then timed out after waiting `4000ms`


I have this bit of Cypress code that clicks a button and it navigates to the url that's on the clipboard.

cy.get('.process-list').within(() => {
    cy.get('.process-list-item').eq(3).within(() => {
        cy.get('.nakedButton').eq(0).click();
        cy.wait(1000);
    });
});

cy.window().then(win => {
    return win.navigator.clipboard.readText().then(text => {
        cy.log(text).then(() => {
            cy.visit(text);
        });
    });
});

This works fine when I run it via cypress desktop ui. When I run the test in headless mode on chrome via power shell, I get the following error:

CypressError: cy.then() timed out after waiting 4000ms.

Your callback function returned a promise that never resolved.

The callback function was:

win => {
    return win.navigator.clipboard.readText().then(text => {
      cy.log(text).then(() => {
        text = text.replace("https://", "http://");
        cy.visit(text);
      });
    });
  }

Any recommendations on how to resolve this so it passes when it runs in headless mode? Thank you.


Solution

  • I fixed the issue by adding this package to my project:

    npm install cypress-clipboard;
    

    Add this using statement to your test:

    import 'cypress-clipboard';
    

    Add this code to your cypress test, it assigns the clipboard the read/write permission:

    cy.wrap(Cypress.automation('remote:debugger:protocol', {
        command: 'Browser.grantPermissions',
        params: {
            permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
            origin: window.location.origin
        },
    }));
    

    The test works fine now when running headless via PowerShell and when running via cypress ui.