Class: Mechanize::Form
- Inherits:
-
Object
- Object
- Mechanize::Form
- Defined in:
- lib/mechanize/form.rb,
lib/mechanize/inspect.rb,
lib/mechanize/form/field.rb,
lib/mechanize/form/button.rb,
lib/mechanize/form/option.rb,
lib/mechanize/monkey_patch.rb,
lib/mechanize/form/check_box.rb,
lib/mechanize/form/file_upload.rb,
lib/mechanize/form/select_list.rb,
lib/mechanize/form/image_button.rb,
lib/mechanize/form/radio_button.rb,
lib/mechanize/form/multi_select_list.rb
Overview
Synopsis
This class encapsulates a form parsed out of an HTML page. Each type of input fields available in a form can be accessed through this object.
Example
Find a form and print out its fields
form = page.forms.first # => Mechanize::Form
form.fields.each { |f| puts f.name }
Set the input field ‘name’ to “Aaron”
form['name'] = 'Aaron'
puts form['name']
Defined Under Namespace
Classes: Button, CheckBox, Field, FileUpload, ImageButton, MultiSelectList, Option, RadioButton, SelectList
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#buttons ⇒ Object
readonly
Returns the value of attribute buttons.
-
#checkboxes ⇒ Object
readonly
Returns the value of attribute checkboxes.
-
#enctype ⇒ Object
Returns the value of attribute enctype.
-
#fields ⇒ Object
(also: #elements)
readonly
Returns the value of attribute fields.
-
#file_uploads ⇒ Object
readonly
Returns the value of attribute file_uploads.
-
#form_node ⇒ Object
(also: #node)
readonly
Returns the value of attribute form_node.
-
#method ⇒ Object
Returns the value of attribute method.
-
#name ⇒ Object
Returns the value of attribute name.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#radiobuttons ⇒ Object
readonly
Returns the value of attribute radiobuttons.
Instance Method Summary collapse
-
#[](field_name) ⇒ Object
Fetch the value of the first input field with the name passed in ==Example Fetch the value set in the input field ‘name’ puts form.
-
#[]=(field_name, value) ⇒ Object
Set the value of the first input field with the name passed in ==Example Set the value in the input field ‘name’ to “Aaron” form = ‘Aaron’.
-
#add_button_to_query(button) ⇒ Object
This method adds a button to the query.
-
#add_field!(field_name, value = nil) ⇒ Object
Add a field with
field_name
andvalue
. -
#attribute_class ⇒ Object
Returns class attribute (<form class=“***”> of ***).
-
#attribute_id ⇒ Object
Returns id attribute (<form id=“***”> of ***).
-
#build_query(buttons = []) ⇒ Object
This method builds an array of arrays that represent the query parameters to be used with this form.
-
#click_button(button = buttons.first) ⇒ Object
Submit form using
button
. -
#delete_field!(field_name) ⇒ Object
Removes all fields with name
field_name
. -
#has_field?(field_name) ⇒ Boolean
(also: #has_key?)
Returns whether or not the form contains a field with
field_name
. - #has_value?(value) ⇒ Boolean
-
#initialize(node, mech = nil, page = nil) ⇒ Form
constructor
A new instance of Form.
- #keys ⇒ Object
-
#method_missing(id, *args) ⇒ Object
Treat form fields like accessors.
- #pretty_print(q) ⇒ Object
-
#request_data ⇒ Object
This method calculates the request data to be sent back to the server for this form, depending on if this is a regular post, get, or a multi-part post,.
-
#set_fields(fields = {}) ⇒ Object
This method sets multiple fields on the form.
-
#submit(button = nil, headers = {}) ⇒ Object
Submit this form with the button passed in.
- #values ⇒ Object
Constructor Details
#initialize(node, mech = nil, page = nil) ⇒ Form
Returns a new instance of Form.
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mechanize/form.rb', line 36 def initialize(node, mech=nil, page=nil) @enctype = node['enctype'] || 'application/x-www-form-urlencoded' @form_node = node @action = Util.html_unescape(node['action']) @method = (node['method'] || 'GET').upcase @name = node['name'] @clicked_buttons = [] @page = page @mech = mech parse end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args) ⇒ Object
Treat form fields like accessors.
124 125 126 127 128 129 130 131 |
# File 'lib/mechanize/form.rb', line 124 def method_missing(id,*args) method = id.to_s.gsub(/=$/, '') if field_with(:name => method) return field_with(:name => method).value if args.empty? return field_with(:name => method).value = args[0] end super end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
24 25 26 |
# File 'lib/mechanize/form.rb', line 24 def action @action end |
#buttons ⇒ Object (readonly)
Returns the value of attribute buttons.
26 27 28 |
# File 'lib/mechanize/form.rb', line 26 def @buttons end |
#checkboxes ⇒ Object (readonly)
Returns the value of attribute checkboxes.
26 27 28 |
# File 'lib/mechanize/form.rb', line 26 def checkboxes @checkboxes end |
#enctype ⇒ Object
Returns the value of attribute enctype.
27 28 29 |
# File 'lib/mechanize/form.rb', line 27 def enctype @enctype end |
#fields ⇒ Object (readonly) Also known as: elements
Returns the value of attribute fields.
26 27 28 |
# File 'lib/mechanize/form.rb', line 26 def fields @fields end |
#file_uploads ⇒ Object (readonly)
Returns the value of attribute file_uploads.
26 27 28 |
# File 'lib/mechanize/form.rb', line 26 def file_uploads @file_uploads end |
#form_node ⇒ Object (readonly) Also known as: node
Returns the value of attribute form_node.
31 32 33 |
# File 'lib/mechanize/form.rb', line 31 def form_node @form_node end |
#method ⇒ Object
Returns the value of attribute method.
24 25 26 |
# File 'lib/mechanize/form.rb', line 24 def method @method end |
#name ⇒ Object
Returns the value of attribute name.
24 25 26 |
# File 'lib/mechanize/form.rb', line 24 def name @name end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
34 35 36 |
# File 'lib/mechanize/form.rb', line 34 def page @page end |
#radiobuttons ⇒ Object (readonly)
Returns the value of attribute radiobuttons.
26 27 28 |
# File 'lib/mechanize/form.rb', line 26 def @radiobuttons end |
Instance Method Details
#[](field_name) ⇒ Object
Fetch the value of the first input field with the name passed in
Example
Fetch the value set in the input field ‘name’
puts form['name']
105 106 107 108 |
# File 'lib/mechanize/form.rb', line 105 def [](field_name) f = field_with(:name => field_name) f && f.value end |
#[]=(field_name, value) ⇒ Object
Set the value of the first input field with the name passed in
Example
Set the value in the input field ‘name’ to “Aaron”
form['name'] = 'Aaron'
114 115 116 117 118 119 120 121 |
# File 'lib/mechanize/form.rb', line 114 def []=(field_name, value) f = field_with(:name => field_name) if f.nil? add_field!(field_name, value) else f.value = value end end |
#add_button_to_query(button) ⇒ Object
This method adds a button to the query. If the form needs to be submitted with multiple buttons, pass each button to this method.
211 212 213 |
# File 'lib/mechanize/form.rb', line 211 def () @clicked_buttons << end |
#add_field!(field_name, value = nil) ⇒ Object
Add a field with field_name
and value
70 71 72 |
# File 'lib/mechanize/form.rb', line 70 def add_field!(field_name, value = nil) fields << Field.new(field_name, value) end |
#attribute_class ⇒ Object
Returns class attribute (<form class=“***”> of ***). If no class, returns nil.
65 |
# File 'lib/mechanize/form.rb', line 65 def attribute_class; form_node['class']; end |
#attribute_id ⇒ Object
Returns id attribute (<form id=“***”> of ***). If no id, returns nil.
67 |
# File 'lib/mechanize/form.rb', line 67 def attribute_id; form_node['id']; end |
#build_query(buttons = []) ⇒ Object
This method builds an array of arrays that represent the query parameters to be used with this form. The return value can then be used to create a query string for this form.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/mechanize/form.rb', line 167 def build_query( = []) query = [] fields().each do |f| qval = proc_query(f) query.push(*qval) end checkboxes().each do |f| if f.checked qval = proc_query(f) query.push(*qval) end end radio_groups = {} ().each do |f| fname = from_native_charset(f.name) radio_groups[fname] ||= [] radio_groups[fname] << f end # take one radio button from each group radio_groups.each_value do |g| checked = g.select {|f| f.checked} if checked.size == 1 f = checked.first qval = proc_query(f) query.push(*qval) elsif checked.size > 1 raise "multiple radiobuttons are checked in the same group!" end end @clicked_buttons.each { |b| qval = proc_query(b) query.push(*qval) } query end |
#click_button(button = buttons.first) ⇒ Object
Submit form using button
. Defaults to the first button.
140 141 142 |
# File 'lib/mechanize/form.rb', line 140 def ( = .first) submit() end |
#delete_field!(field_name) ⇒ Object
Removes all fields with name field_name
.
235 236 237 |
# File 'lib/mechanize/form.rb', line 235 def delete_field!(field_name) @fields.delete_if{ |f| f.name == field_name} end |
#has_field?(field_name) ⇒ Boolean Also known as: has_key?
Returns whether or not the form contains a field with field_name
50 51 52 |
# File 'lib/mechanize/form.rb', line 50 def has_field?(field_name) ! field_with(:name => field_name).nil? end |
#has_value?(value) ⇒ Boolean
56 57 58 |
# File 'lib/mechanize/form.rb', line 56 def has_value?(value) ! field_with(:value => value).nil? end |
#keys ⇒ Object
60 |
# File 'lib/mechanize/form.rb', line 60 def keys; fields.map { |f| f.name }; end |
#pretty_print(q) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mechanize/inspect.rb', line 55 def pretty_print(q) q.object_group(self) { q.breakable; q.group(1, '{name', '}') { q.breakable; q.pp name } q.breakable; q.group(1, '{method', '}') { q.breakable; q.pp method } q.breakable; q.group(1, '{action', '}') { q.breakable; q.pp action } q.breakable; q.group(1, '{fields', '}') { fields.each do |field| q.breakable q.pp field end } q.breakable; q.group(1, '{radiobuttons', '}') { .each { |b| q.breakable; q.pp b } } q.breakable; q.group(1, '{checkboxes', '}') { checkboxes.each { |b| q.breakable; q.pp b } } q.breakable; q.group(1, '{file_uploads', '}') { file_uploads.each { |b| q.breakable; q.pp b } } q.breakable; q.group(1, '{buttons', '}') { .each { |b| q.breakable; q.pp b } } } end |
#request_data ⇒ Object
This method calculates the request data to be sent back to the server for this form, depending on if this is a regular post, get, or a multi-part post,
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/mechanize/form.rb', line 218 def request_data query_params = build_query() case @enctype.downcase when /^multipart\/form-data/ boundary = rand_string(20) @enctype = "multipart/form-data; boundary=#{boundary}" params = [] query_params.each { |k,v| params << param_to_multipart(k, v) unless k.nil? } @file_uploads.each { |f| params << file_to_multipart(f) } params.collect { |p| "--#{boundary}\r\n#{p}" }.join('') + "--#{boundary}--\r\n" else Mechanize::Util.build_query_string(query_params) end end |
#set_fields(fields = {}) ⇒ Object
This method sets multiple fields on the form. It takes a list of field name, value pairs. If there is more than one field found with the same name, this method will set the first one found. If you want to set the value of a duplicate field, use a value which is a Hash with the key as the index in to the form. The index is zero based. For example, to set the second field named ‘foo’, you could do the following:
form.set_fields( :foo => { 1 => 'bar' } )
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/mechanize/form.rb', line 82 def set_fields(fields = {}) fields.each do |k,v| case v when Hash v.each do |index, value| self.fields_with(:name => k.to_s).[](index).value = value end else value = nil index = 0 [v].flatten.each do |val| index = val.to_i unless value.nil? value = val if value.nil? end self.fields_with(:name => k.to_s).[](index).value = value end end end |
#submit(button = nil, headers = {}) ⇒ Object
Submit this form with the button passed in
134 135 136 |
# File 'lib/mechanize/form.rb', line 134 def submit =nil, headers = {} @mech.submit(self, , headers) end |
#values ⇒ Object
62 |
# File 'lib/mechanize/form.rb', line 62 def values; fields.map { |f| f.value }; end |