Class: Chop::Form::Field

Inherits:
Struct
  • Object
show all
Defined in:
lib/chop/form.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fieldObject

Returns the value of attribute field

Returns:

  • (Object)

    the current value of field



105
106
107
# File 'lib/chop/form.rb', line 105

def field
  @field
end

#labelObject

Returns the value of attribute label

Returns:

  • (Object)

    the current value of label



105
106
107
# File 'lib/chop/form.rb', line 105

def label
  @label
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



105
106
107
# File 'lib/chop/form.rb', line 105

def path
  @path
end

#sessionObject

Returns the value of attribute session

Returns:

  • (Object)

    the current value of session



105
106
107
# File 'lib/chop/form.rb', line 105

def session
  @session
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



105
106
107
# File 'lib/chop/form.rb', line 105

def value
  @value
end

Class Method Details

.candidatesObject



119
120
121
122
123
# File 'lib/chop/form.rb', line 119

def self.candidates
  descendants.sort_by do |a|
    a == Chop::Form::Default ? 1 : -1 # ensure Default comes last
  end
end

.combined_css_selectorObject



129
130
131
# File 'lib/chop/form.rb', line 129

def self.combined_css_selector
  candidates.map(&:css_selector).uniq.join(", ")
end

.css_selectorObject



125
126
127
# File 'lib/chop/form.rb', line 125

def self.css_selector
  "input"
end

.for(session, label, value, path) ⇒ Object



106
107
108
109
110
111
# File 'lib/chop/form.rb', line 106

def self.for session, label, value, path
  field = FieldFinder.new(session, combined_css_selector).find(label)
  candidates.map do |klass|
    klass.new(session, label, value, path, field)
  end.find(&:matches?)
end

.from(session, field) ⇒ Object



113
114
115
116
117
# File 'lib/chop/form.rb', line 113

def self.from session, field
  candidates.map do |klass|
    klass.new(session, nil, nil, nil, field)
  end.find(&:matches?)
end

Instance Method Details

#diff_valueObject



154
155
156
# File 'lib/chop/form.rb', line 154

def diff_value
  get_value.to_s
end

#fill_in!Object



158
159
160
# File 'lib/chop/form.rb', line 158

def fill_in!
  field.set value
end

#get_valueObject



133
134
135
# File 'lib/chop/form.rb', line 133

def get_value
  field.value
end

#label_textObject



143
144
145
146
147
# File 'lib/chop/form.rb', line 143

def label_text
  return nil unless field[:id].present?
  label_element = session.first("label[for='#{field[:id]}']", visible: :all, minimum: 0, wait: 0.1)
  label_element&.text(:all)
end

#should_include_in_diff?Boolean

Returns:

  • (Boolean)


137
138
139
140
141
# File 'lib/chop/form.rb', line 137

def should_include_in_diff?
  field[:name].present? &&
    field[:type] != "submit" &&
    field[:type] != "hidden"
end

#to_diff_rowObject



149
150
151
152
# File 'lib/chop/form.rb', line 149

def to_diff_row
  return nil unless label = label_text
  [label, diff_value]
end