简体   繁体   中英

Testing jquery-file-upload with capybara/cucumber

I'm trying to test jquery-file-upload plugin with the autoUpload option enabled using capybara/cucumber. The issue appears to be related to the autoUpload options, since uploading in the test environment works when there is no autoUpload option set. I'm uploading with the attach_file capybara method. The method triggers the change event listener, but apparently not much else. It never hits the create action for the attachment, and when I try to trigger the submit event listener manually, the params[:attachment] in the create action is empty and the attachment can't be created.

The way it's set up currently is this:

  • I have 3 models, a Project model, an Attachments model and an Issue model
  • There are 2 forms on the index page for Issues - one form that deals with submitting a new attachment and another that deals with submitting new issues
  • Whenever an Attachment create action is triggered, the jquery-file-upload fills out a hidden field via javascript with data needed to add the Attachment to the relevant issue - this is done with the fileuploadcompleted callback from jquery-file-upload

This way of uplading works whenever I try to upload a file manually, but fails in the test environment. I've tried adding a hidden submit button to the attachment form and when I press that button via capybara it triggers the create action but the params is once again empty. I'm running out of ideas about what to do, so if anybody had similar problems, I'd be grateful if they could help me out.

Here's some relevant code if that can help:

jquery-file-upload initalization and attachment form

Gems used:
Rails 3.2.12
Capybara 2.1.0
Capybara-webkit 1.0.0
Jquery-fileupload-rails 0.3.0 - also tested with 0.4.1, same results

If you need any more code or any other information, let me know, I can edit this post immediately.

Thanks!

I had similar problems, but i found that:

  1. Capybara + Webdriver expects platform-specific path separator (on Windows it's only '\\' regardless Windows supports '/' , on *nix - only '/' etc) and Capybara does nothing with it
  2. Capybara + WebDriver expects full path
  3. You can't attach file to hidden file input (eg when you use JQuery FileUpload plugin).

I use selector finding hidden elements and set path to file with platform-specific separators:

path = File.expand_path(rel_path).gsub('/', File::ALT_SEPARATOR || File::SEPARATOR)
find(:file_field, input_id, visible: false).set(path)

In my case it works with next versions of gems:

gem "jquery-fileupload-rails", "0.4.1"

gem 'capybara', '2.2.1'
gem 'selenium-webdriver', '2.41.0'

If anyone comes across this old question and is struggling to test jquery-fileupload with Capybara and Poltergeist (the other answer is about selenium), try upgrading PhantomJS to 2.1.0 or higher.

I was having major headaches getting it to work; turns out that the source of the problem is a known issue in PhantomJS 2.0. Upgrading to 2.1.1 fixed it.

(If you're using homebrew you can update PhantomJS by running brew update then brew upgrade phantomjs )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM