Class: WebUnit::Form

Inherits:
HtmlElem show all
Defined in:
lib/webunit/form.rb

Instance Attribute Summary collapse

Attributes inherited from HtmlElem

#array, #attrs, #children, #data, #tag

Instance Method Summary collapse

Methods inherited from HtmlElem

#append, #extract, #find, #has?, #inspect, #print, #readlink, #search

Constructor Details

#initialize(ah) ⇒ Form

Returns a new instance of Form.



15
16
17
18
19
20
21
22
23
# File 'lib/webunit/form.rb', line 15

def initialize( ah )
  super( 'form', ah )
  @parameters = []    # index by int
  @params = {}        # index by name
  @action = ah["name"]
  @action = ah["action"]
  @method = ah["method"] == nil ? "GET" : ah["method"]
  #print @action,":",method,"\n"
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



12
13
14
# File 'lib/webunit/form.rb', line 12

def action
  @action
end

#encodingObject

Returns the value of attribute encoding.



12
13
14
# File 'lib/webunit/form.rb', line 12

def encoding
  @encoding
end

#methodObject

Returns the value of attribute method.



12
13
14
# File 'lib/webunit/form.rb', line 12

def method
  @method
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/webunit/form.rb', line 12

def name
  @name
end

#parametersObject (readonly)

Returns the value of attribute parameters.



13
14
15
# File 'lib/webunit/form.rb', line 13

def parameters
  @parameters
end

#paramsObject (readonly)

Returns the value of attribute params.



13
14
15
# File 'lib/webunit/form.rb', line 13

def params
  @params
end

Instance Method Details

#add_param(param) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/webunit/form.rb', line 25

def add_param( param )
  @parameters.push param
  if @params.key?param.name then
    @params[param.name].update( param )
    param.update( @params[param.name] )
  else
    @params[param.name] = param
  end
end

#freezeObject



35
36
37
38
# File 'lib/webunit/form.rb', line 35

def freeze
  @parameters.freeze
  @params.freeze
end

#set_parameters(array) ⇒ Object

— Form#set_parameters( array )

fill up each fields, by index.


58
59
60
61
62
63
# File 'lib/webunit/form.rb', line 58

def set_parameters( array )
  array.each_index do |i|
    self.parameters[i].value = array[i]
  end
  self
end

#set_params(hash) ⇒ Object

— Form#set_params( hash )

fill up each fields, by name.


45
46
47
48
49
50
51
# File 'lib/webunit/form.rb', line 45

def set_params( hash )
  hash.each do |k,v|
    raise ArgumentError.new("wrong key: " + k) unless self.params[k]
    self.params[k].value = v
  end
  self
end

#store_test_data(fields, data, s = ',', button = nil) ⇒ Object

— Form#store_test_data(fields,data,s=‘,’,button=nil)

store data to backend ( Database, File, ... ).
input csv data to each fields, and submit.


71
72
73
74
75
76
# File 'lib/webunit/form.rb', line 71

def store_test_data( fields, data, s=',', button=nil )
  data.split( "\n" ).collect{ |r| r.strip.split( s ) }.each do |r|
    fields.each_index{ |i| @params[fields[i]].value = r[i] }
    self.submit( button )
  end
end

#submit(button = nil, x = nil, y = nil) ⇒ Object

— Form#submit(button=nil,x=nil,y=nil)

submit form, and return a Response.


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/webunit/form.rb', line 82

def submit( button=nil, x=nil, y=nil )
  data = []
  matched = nil
  multipart = (attrs['enctype'] == 'multipart/form-data')
  # make data from params

  @parameters.each do |p|
    puts "#{p.class}/#{p.name}/#{p.value}" if $DEBUG
    if p.class == InputSubmit || p.class == InputImage
      next unless p.name == button || p.value == button
      matched = p
      if p.class  == InputImage
        p.x = x
        p.y = y
      end
    end
    
    if multipart
      data << p.multipart_query_data(BOUNDARY)
      else
      data << p.query_data
    end
  end
  
  if multipart
    data = data.join('') + "--#{BOUNDARY}--\n"
  else
    data = data.compact.join('&')
  end

  Response::new.init_http( @action, @method, data, false, multipart)
end