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


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

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

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value


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

def value
  @value
end

Class Method Details

.field_class(element) ⇒ Object


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

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


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

def self.field_classes
  @field_classes || []
end

.inherited(klass) ⇒ Object


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

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

.load(session, element) ⇒ Object


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

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

.xpath_searchObject


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

def self.xpath_search
  ".//button|.//input|.//textarea|.//select"
end

.xpath_search_excluding_hiddenObject


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

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

Instance Method Details

#disabled?Boolean

Returns:

  • (Boolean)

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

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

#idObject


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

def id
  @element["id"]
end

#label_textObject


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

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

#raise_error_if_disabledObject

Raises:


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

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

#set(value) ⇒ Object


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

def set(value)
  @value = value
end

#to_query_stringObject


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

def to_query_string
  return nil if disabled?

  query_string = case Webrat.configuration.mode
  when :rails, :merb, :rack, :sinatra
    build_query_string
  when :mechanize
    build_query_string(false)
  end

  query_string
end

#unsetObject


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

def unset
  @value = default_value
end