3.5.0: File upload doesn't work with DataTransfer()
See original GitHub issueCurrent behavior:
I have a feature in my app where the user should upload an file as thier user image. Within my cypress test I faking the file upload dialog with the DataTransfer shown below. With 3.4.1 this works fine, but 3.5.0 looks to break this behavior. I investigated the the file is written into the input element, when I log this with console.error. But in my app, the file change handler is not fired. So something in between is going wrong.
Maybe there is also a much better for mocking file uploads.
Thanks for you help 😃
cy.get('.toolbar').within(() => {
cy.fixture('picture.jpg', 'binary')
.then((binary) => Cypress.Blob.binaryStringToBlob(binary))
.then((blob) => {
cy.get('input[type=file]').then((input) => {
const dataTransfer = new DataTransfer();
dataTransfer.items.add(new File([blob], 'me.jpg', {type: 'image/jpeg'}));
input[0].files = dataTransfer.files;
});
});
cy.get('.btn').contains('Speichern').should('be.visible').click();
});
Desired behavior:
Blob should correctly written to input element and button becomes clickable.
Steps to reproduce: (app code and test code)
see above
Versions
cypress@3.5.0 (Version 3.4.1 works fine)
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
File upload (Drag and Drop) not working - Stack Overflow
I tried to implement a drag and drop functionality to my site to upload files to the server. But it's not working, this...
Read more >DataTransfer.files - Web APIs - MDN Web Docs
The files property of DataTransfer objects is a list of the files in the drag operation. ... Want to fix the problem yourself?...
Read more >Building an HTML5 Drag & Drop File Uploader Using Sinatra ...
There is one caveat to this approach: there's currently no good way to perform your XHR file upload as a multipart form post....
Read more >Custom Query (3991 matches) - FileZilla
Custom Query (3991 matches) ; #906 · Error: Could not retrieve directory listing ; #907 · Problem renaming files ; #908 · Problem...
Read more >6.10 Drag and drop - HTML Standard - WhatWG
The event handler typically needs to check that it's not a text selection that is being dragged, and then needs to store data...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Adding
input[0].dispatchEvent(new Event('change', {bubbles: true}));
as suggested in one comment worked for me. Thanks for your help 😃The file change event would validates the image (size, type and dimensions) and than enables the submit button, to be clickable. So the click is necessary. In another test, I issue the ajax calls directly, which works fine.