如何将 phantomjs uploadFile 与 Capybara 和 poltergeist 一起使用?

How do I use phantomjs uploadFile with Capybara and poltergeist?

我正在尝试在我的规范中附加一个文件,但是水豚 attach_file 方法在 poltergeist 中对我不起作用(它 returns 正确但没有附加任何内容)。它在 Selenium 中确实有效,但出于其他原因我想使用 poltergeist。我知道 phantomjs uploadFile 方法 (http://phantomjs.org/api/webpage/method/upload-file.html) 模拟用户与文件对话框的交互,这是我认为我需要做的。虽然在我的 Capybara 规范中我不知道如何使用它。

我正在尝试使用

def drop_files(files, css_selector)
  js_script = 'fileList = Array(); '
  files.count.times do |index|
    # Generate a fake input selector
    page.execute_script("if ($('#seleniumUpload#{index}').length == 0) { " \
                        "seleniumUpload#{index} = window.$('<input/>')" \
                        ".attr({id: 'seleniumUpload#{index}', type:'file'})" \
                        ".appendTo('body'); }")

    # Attach file to the fake input selector through Capybara
    attach_file("seleniumUpload#{index}", files[index], visible: false)
    # Build up the fake js event
    #
    js_script << "fileList.push(seleniumUpload#{index}.get(0).files[0]); "
  end

  js_script << "e = $.Event('drop'); "
  js_script << "e.dataTransfer = { files : fileList }; "
  js_script << "$('#{css_selector}').trigger(e);"

  # Trigger the fake drop event
  page.execute_script(js_script)
end

来自 https://github.com/teampoltergeist/poltergeist/issues/342 并在 selenium 中工作。发帖的用户说他已经在 poltergeist 中工作了。

原来文件上传目前在 Phantomjs 2.0 上被破坏了。我降级了 v1.9.8,attach_file 现在可以使用了。