| 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 102
102: def [](field_name)
103: find_field_by_name(field_name) || raise(FieldNotFoundError, "Field named '#{field_name}' not found in form.")
104: end
# File lib/form_test_helper.rb, line 117
117: def []=(field_name, value)
118: self[field_name].value = value
119: end
# File lib/form_test_helper.rb, line 47
47: def field_names
48: fields.collect {|field| field.name }
49: end
# File lib/form_test_helper.rb, line 51
51: def fields
52: # Input, textarea, select, and button are valid field tags. Name is a required attribute.
53: @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|
54: case field_tags.first['type']
55: when 'submit'
56: FormTestHelper::Submit.new(field_tags)
57: when 'checkbox'
58: FormTestHelper::CheckBox.new(field_tags)
59: when 'hidden'
60: FormTestHelper::Hidden.new(field_tags)
61: when 'radio'
62: FormTestHelper::RadioButtonGroup.new(field_tags)
63: else
64: if field_tags.first.name == 'select'
65: if field_tags.first['multiple'] # The multiple attribute is set
66: FormTestHelper::SelectMultiple.new(field_tags)
67: else
68: FormTestHelper::Select.new(field_tags)
69: end
70: else
71: FormTestHelper::Field.new(field_tags)
72: end
73: end
74: end
75: end
# File lib/form_test_helper.rb, line 77
77: def fields_hash
78: @fields_hash ||= FieldsHash.new(CGIMethods::FormEncodedPairParser.new(fields.collect {|field| [field.name, field] }).result)
79: end
# File lib/form_test_helper.rb, line 94
94: def find_field_by_name(field_name)
95: field_name = field_name.to_s.gsub(/\[\]$/, '') # Strip any trailing empty square brackets
96: matching_fields = self.fields.select {|field| field.name == field_name }
97: return nil if matching_fields.empty?
98: matching_fields.first
99: end
# File lib/form_test_helper.rb, line 106
106: def method_missing(method, *args)
107: method = method.to_s
108: if method.gsub!(/=$/, '')
109: self[method].value = *args
110: elsif fields_hash.has_key?(method)
111: fields_hash[method].proxy
112: else
113: self[method].proxy
114: end
115: end
# File lib/form_test_helper.rb, line 129
129: def request_method
130: hidden_method_field = self.find_field_by_name("_method")
131: if hidden_method_field # PUT and DELETE
132: hidden_method_field.value.to_sym
133: elsif tag["method"] && !tag["method"].blank? # POST and GET
134: tag["method"].to_sym
135: else # No method specified in form tags
136: :get
137: end
138: end
# File lib/form_test_helper.rb, line 121
121: def reset
122: fields.each {|field| field.reset }
123: end
Submits the form. Raises an exception if no submit button is present.
# File lib/form_test_helper.rb, line 36
36: def submit(opts={})
37: raise MissingSubmitError, "Submit button not found in form" unless tag.select('input[type="submit"], input[type="image"], button[type="submit"]').any?
38: @xhr = opts.delete(:xhr)
39: fields_hash.update(opts)
40: submit_without_clicking_button
41: 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 = CGIMethods::FormEncodedPairParser.new(params).result
31:
32: @testcase.make_request(request_method, path, params, self.uri, xhr)
33: end
# File lib/form_test_helper.rb, line 43
43: def uri
44: @testcase.instance_variable_get("@request").request_uri
45: 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 90
90: def with_object(object_name)
91: yield self.send(object_name)
92: end