Module: Awetestlib::Regression::Find

Included in:
Runner
Defined in:
lib/awetestlib/regression/find.rb

Overview

Methods to fetch references to DOM elements for assigning to variables. Includes collections of elements. Primary use is to limit the scope of other commands to the element passed in their browser parameter. (example here)

Core collapse

Deprecated collapse

Instance Method Details

Return a hash of all links in browser with :href attribute containing the exact url in href.

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.

  • href (String)

    The exact url to be located.

Returns:

  • (Hash)

    The hash is indexed by the order in which the links were located in browser.



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/awetestlib/regression/find.rb', line 274

def find_all_links_with_exact_href(browser, href)
  links = browser.links
  hash  = Hash.new
  idx   = 0
  links.each do |l|
    idx     += 1
    an_href = href
    my_href = l.href
    if my_href == an_href
      hash[idx] = l
      debug_to_log("#{__method__}:#{idx}\n********\n#{l.to_s}\n\n#{l.to_yaml}")
    end
  end
  hash
end

#find_index_for_element(browser, element, how, ord, what) ⇒ Object Also known as: find_index_for_object

Deprecated.

Find the index of an element within browser which has attribute how containing what



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/awetestlib/regression/find.rb', line 317

def find_index_for_element(browser, element, how, ord, what)
  element_sym = (element.to_s.pluralize).to_sym
  how_str = how.to_s
  ptrn    = /#{how}:\s+#{what}/i
  list    = get_element_collection(browser, element_sym, true)
  cnt     = 0
  idx     = 0
  list.each do |nty|
    s = nty.to_s
    #      a    = nty.to_a
    if s =~ ptrn
      cnt += 1
      if cnt == ord
        break
      end
    end
    idx += 1
  end
  idx
end

Return a reference to the first link in browser with :href attribute containing the exact url in href.

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.

  • href (String)

    The exact url to be located.

Returns:

  • (Watir::Link)


294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/awetestlib/regression/find.rb', line 294

def find_link_with_exact_href(browser, href)
  links = browser.links
  link  = nil
  index = 0
  links.each do |l|
    index   += 1
    an_href = href
    my_href = l.href
    if my_href == an_href
      link = l
      #        debug_to_log("#{__method__}:#{__LINE__}\n********\n#{l.to_s}\n\n#{l.to_yaml}")
      break
    end
  end
  link
end

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/awetestlib/regression/find.rb', line 75

def get_attribute_value(browser, element, how, what, attribute, desc = '')
  msg = build_message("Value of #{attribute} in #{element} #{how}=>#{what}.", desc)
  case element
    when :link
      value = browser.link(how => what).attribute_value attribute
    when :button
      value = browser.button(how => what).attribute_value attribute
    else
      if browser.element(how => what).responds_to('attribute_value')
        value = browser.element(how => what).attribute_value attribute
      end
  end
  value
rescue
  failed_to_log(" Unable to #{msg}: '#{$!}'")
end

#get_div(browser, how, what, desc = '', dbg = false) ⇒ Watir::Div

Return a reference to a div element identified by the contents what of its attribute how. This differs from get_element in that it waits for the element to exist before trying to return it. that uniquely identifies the element.

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.

  • how (Symbol)

    The element attribute used to identify the specific element. Valid values depend on the kind of element. Common values: :text, :id, :title, :name, :class, :href (:link only)

  • what (String, Regexp)

    A string or a regular expression to be found in the how attribute

  • desc (String) (defaults to: '')

    Contains a message or description intended to appear in the log and/or report output

  • dbg (Boolean) (defaults to: false)

    If set to true additional debug logging is performed.

Returns:

  • (Watir::Div)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/awetestlib/regression/find.rb', line 142

def get_div(browser, how, what, desc = '', dbg = false)
  msg = build_message("Get :div #{how}=>#{what}.", desc)
  Watir::Wait.until { browser.div(how, what).exists? }
  div = browser.div(how, what)
  debug_to_log(div.inspect) if dbg
  if div
    passed_to_log(msg)
    return div
  else
    failed_to_log(msg)
  end
rescue
  failed_to_log("Unable to '#{msg}' '#{$!}'")
end

#get_element(browser, element, how, what, value = nil, desc = '') ⇒ Object

