cypress-downloadfile
Version:
Cypress custom command to download files
70 lines (51 loc) • 2.03 kB
Markdown
[](https://badge.fury.io/js/cypress-downloadfile)
This is a Cypress custom file download command.
This repository is not maintained by the Cypress developers.
[](https://www.buymeacoffee.com/xav4)
Install the module.
```shell
npm install cypress-downloadfile
```
Add the following line to `cypress/support/commands.js`.
```javascript
require('cypress-downloadfile/lib/downloadFileCommand')
```
### For Cypress 10 and above
Add the following lines to `cypress.config.js`.
```javascript
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})
}
}
})
```
Add the following lines to `cypress/plugins/index.js`.
```javascript
const {downloadFile} = require('cypress-downloadfile/lib/addPlugin')
module.exports = (on, config) => {
on('task', {downloadFile})
}
```
If autocompletion does not work out of the box you can add the following line above your testfile
```javascript
/// <reference types="cypress-downloadfile"/>
```
```javascript
cy.downloadFile('https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg','mydownloads','example.jpg')
```
```javascript
cy.downloadFile('https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg','mydownloads','example.jpg','MyCustomAgentName')
```
Because request is deprecated we changed to cross-fetch. This works on Cypress 3.8.0 and upwards. If you notice problems please let me know