Class: Capybara::Driver::RackTest::Form

Inherits:
Node
  • Object
show all
Defined in:
lib/capybara/driver/rack_test_driver.rb

Defined Under Namespace

Classes: NilUploadedFile

Instance Attribute Summary

Attributes inherited from Node

#driver, #native

Instance Method Summary collapse

Methods inherited from Node

#[], #checked?, #click, #find, #path, #select_option, #selected?, #set, #tag_name, #text, #unselect_option, #value, #visible?

Methods inherited from Node

#[], #checked?, #click, #drag_to, #initialize, #inspect, #path, #select_option, #selected?, #set, #tag_name, #text, #trigger, #unselect_option, #value, #visible?

Constructor Details

This class inherits a constructor from Capybara::Driver::Node

Instance Method Details

#multipart?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/capybara/driver/rack_test_driver.rb', line 173

def multipart?
  self[:enctype] == "multipart/form-data"
end

#params(button) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/capybara/driver/rack_test_driver.rb', line 123

def params(button)
  params = {}

  native.xpath("(.//input|.//select|.//textarea)[not(@disabled)]").map do |field|
    case field.name
    when 'input'
      if %w(radio checkbox).include? field['type']
        merge_param!(params, field['name'].to_s, field['value'].to_s) if field['checked']
      elsif %w(submit image).include? field['type']
        # TO DO identify the click button here (in document order, rather
        # than leaving until the end of the params)
      elsif field['type'] =='file'
        if multipart?
          file = \
            if (value = field['value']).to_s.empty?
              NilUploadedFile.new
            else
              content_type = MIME::Types.type_for(value).first.to_s
              Rack::Test::UploadedFile.new(value, content_type)
            end
          merge_param!(params, field['name'].to_s, file)
        else
          merge_param!(params, field['name'].to_s, File.basename(field['value'].to_s))
        end
      else
        merge_param!(params, field['name'].to_s, field['value'].to_s)
      end
    when 'select'
      if field['multiple'] == 'multiple'
        options = field.xpath(".//option[@selected]")
        options.each do |option|
          merge_param!(params, field['name'].to_s, (option['value'] || option.text).to_s)
        end
      else
        option = field.xpath(".//option[@selected]").first
        option ||= field.xpath('.//option').first
        merge_param!(params, field['name'].to_s, (option['value'] || option.text).to_s) if option
      end
    when 'textarea'
      merge_param!(params, field['name'].to_s, field.text.to_s)
    end
  end
  merge_param!(params, button[:name], button[:value] || "") if button[:name]
  params
end

#submit(button) ⇒ Object



169
170
171
# File 'lib/capybara/driver/rack_test_driver.rb', line 169

def submit(button)
  driver.submit(method, native['action'].to_s, params(button))
end