FitFju
FitFju

Reputation: 31

How to rename downloaded file in Cypress?

is there some way how to rename downloaded file in test?

Was looking for advice on google, but did not find any.

Current code to download load file:

    cy.get(':nth-child(2) > .btn').contains(domData.dwnldContractFiles).as('dwnldContractFiles').should('exist').should('be.visible').and('not.be.disabled')
    cy.get('@dwnldContractFiles').click()
    cy.verifyDownload(domData.allFiles, {timeout: 30000, interval: 3000})

Upvotes: 0

Views: 1483

Answers (1)

Valeriia
Valeriia

Reputation: 627

You can use cypress-downloadfile if you are not tied to any download mechanism. In order to install this package you need tinpm install cypress-downloadfile

Then add the following line to cypress/support/commands.js.

require('cypress-downloadfile/lib/downloadFileCommand');

Update your cypress.config.js file with:

const { defineConfig } = require('cypress')
const {downloadFile} = require('cypress-downloadfile/lib/addPlugin')

module.exports = defineConfig({
  // setupNodeEvents can be defined in either
  // the e2e or component configuration
  e2e: {
    setupNodeEvents(on, config) {
         on('task', {downloadFile})
      })
    }
  }
})

Finally, you can use it in your tests and set any name you want:

cy.downloadFile('https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg','downloads',chosenName)

Upvotes: 0

Related Questions