cypress: Add support to get downloaded file when its name is not known.
It would be useful to have an option to parse downloaded files when their name is not known at the moment of download. I tried to create a helper function (task) for that which returns the latest file, however, it’s not reliable as it’s executed when file is not there yet:
// plugins/index.js
const getLastDownloadFilePath = () => {
const dirPath = 'cypress/downloads';
const filesOrdered = readdirSync(dirPath)
.map(entry => path.join(dirPath, entry))
.filter(entryWithPath => lstatSync(entryWithPath).isFile())
.map(fileName => ({ fileName, mtime: lstatSync(fileName).mtime }))
.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
return filesOrdered.length ? filesOrdered[0].fileName : false;
};
I believe it may be useful to have the ability to:
- wait for file download,
- get the name of the downloaded file.
These two should allow handling such cases.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 2
- Comments: 21 (5 by maintainers)
There’s an example of reading a file directly after download by looking for extension. Is this helpful? https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/testing-dom__download/cypress/integration/spec.js#L263
@viktorgogulenko thanks for that, that should work when the file is generated in the backend and later sent in the response. Unfortunately, in my case, the file is generated by frontend code with
file-saverlibrary. Therefore there’s no additional request with a filename in it.I managed to create a workaround with:
and in test, I use it with
wait-untilplugin:I solved this issue with really great feature of Cypress - interceptor. Filename you can automatically grab from ‘content-disposition’ response header.
Here is a code snippet:
In this way you don’t need any manipulations with downloads folder like finding the last downloaded file, delete all files before test etc. - you have exact filename and you can do with this file whatever you need. Hopefully it will help you.
Seems that the link is broken