Module: Awetestlib::Regression::Find

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

Instance Method Summary collapse

Instance Method Details



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/awetestlib/regression/find.rb', line 373

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_object(browser, obj, how, ord, strg) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/awetestlib/regression/find.rb', line 406

def find_index_for_object(browser, obj, how, ord, strg)
  obj_sym = (obj.to_s.pluralize).to_sym
  how_str = how.to_s
  ptrn    = /#{how}:\s+#{strg}/i
  list    = get_objects(browser, obj_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


389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/awetestlib/regression/find.rb', line 389

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_div(browser, how, what, desc = '', dbg = false) ⇒ Object

:category: A_rdoc_test Returns a reference to a division element. Used to assign a div element to a variable which can then be passed to methods that require a browser parameter.

Parameters

browser - a reference to the browser window or container element to be tested

how - 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 - a string or a regular expression to be found in the how attribute that uniquely identifies the element.

desc - a string containing a message or description intended to appear in the log and/or report output

Example

mainwindow = open_browser('www.myapp.com')  # open a browser to www.google.com
click(mainwindow, :button, :id, 'an id string')  # click a button that opens another browser window
popup = attach_browser(mainwindow, :url, '[url of new window]') *or*
popup = attach_browser(mainwindow, :title, '[title of new window]')


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/awetestlib/regression/find.rb', line 79

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

#get_div_by_class(browser, strg, desc = '', dbg = false) ⇒ Object

:category: A_rdoc_test Returns a reference to a division element identified by the value in its class attribute. Calls get_div()

Parameters

browser - a reference to the browser window or container element to be tested

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

desc - a string containing a message or description intended to appear in the log and/or report output

Example

mainwindow = open_browser('www.myapp.com')  # open a browser to www.google.com
click(mainwindow, :button, :id, 'an id string')  # click a button that opens another browser window
popup = attach_browser(mainwindow, :url, '[url of new window]') *or*
popup = attach_browser(mainwindow, :title, '[title of new window]')


122
123
124
# File 'lib/awetestlib/regression/find.rb', line 122

def get_div_by_class(browser, strg, desc = '', dbg = false)
  get_div(browser, :class, strg, desc, dbg)
end

#get_div_by_id(browser, strg, desc = '', dbg = false) ⇒ Object



97
98
99
# File 'lib/awetestlib/regression/find.rb', line 97

def get_div_by_id(browser, strg, desc = '', dbg = false)
  get_div(browser, :id, strg, desc, dbg)
end

#get_div_by_text(browser, strg, desc = '', dbg = false) ⇒ Object



126
127
128
# File 'lib/awetestlib/regression/find.rb', line 126

def get_div_by_text(browser, strg, desc = '', dbg = false)
  get_div(browser, :text, strg, desc, dbg)
end

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



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/awetestlib/regression/find.rb', line 279

def get_element(browser, element, how, what, value = nil)
  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)
  end
  if target.exists?
    target
  else
    nil
  end
rescue => e
  if not rescue_me(e, __method__, "browser.#{element}(#{how}, '#{what}')", "#{browser.class}", target)
    raise e
  end
end

#get_form(browser, how, strg) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/awetestlib/regression/find.rb', line 130

def get_form(browser, how, strg)
  begin
    #       Watir::Wait.until( browser.form(how, strg).exists? )  # fails in wait_until
  rescue => e
    if not rescue_me(e, __method__, "browser.form(#{how}, '#{strg}').exists?", "#{browser.class}")
      raise e
    end
  end
  myForm = browser.form(how, strg)
  if validate(browser, @myName, __LINE__)
    passed_to_log("Form #{how}='#{strg}' found and returned.")
    return myForm
  end
rescue
  failed_to_log("Unable to return form #{how}='#{strg}': '#{$!}' (#{__LINE__})")
end

#get_form_by_id(browser, strg) ⇒ Object



147
148
149
# File 'lib/awetestlib/regression/find.rb', line 147

def get_form_by_id(browser, strg)
  get_form(browser, :id, strg)
end

#get_frame(browser, how, strg, desc = '') ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/awetestlib/regression/find.rb', line 151

def get_frame(browser, how, strg, desc = '')
  #    begin
  #      Watir::Wait.until(browser.frame(how, strg).exists?) # fails in wait_until
  #    rescue => e
  #      if not rescue_me(e, __method__, "browser.frame(#{how}, '#{strg}').exists?", "#{browser.class}")
  #        raise e
  #      end
  #    end
  begin
    frame = browser.frame(how, strg)
  rescue => e
    if not rescue_me(e, __method__, "browser.frame(#{how}, '#{strg}').exists?", "#{browser.class}")
      raise e
    end
  end
  if validate(browser, @myName, __LINE__)
    passed_to_log("Frame #{how}='#{strg}' found and returned. #{desc}")
    return frame
  end
rescue
  failed_to_log("Unable to return frame #{how}='#{strg}'. #{desc}: '#{$!}' (#{__LINE__})")
end

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



174
175
176
# File 'lib/awetestlib/regression/find.rb', line 174

def get_frame_by_id(browser, strg, desc = '')
  get_frame(browser, :id, strg, desc)
end

#get_frame_by_index(browser, index, desc = '') ⇒ Object



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

def get_frame_by_index(browser, index, desc = '')
  get_frame(browser, :index, index, desc)
end

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



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

def get_frame_by_name(browser, strg, desc = '')
  get_frame(browser, :name, strg, desc)
end

#get_objects(browser, which, dbg = false) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/awetestlib/regression/find.rb', line 327

