Downloads folder not emptying
See original GitHub issueCurrent behavior
When my app downloads files, the downloads folder never gets cleared. The documentation says it should get cleared after each test, but this is not happening. I am having to use rimraf to make that happen but would love if Cypress could handle this.
On a side note, it would be really nice if their was a simple way to view the files in that downloads folder to ensure the correct file with filename downloaded as expected. Right now I have to create a task and use globby (which doesn’t seem very reliable) to make that happen.
Desired behavior
My downloads folder should get emptied after each test.
Test code to reproduce
it('can download JPG when clicking on Download digital passes button', () => {
cy.intercept({ method: 'POST', path: '/file/location/here' }).as('interceptName');
cy.getByAriaLabel('Download digital passes').click();
cy.wait('@interceptName').its('request').then(({ body }) => {
expect(body).to.have.property('someProperty', 'JPG');
});
});
it('has 1 filename that contains code, Passenger Number and airport from/to codes in downloaded file', () => {
cy.task('findFileList', `${downloadsFolder}/*.jpg`).then((fileList) => {
const { recordLocator, segments, passengers } = window.bookingData;
const from = segments[0].departure.airport.airportCode;
const to = segments[segments.length - 1].arrival.airport.airportCode;
const expectedFileName = `${downloadsFolder}/0-${passengers[0].number} ${from}-${to} (${recordLocator}).jpg`;
expect(fileList.includes(expectedFileName)).to.equal(true);
});
});
// the task in plugins/index.js
on('task', {
// a task to find one file matching the given mask
// returns just the first matching file
findFileList(mask) {
if (!mask) {
throw new Error('Missing a file mask to seach');
}
console.log('searching for files %s', mask);
return globby(mask).then((list) => {
if (!list.length) {
throw new Error(`Could not find files matching mask "${mask}"`);
}
console.log('found files: %s', list);
return list;
});
},
});
Versions
Cypress 6.4.0, latest version of OSX on Macbook Pro using Chrome 88.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:6 (1 by maintainers)
Top Results From Across the Web
My Downloads folder is suddenly empty. What could have ...
Honestly I would advise against using the Downloads folder other than for downloading files, and once they are downloaded, copy them into ...
Read more >Simple Methods To Fix Downloads Folder Not Responding On ...
Go back to your Downloads folder and check whether the document is in the folder, if it is empty go back to the...
Read more >My Files Disappeared from Downloads. How Do I ... - Ask Leo!
Downloads is for downloads, not for storage ... An empty Downloads folder. In my opinion, the Downloads folder should never be used for...
Read more >How to Clear Your Downloads Folder - Computer Hope
To permanently delete the files, right-click the Recycle Bin icon on your desktop and select Empty Recycle Bin in the drop-down menu that ......
Read more >Folder Shows Empty but Files Are There Windows 10/8/7
The virus hides the files and makes the folder shows empty but has size. Other probable reasons are improper removal of the external...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@sousajunior @Jakejamesreid - I wrote a solution, a little hacky but it works like a charm for me for this functionality. It will clear downloads folder on cy:open
Function for clearing the downloads folder:
Command to run, chained with cy:open, to invoke the function to clear the downloads folder
node cypress/support/clearDownloads.js && cypress open
@sousajunior Figured I would provide an update for the above solution, I’ve worked it into a beforeEach hook by utilizing the plugins file. This will clear the contents of the cypress/downloads folder before each scenario is run
Please bear in mind that there should be better error reporting here - I consider this to be minimum-viable.
The trick is to use cy.task() to get Cypress to do what you want it to, and to use it within a hook.