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.



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

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
58
59
60
61
62
# File 'lib/webrat/core/elements/field.rb', line 38

def self.field_class(element)
  case element.name
  when "button"   then ButtonField
  when "select"
    if element.attributes["multiple"].nil?
      SelectField
    else
      MultipleSelectField
    end
  when "textarea" then TextareaField
  else
    case 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[element.path] ||= 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)


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

def disabled?
  @element.attributes.has_key?("disabled") && @element["disabled"] != 'false'
end

#idObject



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

def id
  @element["id"]
end

#label_textObject



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

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

#raise_error_if_disabledObject

Raises:



82
83
84
85
# File 'lib/webrat/core/elements/field.rb', line 82

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

#set(value) ⇒ Object



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

def set(value)
  @value = value
end

#to_paramObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/webrat/core/elements/field.rb', line 87

def to_param
  return nil if disabled?

  params = case Webrat.configuration.mode
  when :rails
    parse_rails_request_params("#{name}=#{escaped_value}")
  when :merb
    ::Merb::Parse.query("#{name}=#{escaped_value}")
  else
    { name => [*@value].first.to_s }
  end

  unescape_params(params)
end

#unsetObject



106
107
108
# File 'lib/webrat/core/elements/field.rb', line 106

def unset
  @value = default_value
end