abramenal / cypress-file-upload

File upload testing made easy
https://npm.im/cypress-file-upload
MIT License
498 stars 89 forks source link

[Bug]Upload image fail #244

Closed mingyixu closed 3 years ago

mingyixu commented 3 years ago

Current behavior:

I'm trying to upload an image to a file. This is my code cy.get('#pic').attachFile({ filePath: 'abc', mimeType: 'image/jpg', encoding: 'base64' }, { subjectType: 'drag-n-drop' });

on mac is success, but cann't upload on linux. its must use : cy.fixture("abc.jpg", 'binary') .then(Cypress.Blob.binaryStringToBlob) .then((fileContent) => { cy.get('#pic').attachFile({ fileContent, fileName: 'abc.jpg', mimeType: 'image/jpg', encoding: 'utf-8' }); }) this method not nice.

Desired behavior:

Steps to reproduce: (app code and test code)

Versions

abramenal commented 3 years ago

Hi @mingyixu Thanks for submitting the issue! The two code snippets you mention use different encodings. I think the correct one is binary which should automatically be recognized by the tool. Here are things you can try:

cy.get('#pic').attachFile('abc.jpg', { subjectType: 'drag-n-drop' });

// OR
cy.get('#pic').attachFile({ filePath: 'abc.jpg', mimeType: 'image/jpg', encoding: 'binary' }, { subjectType: 'drag-n-drop' });

If you have tried this and still see the issue, feel free to reopen this so we can investigate it further. Thanks!

abramenal commented 2 years ago

https://github.com/cypress-io/cypress/blob/dcefd77b052b2deec0b9ad0bd29cb16635f50d94/packages/server/lib/fixture.js#L124

According to cypress implementation the default encoding for images is base64. Hence, keeping it as default in this plugin as well. If you need a different one, you can use encoding property as I have described above