我试图模拟dropzone图片上传。我试图遵循这篇StackOverflow文章,但仍然无法正常工作。
How do you test uploading a file with capybara and Dropzone?
def drop_in_dropzone(file_path)
# Generate a fake input selector
page.execute_script <<-JS
fakeFileInput = window.$('<input/>').attr(
{id: 'fakeFileInput', type:'file'}
).appendTo('.dropzone-upload-button');
JS
# Attach the file to the fake input selector with Capybara
attach_file("fakeFileInput", file_path)
# Trigger the fake drop event
page.execute_script <<-JS
var fileList = [fakeFileInput.get(0).files[0]];
var e = jQuery.Event('drop', { dataTransfer : { files : fileList } });
$('.dropzone')[0].dropzone.listeners[0].events.drop(e);
JS
end
错误
$(...)[0].dropzone is undefined (Selenium::WebDriver::Error::JavascriptError)