| Class | FormTestHelper::Form |
| In: |
lib/form_test_helper.rb
|
| Parent: | Object |
| tag | [R] | |
| xhr | [RW] |
# File lib/form_test_helper.rb, line 19
19: def initialize(tag, testcase)
20: @tag, @testcase = tag, testcase
21: end
Same as find_field_by_name but raises an exception if the field doesn‘t exist.
# File lib/form_test_helper.rb, line 101
101: def [](field_name)
102: find_field_by_name(field_name) || raise(FieldNotFoundError, "Field named '#{field_name}' not found in form.")
103: end
# File lib/form_test_helper.rb, line 116
116: def []=(field_name, value)
117: self[field_name].value = value
118: end
# File lib/form_test_helper.rb, line 46
46: def field_names
47: fields.collect {|field| field.name }
48: end
# File lib/form_test_helper.rb, line 50
50: def fields
51: # Input, textarea, select, and button are valid field tags. Name is a required attribute.
52: @fields ||= tag.select('input, textarea, select, button').reject {|field_tag| field_tag['name'].nil? }.group_by {|field_tag| field_tag['name'] }.collect do |name, field_tags|
53: case field_tags.first['type']
54: when 'submit'
55: FormTestHelper::Submit.new(field_tags)
56: when 'checkbox'
57: FormTestHelper::CheckBox.new(field_tags)
58: when 'hidden'
59: FormTestHelper::Hidden.new(field_tags)
60: when 'radio'
61: FormTestHelper::RadioButtonGroup.new(field_tags)
62: else
63: if field_tags.first.name == 'select'
64: if field_tags.first['multiple'] # The multiple attribute is set
65: FormTestHelper::SelectMultiple.new(field_tags)
66: else
67: FormTestHelper::Select.new(field_tags)
68: end
69: else
70: FormTestHelper::Field.new(field_tags)
71: end
72: end
73: end
74: end
# File lib/form_test_helper.rb, line 76
76: def fields_hash
77: @fields_hash ||= FieldsHash.new(ActionController::UrlEncodedPairParser.new(fields.collect {|field| [field.name, field] }).result)
78: end
# File lib/form_test_helper.rb, line 93
93: def find_field_by_name(field_name)
94: field_name = field_name.to_s.gsub(/\[\]$/, '') # Strip any trailing empty square brackets
95: matching_fields = self.fields.select {|field| field.name == field_name }
96: return nil if matching_fields.empty?
97: matching_fields.first
98: end
# File lib/form_test_helper.rb, line 105
105: def method_missing(method, *args)
106: method = method.to_s
107: if method.gsub!(/=$/, '')
108: self[method].value = *args
109: elsif fields_hash.has_key?(method)
110: fields_hash[method].proxy
111: else
112: self[method].proxy
113: end
114: end
# File lib/form_test_helper.rb, line 128
128: def request_method
129: hidden_method_field = self.find_field_by_name("_method")
130: if hidden_method_field # PUT and DELETE
131: hidden_method_field.value.to_sym
132: elsif tag["method"] && !tag["method"].blank? # POST and GET
133: tag["method"].to_sym
134: else # No method specified in form tags
135: :get
136: end
137: end
# File lib/form_test_helper.rb, line 120
120: def reset
121: fields.each {|field| field.reset }
122: end
Submits the form. Raises an exception if no submit button is present.
# File lib/form_test_helper.rb, line 35
35: def submit(opts={})
36: raise MissingSubmitError, "Submit button not found in form" unless tag.select('input[type="submit"], input[type="image"], button[type="submit"]').any?
37: @xhr = opts.delete(:xhr)
38: fields_hash.update(opts)
39: submit_without_clicking_button
40: end
If you submit the form with JavaScript
# File lib/form_test_helper.rb, line 24
24: def submit_without_clicking_button
25: 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
26: params = {}
27: 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
28:
29: # Convert arrays and hashes in param keys, since test processing doesn't do this automatically
30: params = ActionController::UrlEncodedPairParser.new(params).result
31: @testcase.make_request(request_method, path, params, self.uri, xhr)
32: end
# File lib/form_test_helper.rb, line 42
42: def uri
43: @testcase.instance_variable_get("@request").request_uri
44: 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
# File lib/form_test_helper.rb, line 89
89: def with_object(object_name)
90: yield self.send(object_name)
91: end