Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Cypress.io to assert that a file download has been initiated without actually downloading?

Tags:

cypress

The download is initiated by setting

location.href = [some url to an image]

I want assert, as close as possible, that the download will succeed but without actually performing the download. That the URL is correct can be assumed.

like image 589
Rick Cotter Avatar asked Dec 21 '17 18:12

Rick Cotter


People also ask

How do you check if a file is downloaded or not in Cypress?

To verify that the file has been downloaded we can install simple Cypress command, which will set up downloads directory, will wait and will verify that the file is downloaded.


1 Answers

What you essentially want to do is stub a call to location.href using cy.stub(), but testing that your application has called location.href and asserting the url passed to it is a bit tricky since href is an attribute, not a function. Stubs cannot replace attributes, only functions.

This is testable with some restructure of your application code. There is a great answer on how to do this in this stackoverflow answer: https://stackoverflow.com/a/36678937/5878476

like image 175
Jennifer Shehane Avatar answered Oct 13 '22 01:10

Jennifer Shehane