Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download recipe cy command #860

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/testing-dom__download/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<a href=... download>` 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 `<a href=... download>` 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

Expand Down
Original file line number Diff line number Diff line change
@@ -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