Skip to content

Instantly share code, notes, and snippets.

@klebervirgilio
Created March 13, 2016 22:09
Show Gist options
  • Save klebervirgilio/fcc1767391cdb310751b to your computer and use it in GitHub Desktop.
Save klebervirgilio/fcc1767391cdb310751b to your computer and use it in GitHub Desktop.
Rspec: Drag and Drop file upload
module IntegrationSpecHelper
def drop_files files, drop_area_id
js_script = "fileList = Array();"
files.count.times do |i|
# Generate a fake input selector
page.execute_script("if ($('#seleniumUpload#{i}').length == 0) { seleniumUpload#{i} = window.$('<input/>').attr({id: 'seleniumUpload#{i}', type:'file'}).appendTo('body'); }")
# Attach file to the fake input selector through Capybara
attach_file("seleniumUpload#{i}", files[i])
# Build up the fake js event
js_script = "#{js_script} fileList.push(seleniumUpload#{i}.get(0).files[0]);"
end
# Trigger the fake drop event
page.execute_script("#{js_script} e = $.Event('drop'); e.originalEvent = {dataTransfer : { files : fileList } }; $('##{drop_area_id}').trigger(e);")
end
end
describe "when user drop files", js: true do
before do
files = [ Rails.root + 'spec/support/pdffile/pdfTest1.pdf',
Rails.root + 'spec/support/pdffile/pdfTest2.pdf',
Rails.root + 'spec/support/pdffile/pdfTest3.pdf' ]
drop_files files, 'fileDropArea'
end
it "should ..." do
should have_content '...'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment