#399 new
Jarl Friis

Webrat does not support multiple file upload

Reported by Jarl Friis | October 28th, 2010 @ 05:28 AM

Firefox, Chrome and HTML5 let you have a file field like this:

That will trigger the browser to select multiple files.
However Webrat does not support testing this feature.

To mske it work (on server side) the name must end with "[]" indicating an array value is expected.

I have made an ugly monkey patch (against 0.7.1) to make things work, here it is:
module Webrat class FileField def test_uploaded_file return "" if @original_value.blank?

      case Webrat.configuration.mode
      when :rails
        if Array === @original_value  ###THIS IS NEW
          @original_value.enum_for(:each_with_index).collect do |ov, i|
            if content_type[i]
              ActionController::TestUploadedFile.new(@original_value[i], content_type[i])
            else
              ActionController::TestUploadedFile.new(@original_value[i])
            end
          end
        else
          if content_type
            ActionController::TestUploadedFile.new(@original_value, content_type)
          else
            ActionController::TestUploadedFile.new(@original_value)
          end
        end
      end
    end
  end

  class Form
    protected

    def self.replace_param_value(params, oval, nval)
      output = Hash.new
      params.each do |key, value|
        case value
        when Hash
          value = replace_param_value(value, oval, nval)
        when Array
          value = value.map { |o| o == oval ? nval : ( o.is_a?(Hash) ? replace_param_value(o, oval, nval) : o) }
          value.flatten!(1) if Array === nval ###THIS IS NEW
        when oval
          value = nval
        end
        output[key] = value
      end
      output
    end
  end
end

Hope this will be part of webrat.

Comments and changes to this ticket

  • Chris Hapgood

    Chris Hapgood December 30th, 2010 @ 05:07 PM

    Beware that Rails (2.3.5 and perhaps others) appear to have a more fundamental problem with accepting file parameters in an array.

    For example, this results in severe ugliness in Rails:

    post gallery_attachments_path(galleries(:one).id), :attachment => [{:description => "The photo", :file => fixture_file_upload('a/plus.png', Mime::PNG)}]

    The hash representing the first (and only) array element is munged into a single string, probably somewhere in ActionController::TestProcess' parameter handling. Note that this works fine:

    post gallery_attachments_path(galleries(:one).id), :attachment => {:description => "The photo", :file => fixture_file_upload('a/plus.png', Mime::PNG)}

    So I would not be surprised if the bug reported on this ticket is actually a Rails bug.

  • Chris Hapgood

    Chris Hapgood May 13th, 2011 @ 01:13 PM

    After a recent upgrade to Rails 2.3.11, I confirm what Jarl notes: Rails behaves properly. I conclude that uploading a file into an array parameter was broken in 2.3.5 and is now fixed (in Integration testing).

    Furthermore, Webrat 0.7.2 seems to trigger the upload correctly.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Ruby Acceptance Testing for Web applications.

Shared Ticket Bins

People watching this ticket

Pages