You can actually use cy.writeFile() as below,
cy.request('https://dummy.url').then((response) => { cy.writeFile('cypress/fixtures/response.csv', response.import.resultsCsvFileId) })
You can refer the docs here - https://docs.cypress.io/api/commands/writefile.html#JSON
But, this overwrite the file every time. Here, is the workaround to append the required data to a CSV file,
Install npm package csvdata https://www.npmjs.com/package/csvdata
npm install --save-dev csvdata
In the cypress project -> plugins index.js file place the below code,
const csvdata = require('csvdata') on('task', { log(message) { csvdata.write('./logs.csv', message, { append: true, header: 'stats,numusers' }) return null } })
- Then from now on, all the
log task will get appended to the CSV file,
describe('My First Test', function() { it('Visits the Kitchen Sink', function() { cy.visit('https://example.cypress.io') cy.contains('type').then(($el) => { var data = "inService, 30"; cy.task('log', data) }) }) })
Reference: https://github.com/cypress-io/cypress/issues/1249
Update 1: The issue specified above is now resolved and append option is now available,
cy.writeFile('path/to/message.txt', 'Hello World', {flag: "a+"}))