Module: Awetestlib::Regression::PageData

Defined in:
lib/awetestlib/regression/page_data.rb

Overview

Methods for capture and manipulation of data contained in text, values, and/or states of spans, text fields, radios, checkboxes, and select_lists.

Core collapse

Legacy (Backward compatible usage) collapse

Instance Method Details

#capture_page_data(browser, types = [:text, :textarea, :select_list, :span, :hidden, :checkbox, :radio]) ⇒ Object

:category: Page Data :tags: data, DOM, page

Parameters

browser is any container element, usually the browser window or a div within it. Best to use is the smallest that contains the desired data.

types is an array that defaults to all of: :text, :textarea, :select_list, :span, :hidden, :checkbox, and :radio. Set types to an array of a subset of these if fewer elements are desired.

No positive validations are reported but failure is rescued and reported.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/awetestlib/regression/page_data.rb', line 22

def capture_page_data(browser, types = [:text, :textarea, :select_list, :span, :hidden, :checkbox, :radio])
  start = Time.now
  debug_to_log("Begin #{__method__}")
  data         = Hash.new
  data[:id]    = Hash.new
  data[:name]  = Hash.new
  data[:index] = Hash.new
  types.each do |type|
    #debug_to_log("#{__method__}: #{type}. . .")
    data[:id][type], data[:name][type], data[:index][type] = parse_elements(browser, type)
  end
  data
rescue
  failed_to_log("#{__method__}: '#{$!}'")
ensure
  stop = Time.now
  passed_to_log("#{__method__.to_s.titleize} finished. (#{"%.5f" % (stop - start)} secs)")
  #debug_to_log("End #{__method__}")
end

#compare_page_data(before, after, how, desc = '') ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/awetestlib/regression/page_data.rb', line 42

def compare_page_data(before, after, how, desc = '')
  [:text, :textarea, :select_list, :span, :hidden, :checkbox, :radio].each do |type|
    before[how][type].each_key do |what|
      msg = "#{desc} #{type} #{how}=#{what}: Expected '#{before[how][type][what]}'."
      if after[how][type][what] == before[how][type][what]
        passed_to_log(msg)
      else
        failed_to_log("#{msg} Found '#{after[how][type][what]}'")
      end
    end
  end
rescue
  failed_to_log("Unable to compare before and after page data. '#{$!}'")
end

#fetch_page_data(data, how, what, type, get_text = true) ⇒ Object

:category: Page Data :tags:data, DOM

data is the hash returned by capture_page_data().

how is one of :id, :name, :index

what is the target value for how. It can be a string or a regular expression

type is one of :text,:textarea,:select_list,:span,:hidden,:checkbox,:radio

get_text determines whether selected option’s text or value is returned. Default is true, i.e., return the selected text. This only applies when the type is :select_list.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/awetestlib/regression/page_data.rb', line 73

def fetch_page_data(data, how, what, type, get_text = true)
  rslt = data[how][type][what]
  if type == :select_list
    value, text = rslt.split('::')
    if get_text
      rslt = text
    else
      rslt = value
    end
  end
  rslt
end

#get_element_text(browser, element, how, what, desc = '') ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/awetestlib/regression/page_data.rb', line 165

def get_element_text(browser, element, how, what, desc = '')
  msg = build_message("Return text in #{element} #{how}='#{what}'", desc)
  text = browser.element(how, what).text
  passed_to_log("#{msg} text='#{text}'")
  text
rescue
  failed_to_log("Unable to #{msg}: '#{$!}'")
end

#get_textfield_value(browser, how, what, desc = '') ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/awetestlib/regression/page_data.rb', line 150

def get_textfield_value(browser, how, what, desc = '')
  msg = build_message("Return value in textfield #{how}='#{what}'", desc)
  tf = browser.text_field(how, what)
  if tf
    debug_to_log("#{tf.inspect}")
    vlu = tf.value
    passed_to_log("#{msg} Value='#{vlu}'")
    vlu
  else
    failed_to_log("#{msg}")
  end
rescue
  failed_to_log("Unable to #{msg}: '#{$!}'")
end

#get_textfield_value_by_id(browser, strg) ⇒ Object



182
183
184
# File 'lib/awetestlib/regression/page_data.rb', line 182

def get_textfield_value_by_id(browser, strg)
  get_textfield_value(browser, :id, strg)
end

#get_textfield_value_by_name(browser, strg, desc = '') ⇒ Object



178
179
180
# File 'lib/awetestlib/regression/page_data.rb', line 178

def get_textfield_value_by_name(browser, strg, desc = '')
  get_textfield_value(browser, :name, strg, desc)
end

#parse_elements(browser, type) ⇒ Object

:category: Page Data :tags:data, DOM

browser is any container element. best to use is the smallest that contains the desired data.

type is one of these symbols: :text,:textarea,:select_list,:span,:hidden,:checkbox,:radio

Returns three hashes: id[id] = value, name[id] = value, index[id] = value

A given element appears once in the set of hashes depending on how is is found: id first then name, then index.

Select list value is in the form ‘value::text’. parse with x.split(‘::’)

No positive validations are reported but failure is rescued and reported.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/awetestlib/regression/page_data.rb', line 103

def parse_elements(browser, type)
  id    = Hash.new
  name  = Hash.new
  index = Hash.new
  idx   = 0
  #debug_to_log("#{__method__}: #{type}")
  case type
    when :span
      collection = browser.spans
    when :select_list
      collection = browser.select_lists
    when :radio
      collection = browser.radios
    when :checkbox
      collection = browser.checkboxes
    else
      collection = browser.elements(:type, type.to_s)
  end
  #debug_to_log("#{__method__}: collection: #{collection.inspect}")
  collection.each do |e|
    case type
      when :span
        vlu = e.text
      when :select_list
        vlu = "#{e.value}::#{e.selected_options[0]}"
      when :radio
        vlu = e.set?
      when :checkbox
        vlu = e.set?
      else
        vlu = e.value
    end
    idx += 1
    if e.id.length > 0 and not e.id =~ /^__[A-Z]/
      id[e.id] = vlu
    elsif e.name.length > 0 and not e.name =~ /^__[A-Z]/
      name[e.name] = vlu
    else
      index[idx] = vlu if not type == :hidden
    end
  end
  [id, name, index]

rescue
  failed_to_log("#{__method__}: '#{$!}'")
end