Return a reference to a DOM element specified by its type element, attribute how, and the contents of that attribute what. Some elements may require use of the :value attribute in addition to the designated one. The target contents for :value are supplied in value.

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.

  • element (Symbol)

    The kind of element to click. Must be one of the elements recognized by Watir. Some common values are :link, :button, :image, :div, :span.

  • how (Symbol)

    The element attribute used to identify the specific element. Valid values depend on the kind of element. Common values: :text, :id, :title, :name, :class, :href (:link only)

  • what (String, Regexp)

    A string or a regular expression to be found in the how attribute that uniquely identifies the element.

  • value (String, Regexp) (defaults to: nil)

    A string or a regular expression to be found in the :value attribute that uniquely identifies the element.

  • desc (String) (defaults to: '')

    Contains a message or description intended to appear in the log and/or report output



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/awetestlib/regression/find.rb', line 22

def get_element(browser, element, how, what, value = nil, desc = '')
  msg    = build_message("Return #{element} with :#{how}=#{what}", value, desc)
  target = nil
  what = Regexp.new(Regexp.escape(what)) unless how == :index or what.is_a?(Regexp)
  case element
    when :link
      target = browser.link(how, what)
    when :button
      target = browser.button(how, what)
    when :div
      target = browser.div(how, what)
    when :checkbox
      target = browser.checkbox(how, what, value)
    when :text_field, :textfield
      target = browser.text_field(how, what)
    when :image
      target = browser.image(how, what)
    when :file_field, :filefield
      target = browser.file_field(how, what)
    when :form
      target = browser.form(how, what)
    when :frame
      target = browser.frame(how, what)
    when :radio
      target = browser.radio(how, what, value)
    when :span
      target = browser.span(how, what)
    when :table
      target = browser.table(how, what)
    when :li
      target = browser.li(how, what)
    when :select_list, :selectlist
      target = browser.select_list(how, what)
    when :hidden
      target = browser.hidden(how, what)
    when :area
      target = browser.area(how, what)
    else
      target = browser.element(how, what)
  end
  if target.exists?
    passed_to_log(msg)
    target
  else
    failed_to_log(msg)
    nil
  end
rescue => e
  unless rescue_me(e, __method__, rescue_me_command(target, how, what), "#{browser.class}", target)
    raise e
  end
end

#get_element_collection(browser, element, dbg = false) ⇒ Array Also known as: get_objects

Return an array (collection) of all the elements of the type which contained in the browser or container supplied in browser.

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.

  • element (Symbol)

    The kind of element to click. Must be one of the elements recognized by Watir. Some common values are :link, :button, :image, :div, :span.

  • dbg (Boolean) (defaults to: false)

    If set to true additional debug logging is performed.

Returns:

  • (Array)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/awetestlib/regression/find.rb', line 216

def get_element_collection(browser, element, dbg = false)
  cnt = 0
  case element
    when :links, :link
      list = browser.links
      sleep(1)
    when :tables, :table
      list = browser.tables
    when :divs, :div
      list = browser.divs
    when :buttons, :button
      list = browser.buttons
    when :checkboxes, :checkbox
      list = browser.checkboxes
    when :radios, :radio
      list = browser.radios
    when :selectlists, :select_lists, :selectlist, :select_list
      list = browser.selectlists
    when :textfields, :text_fields, :textareas, :text_fields, :textfield, :text_field, :textarea, :text_area
      list = browser.textfields
    when :lis, :li
      list = browser.lis
    when :uls, :ul
      list = browser.uls
    else
      debug_to_log("Unsupported DOM object '#{which}'")
  end
  if dbg
    list.each do |obj|
      cnt += 1
      debug_to_log("\n==========#{which}:\nindex:     #{cnt}\n#{obj}\n#{obj.to_yaml}")
    end
  end
  list
end

#get_form(browser, how, what, desc = '') ⇒ Watir::Form

Return a reference to a form element identified by the contents what of its attribute how.

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.

  • how (Symbol)

    The element attribute used to identify the specific element. Valid values depend on the kind of element. Common values: :text, :id, :title, :name, :class, :href (:link only)

  • what (String, Regexp)

    A string or a regular expression to be found in the how attribute

  • desc (String) (defaults to: '')

    Contains a message or description intended to appear in the log and/or report output

Returns:

  • (Watir::Form)


191
192
193
# File 'lib/awetestlib/regression/find.rb', line 191

def get_form(browser, how, what, desc = '')
  get_element(browser, :form, how, what, desc)
end

#get_frame(browser, how, what, desc = '') ⇒ Watir::Frame

Return a reference to a frame element identified by the contents what of its attribute how.

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.

  • how (Symbol)

    The element attribute used to identify the specific element. Valid values depend on the kind of element. Common values: :text, :id, :title, :name, :class, :href (:link only)

  • what (String, Regexp)

    A string or a regular expression to be found in the how attribute

  • desc (String) (defaults to: '')

    Contains a message or description intended to appear in the log and/or report output

Returns:

  • (Watir::Frame)


