-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
reading binary file from fixture differs from drop event file #1558
Comments
apart from that i kinda think it's an encoding issue and going over the options cypress has of reading fixtures it is kinda stunning to think they can read a file (binary) and represent it in a text string.. how the f* is that binary? tldr: ow and for that matter: how is base64 a character encoding, how is hex? |
Have you tried using |
No i didn't but, i did it right now: cy.readFile('cypress/fixtures/test.xlsx', 'binary').then((excel) {
}) throws an 'Error: the string "UTF-8 encode: second char code 0xe94d at index 434 in surrogate pair out of range" was thrown, throw an Error :)' then again: seeing the 'encoding's' in the documentation of readfile being the same as those of fixture, i'm guessing the same internal method is used. I have been testing a bit with the 'hex' option. Which looks like the only way to accually read the content of a binary file. I create a File object like this: cy.fixture('test.xlsx', 'hex').then((excelHex) => {
const excelBytes = hexStringToByte(excelHex);
//create a File object
const file = new File([excelBytes], 'test.xlsx', {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
//and make the drop
cy.get('canvas').trigger('drop', {
dataTransfer: {
files: [file],
},
});
}); |
I looked into how we handle |
I run into a related issue when trying upload a shapefile. For uploads I am using cypress-file-upload.
|
the bad thing is not in cypress but in the (very) crappy encoding and file read functions in nodejs itself. Nodejs fucked that up, and now it's a cascading failure... |
There are probably two ways to handle it that come to mind: cy.fixture(fname).then(Cypress.Blob.base64StringToBlob).then(blob => {
const file = new File([blob], fname, {type}); And: cy.fixture(fname, 'binary').then(content => {
content = Uint8Array.from(content, x => x.charCodeAt(0))
const file = new File([content], fname, {type}); I have a repository (branch) you can use to see it in action. There I was investigating different ways to upload a binary file using a traditional form (no AJAX, no React, no nothing). And indeed I was thinking if Cypress should just say: " |
Why does Cypress force an encoding? It seems to be impossible to load the raw data of a fixture. I've tried methods from abramenal/cypress-file-upload#70, from this issue, and from various linked issues. But no matter what I try, either Cypress corrupts the data or throws an exception because of an encoding failure. |
Dear Cypress Team, this issue is still present and affects everyone who works with binary files in their application. This is a pretty common use case, for example when users can upload an image, etc. The issue could easily be fixed by setting the encoding passed to For all other frustrated users: if you want to upload a binary file using Cypress and the let files = [];
filenames.map(filename => {
cy.readFile('cypress/fixtures/' + filename, 'base64').then(contents => {
files.push({
fileContent: Cypress.Blob.base64StringToBlob(contents),
fileName: filename,
encoding: 'binary',
mimeType: 'application/octet-stream'
});
});
});
cy.get('input[type=file]')
.eq(0)
.attachFile(files); |
The code for this is done in cypress-io/cypress#18534, but has yet to be released. |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
Is this a Feature or Bug?
Bug
Current behavior:
When i get a binary file from a fixture and want to create a
File()
object (see: https://developer.mozilla.org/en-US/docs/Web/API/File) i get a wrong result (Some bad bytes)When i drop a file in my application, and get the
event.dataTransfer.Files[0]
it is (slightly) differentDesired behavior:
I want an example how to convert a binary fixture to a
File()
object or a bugfix to make sure it is read correctlyHow to reproduce:
I use a
xlsx
file (which iszip
), add it too my fixtures and read it as i should.Then catch the drop event of the same file and compare it.
Test code:
the normal dropcode:
https://codepen.io/anon/pen/GxzmqB
the test:
Testresults (with my file):
running the codepen + dropping manually:
80 75 3 4 20 0 6 0 8 0 0 0 33 0 113 14 57 43 112 1 0 0 160 5 0 0 19 0 219 1 91 67 111 110 116 101 110 116 95 84 121 112 101 115 93 46 120 109 108 32
running the codepen + 'dropping' with cypress:
80 75 3 4 20 0 6 0 8 0 0 0 33 0 113 14 57 43 112 1 0 0 194 160 5 0 0 19 0 195 155 1 91 67 111 110 116 101 110 116 95 84 121 112 101 115 93 46 120 109
The text was updated successfully, but these errors were encountered: