简体   繁体   中英

How to test multiple file upload using Cucumber/Capybara?

I could test upload of one file

attach_file 'photo', File.join(Rails.root, 'public', 'uploads', 'test.png')

But what if I have a file field with multiple atribute? How can I test multiple files upload using Cucumber/Capybara?

HTML:

<input id="fileupload" class="photo-uploader" type="file" multiple="" name="images">

Capybara:

page.attach_file "images", ['path to file1.jpg', 'path to file2.jpg', 'path to file3.jpg']
  1. Look for the name attribute of the <input> with type="file" and add it as the first parameter.
  2. Add the path to the files as the second parameter . Refer to the statement given above. Do not use relative paths for uploading the file.

It seems it is currently not possible with Capybara:

https://github.com/jnicklas/capybara/issues/37

Before testing multiple file uploads you should ask yourself, how you're going to store several photos in one model. One possible solution for this would be:

  class Post < ActiveRecord::Base
    has_many :photos
  end

  class Photo < ActiveRecord::Base
    belongs_to :post
    # if you're using Carrierwave
    mount_uploader :photo, PhotoUploader 
  end

After that you could realize that there's no difference between testing one or many files upload.

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