New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
can cy.writeFile write any type of document? #2029
Comments
To better understand this use case, why do you need to For instance, if you could write a giant I'm just trying to understand what you actually gain by writing the file after you receive the response. What is the content type of the server's response? My guess is that you need to set the |
Do you need it in |
@kutlaykural can you provide your code? The screenshot you posted is just headers, not the body. |
@jennifer-shehane @brian-mann var downloadUrl='/v1/enterprise/import/template/'+jobId;
cy.request({
method: 'GET',
url: downloadUrl
}).then((response) => {
cy.writeFile('/cypress/fixtures/excel.xlsx',response.body)
}) |
I am having a similar issue here, except that I am dealing with zip files. cy.log('Package URL is ' + downloadURL)
cy.request(downloadURL).then((response) => {
expect(response.status).to.be.equal(200)
cy.log(response.body.length)
cy.writeFile('cypress/work/downloadTest.zip', response.body, 'binary')
}) The logged response.body.length (50505) is of different value from the original file size on the server and the content-length header (53398) in the response. If I manually download the zip file using another web browser, the saved file can be opened correctly. I suspect cy.request is performing some post-processing on the response body, even though the response's content-type is set to application/zip. I have repeated the same test a number of times; the response.body.length is always the same length, so it is clearly not a network issue. |
Same issue here, the downloaded zip is corrupt. |
Yes I have come across this issue too. I am thinking of maybe using cypress to get the URL, but maybe using something else to hit that URL to download the file as a workaround for now. |
@nootn : Did you find any workaround for this? |
Got the same issue on downloading/saving PDF files. Can anyone help us with this? |
As a workaround, I found that I could use a custom plugin to perform the download. In my case, I was trying to download an Added via npm:
plugins/index.js: on('task', {
// args must be {url: "<url>, cookies: [{name: "cookie1", value: "value1"}, {name: "cookie2", value: "value2"}, ...]"}
parseXlsx(args) {
const cookieheader = args.cookies.map(e => e.name + "=" + e.value).join(";");
return new Promise((resolve, reject) => {
const r = request({url: args.url, encoding:null, headers: {Cookie: cookieheader}}, function(err, res, body) {
if (!res) {
return reject(new Error("No response"));
}
if (res.statusCode !== 200) {
return reject(new Error("Bad status code: " + res.statusCode));
}
const sheet = xlsx.parse(body);
console.log(JSON.stringify(sheet));
resolve(sheet);
});
});
}
}); support/commands.js: Cypress.Commands.add("parseXlsx", (url) => {
return cy.getCookies().then(cookies => {
return cy.task('parseXlsx', {url: url, cookies: cookies });
});
}); In my test file: // Call parseXlsx and verify that the export is as expected
cy.parseXlsx(exportUrl).should(data => {
expect(data).to.have.xlsSheetName("Payments");
expect(data).to.have.xlsSheetRowCell(0,0,0,'Reference #');
}); |
@WesleySSmith thank you very much, this idea helped me to create a workaround. Maybe I put it here, just in a case someone needs it, too: plugins/index.js: const request = require('request');
const fs = require('fs');
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
on('task', {
downloadPdf(args) {
const directory = args.directory;
const cookieHeader = args.cookies.map(e => e.name + '=' + e.value).join(';');
return new Promise((resolve, reject) => {
request({url: args.url, encoding: null, headers: {Cookie: cookieHeader}}, function(err, res, body) {
if (!res) {
return reject(new Error('No response'));
}
if (res.statusCode !== 200) {
return reject(new Error('Bad status code: ' + res.statusCode));
}
const contentDisposition = res.headers['content-disposition'];
if (!contentDisposition || contentDisposition.indexOf('inline') === -1) {
return reject(
new Error('Broken response: does not contain content-disposition of inline file type')
);
}
const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
const matches = filenameRegex.exec(contentDisposition);
if (matches == null || !matches[1]) {
return reject(new Error('Broken response: does not contain filename'));
}
const fileName = matches[1].replace(/['"]/g, '') + '.pdf';
fs.writeFileSync(directory + fileName, body);
resolve(body);
});
});
}
});
}; support/commands.js: Cypress.Commands.add('downloadPdf', (url, directory) => {
return cy.getCookies().then(cookies => {
return cy.task('downloadPdf', {url: url, directory: directory, cookies: cookies });
});
}); In my test file: cy.downloadPdf(pdfUrl, 'temp/'); My Also, I reported this issue in a separated task: #3576 |
Thanks roma-glushko! Saved my day. |
The code for this is done in cypress-io/cypress#7382, but has yet to be released. |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
Current behavior:
After a download attempt an excel file with
cy.request()
command,response.body
can be saved by usingcy.writeFile
command with any filename and extension (like 'text.xlsx') but file cannot be open.Desired behavior:
Any type of file should be saved with any extension and can be opened properly.
Versions
Cypress: 3.0.1
Operating system: Mac
Browser : Electron 59
The text was updated successfully, but these errors were encountered: