Class: Webrat::Field

Inherits:
Element show all
Defined in:
lib/webrat/core/elements/field.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes inherited from Element

#element

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Element

#inspect, load_all, #path

Constructor Details

#initialize(*args) ⇒ Field

Returns a new instance of Field.



54
55
56
57
# File 'lib/webrat/core/elements/field.rb', line 54

def initialize(*args)
  super
  @value = default_value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



13
14
15
# File 'lib/webrat/core/elements/field.rb', line 13

def value
  @value
end

Class Method Details

.field_class(element) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/webrat/core/elements/field.rb', line 34

def self.field_class(element)
  case element.name
  when "button"   then ButtonField
  when "select"   then SelectField
  when "textarea" then TextareaField
  else
    case Webrat::XML.attribute(element, "type")
    when "checkbox" then CheckboxField
    when "hidden"   then HiddenField
    when "radio"    then RadioField
    when "password" then PasswordField
    when "file"     then FileField
    when "reset"    then ResetField
    when "submit"   then ButtonField
    when "image"    then ButtonField
    else  TextField
    end
  end
end

.field_classesObject



19
20
21
# File 'lib/webrat/core/elements/field.rb', line 19

def self.field_classes
  @field_classes || []
end

.inherited(klass) ⇒ Object



23
24
25
26
27
# File 'lib/webrat/core/elements/field.rb', line 23

def self.inherited(klass)
  @field_classes ||= []
  @field_classes << klass
  # raise args.inspect
end

.load(session, element) ⇒ Object



29
30
31
32
# File 'lib/webrat/core/elements/field.rb', line 29

def self.load(session, element)
  return nil if element.nil?
  session.elements[Webrat::XML.xpath_to(element)] ||= field_class(element).new(session, element)
end

.xpath_searchObject



15
16
17
# File 'lib/webrat/core/elements/field.rb', line 15

def self.xpath_search
  [".//button", ".//input", ".//textarea", ".//select"]
end

Instance Method Details

#disabled?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/webrat/core/elements/field.rb', line 68

def disabled?
  @element.attributes.has_key?("disabled") && Webrat::XML.attribute(@element, "disabled") != 'false'
end

#idObject



64
65
66
# File 'lib/webrat/core/elements/field.rb', line 64

def id
  Webrat::XML.attribute(@element, "id")
end

#label_textObject



59
60
61
62
# File 'lib/webrat/core/elements/field.rb', line 59

def label_text
  return nil if labels.empty?
  labels.first.text
end

#raise_error_if_disabledObject

Raises:



72
73
74
75
# File 'lib/webrat/core/elements/field.rb', line 72

def raise_error_if_disabled
  return unless disabled?
  raise DisabledFieldError.new("Cannot interact with disabled form element (#{self})")
end

#set(value) ⇒ Object



90
91
92
# File 'lib/webrat/core/elements/field.rb', line 90

def set(value)
  @value = value
end

#to_paramObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/webrat/core/elements/field.rb', line 77

def to_param
  return nil if disabled?
  
  case Webrat.configuration.mode
  when :rails
    ActionController::AbstractRequest.parse_query_parameters("#{name}=#{escaped_value}")
  when :merb
    ::Merb::Parse.query("#{name}=#{escaped_value}")
  else
    { name => escaped_value }
  end
end

#unsetObject



94
95
96
# File 'lib/webrat/core/elements/field.rb', line 94

def unset
  @value = default_value
end