How do i delete downloaded file in using cypress

I'm trying to delete a downloaded file before my next text execution but not able to find a way how can I delete the downloaded file

5 Answers

The configuration option "trashAssetsBeforeRuns": true is by default true, so unless you have already changed it, that's not the answer you're looking for.

Please be aware that it only applies to cypress run (headless) mode, ref cypress.d.ts (confirmed with a simple test).

Also be aware of the downloadsFolder configuration option which defaults to /cypress/downloads. Outside the project, use the full path.


For cypress open the recipe local-download-spec.js gives you an example.

Test

import { deleteDownloadsFolder } from './utils'
...
beforeEach(deleteDownloadsFolder)
...

Utils

export const deleteDownloadsFolder = () => { const downloadsFolder = Cypress.config('downloadsFolder') cy.task('deleteFolder', downloadsFolder)
}

Task in /cypress/plugins/index.js

const { rmdir } = require('fs')
module.exports = (on, config) => { on('task', { deleteFolder(folderName) { console.log('deleting folder %s', folderName) return new Promise((resolve, reject) => { rmdir(folderName, { maxRetries: 10, recursive: true }, (err) => { if (err) { console.error(err) return reject(err) } resolve(null) }) }) }, })
}
1

You can add this configuration to cypress.json: "trashAssetsBeforeRuns": true

0

The quickest solution is to use cypress-delete-downloads-folder npm package. Just several lines added to your Cypress configuration files would allow cleaning the folder from your tests with one command:

cy.deleteDownloadsFolder()

you can try with cy.exce() Examplecy.exec("find . -name " + <downloadDirectory> + "file");cy.exec("cd " + <downloadDirectory> + ' ' + "&& rm -rf *");

@Rosen Mihaylov here's correct example, you just missed a few brackets and lines

Task in /cypress/plugins/index.js

 /// <reference types="cypress" /> /* eslint-disable no-console */ const { rmdir } = require('fs') /** * @type {Cypress.PluginConfig} */ module.exports = (on, config) => { // `on` is used to hook into various events Cypress emits // `config` is the resolved Cypress config // register utility tasks to read and parse Excel files on('task', { deleteFolder(folderName) { console.log('deleting folder %s', folderName) return new Promise((resolve, reject) => { rmdir(folderName, { maxRetries: 10, recursive: true }, (err) => { if (err) { console.error(err) return reject(err) } resolve(null) }) }) }, }) }
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like