From 6295c0f18769b9da3175c2ed98987e11e4f82252 Mon Sep 17 00:00:00 2001 From: Abraham Brookes Date: Sat, 16 Dec 2023 10:57:47 +1000 Subject: [PATCH 1/2] Create download-click-command.js adds a recipe showing the cypress command for download button clicks --- .../cypress/e2e/download-click-command.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 examples/testing-dom__download/cypress/e2e/download-click-command.js diff --git a/examples/testing-dom__download/cypress/e2e/download-click-command.js b/examples/testing-dom__download/cypress/e2e/download-click-command.js new file mode 100644 index 00000000..8d5d9813 --- /dev/null +++ b/examples/testing-dom__download/cypress/e2e/download-click-command.js @@ -0,0 +1,22 @@ +// add this to your cypress commands.js file: + +/** + * When we download a file with a button click, Cypress expects a page reload. This causes our tests to fail even when the + * file is downloaded successfully. This helper fudges a page reload so that Cypress doesn't fail the test. + */ +Cypress.Commands.add('downloadClick', (callBack, timeout = 5000) => { + cy.window().then((win) => { + win.document.addEventListener('click', () => { + setTimeout(() => { + win.document.location.reload(); + }, timeout); + }); + + callBack(); + }); +}) + +// and then in your test you can use it like so: +cy.downloadClick(() => { + cy.get('.your-download-button').click() +}, 3000) // passing in the timeout is optional From 263206efa44ddfb592184b41a62f38c56d900a7e Mon Sep 17 00:00:00 2001 From: Abraham Brookes Date: Sat, 16 Dec 2023 11:01:22 +1000 Subject: [PATCH 2/2] Update README.md adds a link to the command spec file --- examples/testing-dom__download/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/testing-dom__download/README.md b/examples/testing-dom__download/README.md index d3a8010c..1f635f42 100644 --- a/examples/testing-dom__download/README.md +++ b/examples/testing-dom__download/README.md @@ -16,6 +16,7 @@ Spec file | Description [local-download-spec.cy.js](./cypress/e2e/local-download-spec.cy.js) | Downloads files from local domain by using `` anchor links [location-href-spec.cy.js](./cypress/e2e/location-href-spec.cy.js) | Intercepts and verifies a file downloaded via setting `document.location.href=...` URL [remote-download-spec.cy.js](./cypress/e2e/remote-download-spec.cy.js) | Downloads files from another domain by using `` anchor links +[download-click-command.js](./cypress/e2e/download-click-command.js) | Add a cypress helper to allow you to avoid "waiting for page load" ## Notes