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) }) }) }, }) }
1You can add this configuration to cypress.json: "trashAssetsBeforeRuns": true
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() Example cy.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) }) }) }, }) }
3ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobXFpZ2uGcYGOoaawZZSkeqp5w56jnqyVYrGww82lppqclZl6p7XLnmSipl2qwKq6xmaasqiimsC0