Home > Mobile >  Wait for page to load after click() on download button
Wait for page to load after click() on download button

Time:06-29

It is possible to somehow skip waiting for page to load, after Cypress test click() on download button which start download but does not redirect to other page?

it('name of the test', () =>{
        cy.get('#btnGeneratePdf').click()
        .url().should('contain', "/Closed"); //url is same as before than button for download was clicked

})

CodePudding user response:

This is a known Cypress problem. A lot of related issues have been opened on Cypress github, however you can follow the main discussion in this one.

Right now you cannot skip the Cypress waiting for load after clicking the anchor, but there is a workaround - you can force your page to reload after the file has been downloaded, to make the test not fail. There is a 5 seconds timeout that you can adjust to make sure the file gets to download before the page reloads.

cy.window().document().then(function (doc) {
  doc.addEventListener('click', () => {
    // this adds a listener that reloads your page 
    // after 5 seconds from clicking the download button
    setTimeout(function () { doc.location.reload() }, 5000)
  })
  cy.get('#btnGeneratePdf').click()
})

This issue has been around for a long time. Hopefully Cypress team will resolve it soon.

  • Related