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.



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

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/webrat/core/elements/field.rb', line 38

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



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

def self.field_classes
  @field_classes || []
end

.inherited(klass) ⇒ Object



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

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

.load(session, element) ⇒ Object



33
34
35
36
# File 'lib/webrat/core/elements/field.rb', line 33

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

.xpath_search_excluding_hiddenObject



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

def self.xpath_search_excluding_hidden
  [".//button", ".//input[ @type != 'hidden']", ".//textarea", ".//select"]
end

Instance Method Details

#disabled?Boolean

Returns:

  • (Boolean)


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

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

#idObject



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

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

#label_textObject



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

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

#raise_error_if_disabledObject

Raises:



77
78
79
80
# File 'lib/webrat/core/elements/field.rb', line 77

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_paramObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/webrat/core/elements/field.rb', line 82

def to_param
  return nil if disabled?

  case Webrat.configuration.mode
  when :rails
    parse_rails_request_params("#{name}=#{escaped_value}")
  when :merb
    ::Merb::Parse.query("#{name}=#{escaped_value}")
  else
    { name => value }
  end
end

#unsetObject



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

def unset
  @value = default_value
end