How do i delete downloaded file in using cypress
Asked Answered
E

6

8

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

Eliseoelish answered 29/6, 2021 at 10:25 Comment(0)
S
23

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)
        })
      })
    },
  })
}
Sitarski answered 1/7, 2021 at 5:48 Comment(1)
I tried this code and it worked perfectly for me the first time. However I needed to also implement a check for the folder becuase if the folder does not exist this code will fail.Renaissance
B
4

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

Blouse answered 30/6, 2021 at 16:7 Comment(0)
M
3

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()
Microcosm answered 27/7, 2022 at 11:53 Comment(0)
M
0

@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)
                    })
                })
            },
        })
    }
Mccown answered 21/2, 2022 at 15:27 Comment(3)
It's the same answer as above.Geryon
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Walston
Year, it is. But I'm saying here was a few mistakes with missing brackets and line, such as: javascript on('task', { Mccown
B
0

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

Bueschel answered 22/2, 2022 at 6:37 Comment(0)
G
0

Add this in your config.js

on('task', {
  deleteFolder(folderName) {
    return new Promise((resolve, reject) => {
      rm(
        folderName,
        {
          maxRetries: 10,
          recursive: true
        },
        (err) => {
          if (err) {
            console.error(err)
          }
          resolve(null)
        }
      )
    })
  }
})

And then add this in your test:

beforeEach(() => {
  cy.task('deleteFolder', 'cypress//downloads//');
});

If specifically want to delete a file you can add filename after cypress//downloads//filename

Geese answered 28/5, 2024 at 13:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.