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.

Drag & Drop upload

See original GitHub issue

We are still looking for an option to test Drag & Drop file uploads. Is there anything in the plans about this?

Would be nice if I could drop a fixture onto the selected subject. Something like this maybe:

// fixture reference according to: 
// https://docs.cypress.io/api/commands/fixture.html#Shortcuts
cy.get('.dnd-upload-area').dropFile('fixture:image.jpg');

Thanks in advance 😊

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:40 (10 by maintainers)

github_iconTop GitHub Comments

33reactions
jennifer-shehanecommented, Nov 8, 2017

@egucciar You have to look at the way the particular drag and drop you want to test is currently working. The way I do this is, within the Sources panel of the DevTools, you set some Event Listener Breakpoints for Drag/drop, then you use the drag and drop normally in your app by dragging an image in. Then you can see exactly what these events look like and the data that it uses in your application.

Set a global Event Listener Breakpoint screen shot 2017-11-08 at 3 01 35 pm

The dataTransfer object from me dragging in a photo manually to their site screen shot 2017-11-08 at 3 04 31 pm

Doing this with the demo page of angular-file-upload, I can see that a dragover, drop and dragleave are all triggered (you’ll need to select and deselect them one by one as you set the breakpoint to inspect each one.

From this, I can see that they have code around more data than my implementation above cared about in their dragover and drop event. They’re expecting a types array in the dataTransfer object for example. And in order to get the filename to show up in the table, they have more data in their files array. The test code below should help get you started. I’m closing this issue.

describe('Drag and Drop', function() {
  const fileName = "foobar.jpg"
  
  beforeEach(function() {
    this.dropEvent = {
      dataTransfer: {
        files: [{
          name: fileName,
          size: 32796,
          type: "image/jpeg",
          lastModified: 1510154936000,
          webkitRelativePath: ""
        }],
        types: ["Files"]
      }
    }
  })
  
  it('drag and drop upload', function(){
    cy.visit('http://nervgh.github.io/pages/angular-file-upload/examples/simple/')
    cy.get('.my-drop-zone').first()
      .trigger('dragover', this.dropEvent)
      .should('have.class', 'nv-file-over')
      .trigger('drop', this.dropEvent)

    cy.get('table').contains(fileName)
  })
})
24reactions
andygockcommented, Jun 10, 2018

I’ve actually found a solution to my previous question, referring to this comment:

To drag and drop upload file myfile.csv from fixtures/

Cypress.Commands.add('upload_file', (selector, fileUrl, type = '') => {
  return cy.fixture(fileUrl, 'base64')
    .then(Cypress.Blob.base64StringToBlob)
    .then(blob => {
      const nameSegments = fileUrl.split('/')
      const name = nameSegments[nameSegments.length - 1]
      const testFile = new File([blob], name, { type })
      const event = { dataTransfer: { files: [testFile] } }
      return cy.get(selector).trigger('drop', event)
    })
});

describe("drag and drop upload", () => {
  it("uploads file from filesystem", () => {
    cy.upload_file('.drop', 'myfile.csv');
  });
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

File drag and drop - Web APIs | MDN
The main steps to drag and drop are to define a drop zone (i.e. a target element for the file drop) and to...
Read more >
How To Make A Drag-and-Drop File Uploader With Vanilla ...
Drag -and-drop image uploader in action A demonstration of a web page in which you can upload images via drag and drop, preview...
Read more >
Drag and Drop File Uploading - CSS-Tricks
I wanted to enrich the feed import experience by making allowing for drag and drop file upload alongside the traditional file input.
Read more >
Drag and Drop Example - Uppy
Here you'll see a demo of how you might set up Drag and Drop with Uppy. autoProceed is on. Drop here or browse....
Read more >
Creating a drag drop upload area | by Kim T - Medium
Here we will look at a quick way to implement drag/drop uploads without any external libraries/code. First off we need an area in...
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