Shivaprasad BM
Shivaprasad BM

Reputation: 93

How do I delete a downloaded file in a test using Cypress

I'm trying to delete a downloaded file before the test run, but am not able to find a way.

How can I delete the downloaded file?

Upvotes: 8

Views: 26906

Answers (6)

Niluka Monnankulama
Niluka Monnankulama

Reputation: 375

You can try with cy.exec():

Example:

cy.exec("find . -name " + <downloadDirectory> + "file");

cy.exec("cd " + <downloadDirectory> + ' ' + "&& rm -rf *");

Upvotes: 0

Mahnoor Khan
Mahnoor Khan

Reputation: 1

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

Upvotes: 0

Valentine Shi
Valentine Shi

Reputation: 7848

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()

Upvotes: 3

user9161752
user9161752

Reputation:

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)
        })
      })
    },
  })
}

Upvotes: 23

Sergei Goncharov
Sergei Goncharov

Reputation: 9

@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)
                    })
                })
            },
        })
    }

Upvotes: 0

Rosen Mihaylov
Rosen Mihaylov

Reputation: 1427

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

Upvotes: 4

Related Questions