def get_objects(browser, which, dbg=false)
  cnt = 0
  case which
    when :links
      list = browser.links
      sleep(1)
    when :tables
      list = browser.tables
    when :divs
      list = browser.divs
    when :buttons
      list = browser.buttons
    when :checkboxes
      list = browser.checkboxes
    when :radios
      list = browser.radios
    when :selectlists
      list = browser.selectlists
    when :textfields
      list = browser.textfields
    when :lis
      list = browser.lis
    else
      debug_to_log("Unrecognized 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_ole(element) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
# File 'lib/awetestlib/regression/find.rb', line 361

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 = '') ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/awetestlib/regression/find.rb', line 5

def get_select_list(browser, how, what, desc = '')
  list = browser.select_list(how, what)
  if validate(browser, @myName, __LINE__)
    passed_to_log("Select list #{how}='#{what}' found and returned.")
    return list
  end
rescue
  failed_to_log("Unable to return select list #{how}='#{what}': '#{$!}' (#{__LINE__})")
end

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



15
16
17
18
19
20
21
# File 'lib/awetestlib/regression/find.rb', line 15

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_select_options_by_id(browser, strg, dump = false) ⇒ Object



23
24
25
# File 'lib/awetestlib/regression/find.rb', line 23

def get_select_options_by_id(browser, strg, dump = false)
  get_select_options(browser, :id, strg, dump)
end

#get_select_options_by_name(browser, strg, dump = false) ⇒ Object



27
28
29
# File 'lib/awetestlib/regression/find.rb', line 27

def get_select_options_by_name(browser, strg, dump = false)
  get_select_options(browser, :name, strg, dump)
end

#get_selected_options(browser, how, what) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/awetestlib/regression/find.rb', line 31

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

#get_selected_options_by_id(browser, strg) ⇒ Object Also known as: get_selected_option_by_id



42
43
44
# File 'lib/awetestlib/regression/find.rb', line 42

def get_selected_options_by_id(browser, strg)
  get_selected_options(browser, :id, strg)
end

#get_selected_options_by_name(browser, strg) ⇒ Object Also known as: get_selected_option_by_name



48
49
50
# File 'lib/awetestlib/regression/find.rb', line 48

def get_selected_options_by_name(browser, strg)
  get_selected_options(browser, :name, strg)
end

#get_span(browser, how, strg, desc = '') ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/awetestlib/regression/find.rb', line 186

def get_span(browser, how, strg, desc = '')
  begin
    #TODO: use LegacyExtensions#wait_until
    Watir::Wait.until { browser.span(how, strg).exists? }
  rescue => e
    if not rescue_me(e, __method__, "browser.span(#{how}, '#{strg}').exists?", "#{browser.class}")
      raise e
    end
  end
  begin
    span = browser.span(how, strg)
  rescue => e
    if not rescue_me(e, __method__, "browser.span(#{how}, '#{strg}').exists?", "#{browser.class}")
      raise e
    end
  end
  if validate(browser, @myName, __LINE__)
    passed_to_log("Span #{how}='#{strg}' found and returned. #{desc}")
    return span
  end
rescue
  failed_to_log("Unable to return span #{how}='#{strg}'. #{desc}: '#{$!}' (#{__LINE__})")
end

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



210
211
212
# File 'lib/awetestlib/regression/find.rb', line 210

def get_span_by_id(browser, strg, desc = '')
  get_span(browser, :id, strg, desc)
end

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

:category: A_rdoc_test Returns a reference to a table element. Used to assign a table element to a variable which can then be used directly or passed to methods that require a browser or table parameter.

Parameters

browser - a reference to the browser window or container element to be tested

how - 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 - a string or a regular expression to be found in the how attribute that uniquely identifies the element.

desc - a string containing a message or description intended to appear in the log and/or report output

Example

a_table = get_table(browser, :id, 'table1')
a_table_cell = a_table[2][1]   # The cell in the first column of the second row of the table


237
238
239
240
241
242
243
244
245
246
# File 'lib/awetestlib/regression/find.rb', line 237

def get_table(browser, how, what, desc = '')
  msg = "Return table :#{how}='#{what}'. #{desc}"
  tbl = browser.table(how, what)
  if validate(browser, @myName, __LINE__)
    passed_to_log(msg)
    tbl
  end
rescue
  failed_to_log("#{msg}': '#{$!}'")
end

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



248
249
250
# File 'lib/awetestlib/regression/find.rb', line 248

def get_table_by_id(browser, strg, desc = '')
  get_table(browser, :id, strg, desc)
end

#get_table_by_index(browser, idx) ⇒ Object



252
253
254
# File 'lib/awetestlib/regression/find.rb', line 252

def get_table_by_index(browser, idx)
  get_table(browser, :index, idx, desc)
end

#get_table_by_text(browser, strg) ⇒ Object



256
257
258
# File 'lib/awetestlib/regression/find.rb', line 256

def get_table_by_text(browser, strg)
  get_table(browser, :text, strg, desc)
end

#get_table_headers(table, header_index = 1) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/awetestlib/regression/find.rb', line 260

def get_table_headers(table, header_index = 1)
  headers          = Hash.new
  headers['index'] = Hash.new
  headers['name']  = Hash.new
  count            = 1
  table[header_index].each do |cell|
    if cell.text.length > 0
      name                    = cell.text.gsub(/\s+/, ' ')
      headers['index'][count] = name
      headers['name'][name]   = count
    end
    count += 1
  end
  #debug_to_log("#{__method__}:****** headers:\n#{headers.to_yaml}")
  headers
rescue
  failed_to_log("Unable to get content headers. '#{$!}'")
end