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.



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

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
53
# 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 "button"   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)


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

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

#idObject



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

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

#label_textObject



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

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

#nameObject



103
104
105
# File 'lib/webrat/core/elements/field.rb', line 103

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

#raise_error_if_disabledObject

Raises:



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

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

#set(value) ⇒ Object



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

def set(value)
  @value = value
end

#to_multipart_paramObject



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

def to_multipart_param
  { name => escaped_value }
end

#to_paramObject



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

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

#unsetObject



99
100
101
# File 'lib/webrat/core/elements/field.rb', line 99

def unset
  @value = default_value
end