Class FormTestHelper::Form
In: lib/form_test_helper.rb
Parent: Object

Methods

Included Modules

TagProxy

Classes and Modules

Class FormTestHelper::Form::FieldNotFoundError
Class FormTestHelper::Form::MissingSubmitError

Constants

REMOTE_FORM_ONSUBMIT_ACTION_RGX = /new Ajax.Request\('([^']+)'/

Attributes

tag  [R] 

Public Class methods

[Source]

    # File lib/form_test_helper.rb, line 20
20:     def initialize(tag, testcase, options={})
21:       @tag, @testcase = tag, testcase, 
22:       @submit_value = options.delete(:submit_value)
23:       @xhr = options.delete(:xhr)
24:     end

Public Instance methods

Same as find_field_by_name but raises an exception if the field doesn‘t exist.

[Source]

     # File lib/form_test_helper.rb, line 124
124:     def [](field_name)
125:       find_field_by_name(field_name) || raise(FieldNotFoundError, "Field named '#{field_name}' not found in form.")
126:     end

[Source]

     # File lib/form_test_helper.rb, line 139
139:     def []=(field_name, value)
140:       self[field_name].value = value
141:     end

[Source]

     # File lib/form_test_helper.rb, line 147
147:     def action
148:       tag["action"]
149:     end

[Source]

    # File lib/form_test_helper.rb, line 66
66:     def field_names
67:       fields.collect {|field| field.name }
68:     end

[Source]

    # File lib/form_test_helper.rb, line 70
70:     def fields
71:       return @fields if @fields
72:       # Input, textarea, select, and button are valid field tags.  Name is a required attribute.
73:       fields = tag.select('input, textarea, select, button').reject{ |tag| tag['name'].nil? }
74:       @fields = fields.group_by {|field_tag| field_tag['name'] }.collect do |name, field_tags|
75:         case field_tags.first['type']
76:         when 'submit'
77:           field_tags.reject!{ |tag,*| tag['value'] != @submit_value } if @submit_value
78:           FormTestHelper::Submit.new(field_tags)
79:         when 'checkbox'
80:           FormTestHelper::CheckBox.new(field_tags)
81:         when 'hidden'
82:           FormTestHelper::Hidden.new(field_tags)
83:         when 'radio'
84:           FormTestHelper::RadioButtonGroup.new(field_tags)
85:         else
86:           if field_tags.first.name == 'select'
87:             if  field_tags.first['multiple'] # The multiple attribute is set
88:               FormTestHelper::SelectMultiple.new(field_tags)
89:             else
90:               FormTestHelper::Select.new(field_tags)
91:             end
92:           else
93:             FormTestHelper::Field.new(field_tags)
94:           end
95:         end
96:       end
97:     end

[Source]

     # File lib/form_test_helper.rb, line 99
 99:     def fields_hash
100:       @fields_hash ||= FieldsHash.new(ActionController::UrlEncodedPairParser.new(fields.collect {|field| [field.name, field] }).result)
101:     end

[Source]

     # File lib/form_test_helper.rb, line 116
116:     def find_field_by_name(field_name)
117:       field_name = field_name.to_s.gsub(/\[\]$/, '') # Strip any trailing empty square brackets
118:       matching_fields = self.fields.select {|field| field.name == field_name }
119:       return nil if matching_fields.empty?
120:       matching_fields.first
121:     end

[Source]

     # File lib/form_test_helper.rb, line 128
128:     def method_missing(method, *args)
129:       method = method.to_s
130:       if method.gsub!(/=$/, '')
131:         self[method].value = *args
132:       elsif fields_hash.has_key?(method)
133:         fields_hash[method].proxy
134:       else
135:         self[method].proxy
136:       end
137:     end

[Source]

     # File lib/form_test_helper.rb, line 151
151:     def request_method
152:       hidden_method_field = self.find_field_by_name("_method")
153:       if hidden_method_field # PUT and DELETE
154:         hidden_method_field.value.to_sym
155:       elsif tag["method"] && !tag["method"].blank? # POST and GET
156:         tag["method"].to_sym
157:       else # No method specified in form tags
158:         :get
159:       end
160:     end

[Source]

     # File lib/form_test_helper.rb, line 143
143:     def reset
144:       fields.each {|field| field.reset }
145:     end

Submits the form. Raises an exception if no submit button is present.

[Source]

    # File lib/form_test_helper.rb, line 50
50:     def submit(opts={})
51:       msg = "Submit button not found in form"
52:       selector = 'input[type="submit"], input[type="image"], button[type="submit"]'
53:       if @submit_value
54:         msg << " with a value of '#{@submit_value}'"
55:         selector.gsub!(/\]/, "][value=#{@submit_value}]")
56:       end
57:       raise MissingSubmitError, msg unless tag.select(selector).any?
58:       fields_hash.update(opts)
59:       submit_without_clicking_button
60:     end

If you submit the form with JavaScript

[Source]

    # File lib/form_test_helper.rb, line 31
31:     def submit_without_clicking_button
32:       if xhr?
33:         if tag.attributes['onsubmit'] =~ REMOTE_FORM_ONSUBMIT_ACTION_RGX
34:           path = $1
35:         else
36:           raise "No path found for the remote request"
37:         end
38:       else
39:         path = self.action.blank? ? self.uri : self.action # If no action attribute on form, it submits to the same URI where the form was displayed
40:       end
41:       params = {}
42:       fields.each {|field| params[field.name] = field.value unless field.value.nil? || field.value == [] || params[field.name] } # don't submit the nils, empty arrays, and fields already named
43:       
44:       # Convert arrays and hashes in param keys, since test processing doesn't do this automatically
45:       params = ActionController::UrlEncodedPairParser.new(params).result
46:       @testcase.make_request(request_method, path, params, self.uri, @xhr)
47:     end

[Source]

    # File lib/form_test_helper.rb, line 62
62:     def uri
63:       @testcase.instance_variable_get("@request").request_uri
64:     end

Accepts a block that can work with a single object (group of fields corresponding to a single ActiveRecord object)

Example:

  form.with_object(:book) do |book|
    book.name = 'Pickaxe'
    book.category = 'Programming'
    book.classic.check
  end

[Source]

     # File lib/form_test_helper.rb, line 112
112:     def with_object(object_name)
113:       yield self.send(object_name)
114:     end

[Source]

    # File lib/form_test_helper.rb, line 26
26:     def xhr?
27:       @xhr
28:     end

[Validate]