question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

3.5.0: File upload doesn't work with DataTransfer()

See original GitHub issue

Current 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:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
yuki-93commented, Oct 29, 2019

Adding input[0].dispatchEvent(new Event('change', {bubbles: true})); as suggested in one comment worked for me. Thanks for your help 😃

0reactions
yuki-93commented, Oct 29, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found