layerssss / paste.js

read image/text data from clipboard (cross-browser)
http://layerssss.github.io/paste.js/
MIT License
463 stars 94 forks source link

Fake paste event for automatic testing? #39

Closed jmuheim closed 7 years ago

jmuheim commented 7 years ago

Hello

Is there a way to fake the paste event? I need to test my JavaScript code and don't know how to trigger the event.

Thank you.

layerssss commented 7 years ago

That doesn't sound always possible, what's your test driver for doing that?

layerssss commented 7 years ago

If you are satisfied with this one

$('.test')[0].dispatchEvent({
  clipboardData: {
    type: 'image/png',
    items: [{
      getAsFile: function () { /* return the blob of the image file */ }
    }]
  }
});

This is what we are expecting from Chrome's API.

jmuheim commented 7 years ago

I'm using Ruby on Rails with RSpec, Capybara, and PhantomJS. It's pretty cool, but for JavaScript testing it's rather limited. We'll see what I can do with the code you posted above... Thank you.