lib/mechanize/form.rb in mechanize-2.5.1 vs lib/mechanize/form.rb in mechanize-2.6.0

- old
+ new

@@ -1,9 +1,9 @@ require 'mechanize/element_matcher' # 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. +# input field available in a form can be accessed through this object. # # == Examples # # Find a form and print out its fields # @@ -36,11 +36,11 @@ alias :elements :fields attr_reader :form_node attr_reader :page - def initialize(node, mech=nil, page=nil) + def initialize(node, mech = nil, page = nil) @enctype = node['enctype'] || 'application/x-www-form-urlencoded' @form_node = node @action = Mechanize::Util.html_unescape(node['action']) @method = (node['method'] || 'GET').upcase @name = node['name'] @@ -58,31 +58,80 @@ fields.find { |f| f.name == field_name } end alias :has_key? :has_field? + # Returns whether or not the form contains a field with +value+ def has_value?(value) fields.find { |f| f.value == value } end - def keys; fields.map { |f| f.name }; end + # Returns all field names (keys) for this form + def keys + fields.map { |f| f.name } + end - def values; fields.map { |f| f.value }; end + # Returns all field values for this form + def values + fields.map { |f| f.value } + end - def submits ; @submits ||= buttons.select { |f| f.class == Submit }; end - def resets ; @resets ||= buttons.select { |f| f.class == Reset }; end - def texts ; @texts ||= fields.select { |f| f.class == Text }; end - def hiddens ; @hiddens ||= fields.select { |f| f.class == Hidden }; end - def textareas; @textareas ||= fields.select { |f| f.class == Textarea }; end - def keygens ; @keygens ||= fields.select { |f| f.class == Keygen }; end + # Returns all buttons of type Submit + def submits + @submits ||= buttons.select { |f| f.class == Submit } + end - def submit_button?(button_name) submits.find{|f| f.name == button_name}; end - def reset_button?(button_name) resets.find{|f| f.name == button_name}; end - def text_field?(field_name) texts.find{|f| f.name == field_name}; end - def hidden_field?(field_name) hiddens.find{|f| f.name == field_name}; end - def textarea_field?(field_name) textareas.find{|f| f.name == field_name}; end + # Returns all buttons of type Reset + def resets + @resets ||= buttons.select { |f| f.class == Reset } + end + # Returns all fields of type Text + def texts + @texts ||= fields.select { |f| f.class == Text } + end + + # Returns all fields of type Hidden + def hiddens + @hiddens ||= fields.select { |f| f.class == Hidden } + end + + # Returns all fields of type Textarea + def textareas + @textareas ||= fields.select { |f| f.class == Textarea } + end + + # Returns all fields of type Keygen + def keygens + @keygens ||= fields.select { |f| f.class == Keygen } + end + + # Returns whether or not the form contains a Submit button named +button_name+ + def submit_button?(button_name) + submits.find { |f| f.name == button_name } + end + + # Returns whether or not the form contains a Reset button named +button_name+ + def reset_button?(button_name) + resets.find { |f| f.name == button_name } + end + + # Returns whether or not the form contains a Text field named +field_name+ + def text_field?(field_name) + texts.find { |f| f.name == field_name } + end + + # Returns whether or not the form contains a Hidden field named +field_name+ + def hidden_field?(field_name) + hiddens.find { |f| f.name == field_name } + end + + # Returns whether or not the form contains a Textarea named +field_name+ + def textarea_field?(field_name) + textareas.find { |f| f.name == field_name } + end + # This method is a shortcut to get form's DOM id. # Common usage: # page.form_with(:dom_id => "foorm") # Note that you can also use +:id+ to get to this method: # page.form_with(:id => "foorm") @@ -115,11 +164,10 @@ # # For example, to set the second field named 'foo', you could do the # following: # # form.set_fields :foo => { 1 => 'bar' } - def set_fields fields = {} fields.each do |name, v| case v when Hash v.each do |index, value| @@ -137,22 +185,18 @@ self.fields_with(:name => name.to_s)[index].value = value end end end - # Fetch the value of the first input field with the name passed in - # ==Example - # Fetch the value set in the input field 'name' + # Fetch the value of the first input field with the name passed in. Example: # puts form['name'] def [](field_name) f = field(field_name) f && f.value end - # Set the value of the first input field with the name passed in - # ==Example - # Set the value in the input field 'name' to "Aaron" + # Set the value of the first input field with the name passed in. Example: # form['name'] = 'Aaron' def []=(field_name, value) f = field(field_name) if f f.value = value @@ -171,12 +215,13 @@ end super end - # Submit this form with the button passed in - def submit button=nil, headers = {} + # Submit the form. Does not include the +button+ as a form parameter. + # Use +click_button+ or provide button as a parameter. + def submit button = nil, headers = {} @mech.submit(self, button, headers) end # Submit form using +button+. Defaults # to the first button. @@ -207,11 +252,13 @@ query = [] @mech.log.info("form encoding: #{encoding}") if @mech && @mech.log successful_controls = [] - (fields + checkboxes).sort.each do |f| + (fields + checkboxes).reject do |f| + f.node["disabled"] + end.sort.each do |f| case f when Mechanize::Form::CheckBox if f.checked successful_controls << f end @@ -479,11 +526,11 @@ form_node.search('button').each do |node| type = (node['type'] || 'submit').downcase next if type == 'reset' @buttons << Button.new(node) end - + # Find all keygen tags form_node.search('keygen').each do |node| @fields << Keygen.new(node, node['value'] || '') end end @@ -530,11 +577,10 @@ "\r\n#{file.file_data}\r\n" end body end - end require 'mechanize/form/field' require 'mechanize/form/button' require 'mechanize/form/hidden' @@ -548,6 +594,5 @@ require 'mechanize/form/multi_select_list' require 'mechanize/form/option' require 'mechanize/form/radio_button' require 'mechanize/form/check_box' require 'mechanize/form/select_list' -