198
199
200
# File 'lib/awetestlib/regression/find.rb', line 198

def get_frame(browser, how, what, desc = '')
  get_element(browser, :frame, how, what, desc)
end

#get_ole(element) ⇒ Object

TODO:

Detect $watir_script variable and disable if not set to true

Note:

Usable only with classic Watir.

Return the ole object for the specified element.

Parameters:

  • element (Symbol)

    A reference to the already identified element.



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/awetestlib/regression/find.rb', line 258

def get_ole(element)
  ole = element.ole_object
  if ole
    passed_to_log("Found ole_object for #{element}.")
    ole
  else
    failed_to_log("Did not find ole_object for #{element}.")
  end
rescue
  failed_to_log("Unable to find ole_object for #{element}.  #{$!}")
end

#get_select_list(browser, how, what, desc = '') ⇒ Watir::SelectList

Return a reference to a select_list element identified by the contents what of its attribute how.

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.

  • how (Symbol)

    The element attribute used to identify the specific element. Valid values depend on the kind of element. Common values: :text, :id, :title, :name, :class, :href (:link only)

  • what (String, Regexp)

    A string or a regular expression to be found in the how attribute

  • desc (String) (defaults to: '')

    Contains a message or description intended to appear in the log and/or report output

Returns:

  • (Watir::SelectList)


205
206
207
# File 'lib/awetestlib/regression/find.rb', line 205

def get_select_list(browser, how, what, desc = '')
  get_element(browser, :select_list, how, what, desc)
end

#get_select_options(browser, how, what, dump = false) ⇒ Array

Return an array containing the options available for selection in a select_list identifified by its attribute how, and the contents of that attribute what. that uniquely identifies the element. See Utilities#dump_select_list_options

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.

  • how (Symbol)

    The element attribute used to identify the specific element. Valid values depend on the kind of element. Common values: :text, :id, :title, :name, :class, :href (:link only)

  • what (String, Regexp)

    A string or a regular expression to be found in the how attribute

  • dump (Boolean) (defaults to: false)

    If set to true, a dump of the contents of the options will go to the log.

Returns:

  • (Array)


103
104
105
106
107
108
109
# File 'lib/awetestlib/regression/find.rb', line 103

def get_select_options(browser, how, what, dump = false)
  list = browser.select_list(how, what)
  dump_select_list_options(list) if dump
  list.options
rescue
  failed_to_log("Unable to get select options for #{how}=>#{what}.  '#{$!}'")
end

#get_selected_options(browser, how, what) ⇒ Array

Return an array containing the selected options in a select_list identified by its attribute how, and the contents of that attribute what. that uniquely identifies the element.

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.

  • how (Symbol)

    The element attribute used to identify the specific element. Valid values depend on the kind of element. Common values: :text, :id, :title, :name, :class, :href (:link only)

  • what (String, Regexp)

    A string or a regular expression to be found in the how attribute

Returns:

  • (Array)


120
121
122
123
124
125
126
127
128
129
# File 'lib/awetestlib/regression/find.rb', line 120

def get_selected_options(browser, how, what)
  begin
    list = browser.select_list(how, what)
  rescue => e
    unless rescue_me(e, __method__, rescue_me_command(:select_list, how, what), "#{browser.class}")
      raise e
    end
  end
  list.selected_options if list
end

#get_span(browser, how, what, desc = '') ⇒ Watir::Span

Return a reference to a span element identified by the contents what of its attribute how. This differs from get_element in that it waits for the element to exist before trying to return it. that uniquely identifies the element.

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.

  • how (Symbol)

    The element attribute used to identify the specific element. Valid values depend on the kind of element. Common values: :text, :id, :title, :name, :class, :href (:link only)

  • what (String, Regexp)

    A string or a regular expression to be found in the how attribute

  • desc (String) (defaults to: '')

    Contains a message or description intended to appear in the log and/or report output

Returns:

  • (Watir::Span)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/awetestlib/regression/find.rb', line 167

def get_span(browser, how, what, desc = '')
  begin
    Watir::Wait.until { browser.span(how, what).exists? }
  rescue => e
    unless rescue_me(e, __method__, rescue_me_command(:span, how, what, :exists?), "#{browser.class}")
      raise e
    end
  end
  begin
    span = browser.span(how, what)
  rescue => e
    unless rescue_me(e, __method__, rescue_me_command(:span, how, what, :exists?), "#{browser.class}")
      raise e
    end
  end
  passed_to_log("Span #{how}='#{what}' found and returned. #{desc}")
  return span
rescue
  failed_to_log("Unable to return span #{how}='#{what}'. #{desc}: '#{$!}' (#{__LINE__})")
end