class Pincers::Core::Helpers::Form

Attributes

backend[R]

Public Class Methods

new(_backend, _form_element, _trigger_element = nil) click to toggle source
# File lib/pincers/core/helpers/form.rb, line 9
def initialize(_backend, _form_element, _trigger_element = nil)
  @backend = _backend
  @form = _form_element
  @trigger = _trigger_element
  @force_multipart = false
end

Public Instance Methods

action() click to toggle source
# File lib/pincers/core/helpers/form.rb, line 16
def action
  @action ||= begin
    (trigger_attr(:formaction) || form_attr(:action) || '')
  end
end
encoding() click to toggle source
# File lib/pincers/core/helpers/form.rb, line 34
def encoding
  process_inputs
  @encoding ||= begin
    if @force_multipart
      Pincers::Http::Utils::FORM_MULTIPART
    else
      trigger_attr(:formenctype) || form_attr(:enctype) || Pincers::Http::Utils::FORM_URLENCODED
    end
  end
end
inputs() click to toggle source
# File lib/pincers/core/helpers/form.rb, line 45
def inputs
  process_inputs
  @inputs
end
method() click to toggle source
# File lib/pincers/core/helpers/form.rb, line 22
def method
  @method ||= begin
    (trigger_attr(:formmethod) || form_attr(:method) || 'get').downcase.to_sym
  end
end
target() click to toggle source
# File lib/pincers/core/helpers/form.rb, line 28
def target
  @target ||= begin
    trigger_attr(:formtarget) || form_attr(:target)
  end
end

Private Instance Methods

categorize_input(_input) click to toggle source
# File lib/pincers/core/helpers/form.rb, line 78
def categorize_input(_input)
  case backend.extract_element_tag _input
  when 'input'
    input_type = backend.extract_element_attribute(_input, :type)
    return nil if input_type == 'submit' && !is_trigger?(_input)
    return nil if input_type == 'checkbox' && !is_checked?(_input)
    return nil if input_type == 'radio' && !is_checked?(_input)
    input_type == 'file' ? :multipart : :urlencoded
  when 'button'
    input_type = backend.extract_element_attribute(_input, :type) || 'submit'
    return nil if input_type != 'submit' || !is_trigger?(_input)
    :urlencoded
  when 'textarea', 'select'
    :urlencoded
  else
    nil
  end
end
form_attr(_name) click to toggle source
# File lib/pincers/core/helpers/form.rb, line 57
def form_attr(_name)
  backend.extract_element_attribute(@form, _name)
end
is_checked?(_input) click to toggle source
# File lib/pincers/core/helpers/form.rb, line 102
def is_checked?(_input)
  backend.extract_element_attribute _input, :checked
end
is_trigger?(_input) click to toggle source
# File lib/pincers/core/helpers/form.rb, line 97
def is_trigger?(_input)
  return false if @trigger.nil?
  backend.elements_equal _input, @trigger
end
process_inputs() click to toggle source
# File lib/pincers/core/helpers/form.rb, line 61
def process_inputs
  return unless @inputs.nil?
  elements = backend.search_by_xpath(@form, './/*[@name]', nil)
  @inputs = elements.map do |input|
    category = categorize_input input
    next nil if category.nil?

    @force_multipart = true if category == :multipart

    value = backend.extract_element_attribute(input, :value)
    next nil if value.nil?

    name = backend.extract_element_attribute(input, :name)
    [name, value]
  end.reject(&:nil?)
end
trigger_attr(_name) click to toggle source
# File lib/pincers/core/helpers/form.rb, line 52
def trigger_attr(_name)
  return nil if @trigger.nil?
  backend.extract_element_attribute(@trigger, _name)
end