Module: Awetestlib::Regression::Utilities

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

Instance Method Summary collapse

Instance Method Details

#build_message(strg1, desc = '', strg2 = '', strg3 = '') ⇒ Object



46
47
48
49
50
51
# File 'lib/awetestlib/regression/utilities.rb', line 46

def build_message(strg1, desc = '', strg2 = '', strg3 = '')
  msg = "#{strg1}"
  msg << " #{desc}" if desc.length > 0
  msg << " #{strg2}" if strg2.length > 0
  msg << " #{strg3}" if strg3.length > 0
end

#calc_index(index, every = 1) ⇒ Object



95
96
97
# File 'lib/awetestlib/regression/utilities.rb', line 95

def calc_index(index, every = 1)
  (index / every) + (every - 1)
end

#capture_screen(browser, ts) ⇒ Object



543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/awetestlib/regression/utilities.rb', line 543

def capture_screen(browser, ts)
  browser.maximize
  browser.bring_to_front
  caller = get_caller
  caller.match(/:(\d+):/)
  lnbr       = $1
  path       = "#{@myRoot}/screenshot/"
  screenfile = "#{@myName}_#{@myRun.id}_#{lnbr.to_s}_#{ts.to_f.to_s}.scrsht.jpg"
  info_to_log("path:#{path} screenfile:#{screenfile}")
  screenSpec = '"' + path + screenfile + '"'
  screenSpec.gsub!('/', '\\')
  screen_capture(screenSpec)
  screenfile
end

#close_print(title = 'Print', text = '', button = '&Cancel', side = 'left') ⇒ Object

method for cancelling Print window TODO need to handle ‘Cancel’ and ‘&Cancel’ (first character underlined)



767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
# File 'lib/awetestlib/regression/utilities.rb', line 767

def close_print(title = 'Print', text = '', button = '&Cancel', side = 'left')
  msg = "Popup: title=#{title} button='#{button}' text='#{text}' side='#{side}':"
  if @ai.WinWait(title, text, WAIT)
    passed_to_log("#{msg} found.")
    @ai.WinActivate(title)
    if @ai.WinActive(title, text)
      passed_to_log("#{msg} activated.")
      if @ai.ControlFocus(title, text, button)
        passed_to_log("#{msg} focus attained.")
        if @ai.ControlClick(title, text, button, side)
          passed_to_log("#{msg} closed successfully.")
        else
          failed_to_log("#{msg} click failed on button (#{__LINE__})")
        end
      else
        failed_to_log("#{msg} Unable to gain focus on button (#{__LINE__})")
      end
    else
      failed_to_log("#{msg} Unable to activate (#{__LINE__})")
    end
  else
    failed_to_log("#{msg} did not appear after #{WAIT} seconds. (#{__LINE__})")
  end
rescue
  failed_to_log("Close #{msg}: '#{$!}'. (#{__LINE__})")
end

#debug_call_list(msg) ⇒ Object



223
224
225
226
# File 'lib/awetestlib/regression/utilities.rb', line 223

def debug_call_list(msg)
  call_array = get_call_array
  debug_to_log("#{msg}\n#{dump_array(call_array)}")
end

#dump_all_tables(browser, to_report = false) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/awetestlib/regression/utilities.rb', line 463

def dump_all_tables(browser, to_report = false)
  tables  = browser.tables
  msg     = ''
  tbl_cnt = 0
  tables.each do |tbl|
    tbl_cnt += 1
    row_cnt = 0
    msg <<"\n=================\ntable: #{tbl_cnt}\n=================\n#{tbl}\ntext:\n#{tbl.text}"
    tbl.rows.each do |row|
      row_cnt  += 1
      cell_cnt = 0
      msg <<"\n=================\ntable: #{tbl_cnt} row: #{row_cnt}\n#{row.inspect}\n#{row}\ntext:'#{row.text}'"
      row.each do |cell|
        cell_cnt += 1
        msg <<"\ncell: #{cell_cnt}\n#{cell.inspect}\n#{row}\ntext: '#{cell.text}'"
      end
    end
  end
  if to_report
    debug_to_report(msg)
  else
    debug_to_log(msg)
  end
end

#dump_array(arr, space_to_underscore = false) ⇒ Object



406
407
408
409
410
411
412
413
414
# File 'lib/awetestlib/regression/utilities.rb', line 406

def dump_array(arr, space_to_underscore = false)
  dump = "  #{arr.inspect}\n"
  arr.each_index do |x|
    value = arr[x].to_s
    value.gsub!(/\s/, '_') if space_to_underscore
    dump << " #{x.to_s.rjust(5)}>> '#{arr[x].to_s}'\n"
  end
  dump
end

#dump_ole_get_methods(ole) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
# File 'lib/awetestlib/regression/utilities.rb', line 428

def dump_ole_get_methods(ole)
  rtrn = ''
  ole.ole_get_methods.each do |m|
    prms = ''
    m.params.each do |p|
      prms << "#{p}, "
    end
    rtrn << "#{m.name}(#{prms.chop.chop})\n"
  end
  rtrn
end

#dump_ole_help(ole) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
# File 'lib/awetestlib/regression/utilities.rb', line 440

def dump_ole_help(ole)
  rtrn = ''
  ole.ole_obj_help.each do |m|
    prms = ''
    m.params.each do |p|
      prms << "#{p}, "
    end
    rtrn << "#{m.name}(#{prms.chop.chop})\n"
  end
  rtrn
end

#dump_ole_methods(ole) ⇒ Object



416
417
418
419
420
421
422
423
424
425
426
# File 'lib/awetestlib/regression/utilities.rb', line 416

def dump_ole_methods(ole)
  rtrn = ''
  ole.ole_methods.each do |m|
    prms = ''
    m.params.each do |p|
      prms << "#{p}, "
    end
    rtrn << "#{m.name}(#{prms.chop.chop})\n"
  end
  rtrn
end

#dump_row_cells(row) ⇒ Object



521
522
523
524
525
526
527
528
529
530
# File 'lib/awetestlib/regression/utilities.rb', line 521

def dump_row_cells(row)
  msg      = ''
  cell_cnt = 0
  msg <<"\n=================\nrow: #{row.inspect}\n#{row}\ntext:'#{row.text}'"
  row.each do |cell|
    cell_cnt += 1
    msg <<"\ncell: #{cell_cnt}\n#{cell.inspect}\n#{row}\ntext: '#{cell.text}'"
  end
  debug_to_log(msg)
end

#dump_select_list_options(element) ⇒ Object



452
453
454
455
456
457
458
459
460
461
# File 'lib/awetestlib/regression/utilities.rb', line 452

def dump_select_list_options(element)
  msg     = "#{element.inspect}"
  options = element.options
  cnt     = 1
  options.each do |o|
    msg << "\n\t#{cnt}:\t'#{o}"
    cnt += 1
  end
  debug_to_log(msg)
end

#dump_table_and_rows(table, to_report = false) ⇒ Object



488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/awetestlib/regression/utilities.rb', line 488

def dump_table_and_rows(table, to_report = false)
  msg = "\n=================\ntable\n=================\nn#{table}\n#{table.to_yaml}\nrows:"
  cnt = 0
  table.rows.each do |r|
    cnt += 1
    msg << "\n#{cnt}: #{r.text}"
  end
  msg << "\n=================\n================="
  if to_report
    debug_to_report(msg)
  else
    debug_to_log(msg)
  end
end

#dump_table_rows_and_cells(tbl) ⇒ Object Also known as: dump_table_rows



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/awetestlib/regression/utilities.rb', line 503

def dump_table_rows_and_cells(tbl)
  msg     = ''
  row_cnt = 0
  msg <<"\n=================\ntable: #{tbl.inspect}\n=================\n#{tbl}\ntext:\n#{tbl.text}"
  tbl.rows.each do |row|
    row_cnt  += 1
    cell_cnt = 0
    msg <<"\n=================\nrow: #{row_cnt}\n#{row.inspect}\n#{row}\ntext:'#{row.text}'"
    row.each do |cell|
      cell_cnt += 1
      msg <<"\ncell: #{cell_cnt}\n#{cell.inspect}\n#{row}\ntext: '#{cell.text}'"
    end
  end
  debug_to_log(msg)
end

#file_download(browser = nil) ⇒ Object

method for handling file download dialog use click_no_wait on the action that triggers the save dialog TODO need version for Firefox TODO need to handle ‘Cancel’ and ‘&Cancel’ (first character underlined) TODO replace call to close_modal_ie with actual file download



799
800
801
802
803
804
805
806
807
808
809
# File 'lib/awetestlib/regression/utilities.rb', line 799

def file_download(browser = nil)
  title  = 'File Download'
  title  = translate_popup_title(title)
  text   = ''
  button = 'Cancel'
  if @browserAbbrev == 'IE'
    close_popup(title, button, text)
  else

  end
end

#file_upload(filepath) ⇒ Object

method for handling file upload dialog use click_no_wait on the action that triggers the save dialog TODO need version for Firefox



814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
# File 'lib/awetestlib/regression/utilities.rb', line 814

def file_upload(filepath)
  title   = 'Choose File'
  title   = translate_popup_title(title)
  text    = ''
  button  = "&Open"
  control = "Edit1"
  side    = 'primary'
  msg     = "Window title=#{title} button='#{button}' text='#{text}' side='#{side}':"
  begin
    if @ai.WinWait(title, text, WAIT)
      passed_to_log("#{msg} found.")
      @ai.WinActivate(title, text)
      if @ai.WinActive(title, text)
        passed_to_log("#{msg} activated.")
        if @ai.ControlSend(title, text, control, filepath)
          passed_to_log("#{msg} #{control} command sent.")
          sleep_for 1
          if @ai.ControlClick(title, text, button, "primary")
            passed_to_log("#{msg} Upload of #{filepath} succeeded.")
          else
            failed_to_log("#{msg} Upload of #{filepath} failed. (#{__LINE__})")
          end
        else
          failed_to_log("#{msg} Unable to select #{filepath}. (#{__LINE__})")
        end
      else
        failed_to_log("#{msg} Unable to activate. (#{__LINE__})")
      end
    else
      failed_to_log("#{msg} did not appear after #{WAIT} seconds. (#{__LINE__})")
    end
  rescue
    failed_to_log("#{msg} Unable to upload: '#{$!}'. (#{__LINE__})")
  end

end

#find_sheet_with_name(workbook, sheet_name) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/awetestlib/regression/utilities.rb', line 242

def find_sheet_with_name(workbook, sheet_name)
  sheets = workbook.sheets
  idx    = 0
  found  = false
  sheets.each do |s|
    if s == sheet_name
      found = true
      break
    end
    idx += 1
  end
  if found
    idx
  else
    -1
  end
end

#flash(element, count = 4) ⇒ Object



585
586
587
588
589
590
591
# File 'lib/awetestlib/regression/utilities.rb', line 585

def flash(element, count = 4)
  element.flash(count)
  debug_to_log("'#{element.inspect}' flashed #{count} times.")
  true
rescue
  debug_to_log("Flash '#{element.inspect}' failed: '#{$!}' (#{__LINE__})")
end

#flash_id(browser, strg, count) ⇒ Object



573
574
575
576
577
578
579
580
581
582
583
# File 'lib/awetestlib/regression/utilities.rb', line 573

def flash_id(browser, strg, count)
  msg = "Flash link id='#{strg}' #{count} times."
  msg << " #{desc}" if desc.length > 0
  browser.link(:id, strg).flash(count)
  if validate(browser, @myName, __LINE__)
    passed_to_log(msg)
    true
  end
rescue
  failed_to_log("Unable to #{msg} '#{$!}'")
end

#flash_text(browser, strg, count, desc = '') ⇒ Object



882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
# File 'lib/awetestlib/regression/utilities.rb', line 882

def flash_text(browser, strg, count, desc = '')
  msg = "Flash link text='#{strg}' #{count} times."
  msg << " #{desc}" if desc.length > 0
  strgCnt = string_count_in_string(browser.text, strg)
  if strgCnt > 0
    browser.link(:text, strg).flash(count)
    if validate(browser, @myName, __LINE__)
      passed_to_log(msg)
      true
    end
  else
    failed_to_log("#{msg} Link not found.")
  end
rescue
  failed_to_log("Unable to #{msg} '#{$!}'")
end

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



867
868
869
870
871
872
873
874
875
876
877
878
879
880
# File 'lib/awetestlib/regression/utilities.rb', line 867

def focus_on_textfield_by_id(browser, strg, desc = '')
  msg = "Set focus on textfield name='#{strg}' "
  msg << " #{desc}" if desc.length > 0
  tf = browser.text_field(:id, strg)
  if validate(browser, @myName, __LINE__)
    tf.focus
    if validate(browser, @myName, __LINE__)
      passed_to_log(msg)
      true
    end
  end
rescue
  failed_to_log("Unable to #{msg} '#{$!}'")
end

#get_call_array(depth = 9) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
# File 'lib/awetestlib/regression/utilities.rb', line 375

def get_call_array(depth = 9)
  arr       = []
  call_list = Kernel.caller
  call_list.each_index do |x|
    myCaller = call_list[x].to_s
    break if x > depth or myCaller =~ /:in .run.$/
    myCaller =~ /([\(\)\w_\_\-\.]+\:\d+\:?.*?)$/
    arr << $1.gsub(/eval/, @myName)
  end
  arr
end

#get_call_list(depth = 9, dbg = false) ⇒ Object Also known as: get_callers



333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/awetestlib/regression/utilities.rb', line 333

def get_call_list(depth = 9, dbg = false)
  myList    = []
  call_list = Kernel.caller
  puts call_list if dbg
  call_list.each_index do |x|
    myCaller = call_list[x].to_s
    break if x > depth or myCaller =~ /:in .run.$/
    myCaller =~ /([\(\)\w_\_\-\.]+\:\d+\:?.*?)$/
    myList << "[#{$1.gsub(/eval/, @myName)}] "
  end
  myList
end

#get_call_list_new(depth = 9, dbg = false) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/awetestlib/regression/utilities.rb', line 348

def get_call_list_new(depth = 9, dbg = false)
  myList    = []
  call_list = Kernel.caller
  puts call_list if dbg
  call_list.each_index do |x|
    myCaller = call_list[x].to_s
    break if x > depth or myCaller =~ /:in .run.$/
    if myCaller.include? @myName
      myCaller =~ /([\(\)\w_\_\-\.]+\:\d+\:?.*?)$/
      myList << "[#{$1.gsub(/eval/, @myName)}] "
      break
    end
  end
  if @projName
    call_list.each_index do |x|
      myCaller = call_list[x].to_s
      break if x > depth or myCaller =~ /:in .run.$/
      if myCaller.include? @projName
        myCaller =~ /([\(\)\w_\_\-\.]+\:\d+\:?.*?)$/
        myList << "[#{$1.gsub(/eval/, @projName)}] "
        break
      end
    end
  end
  myList
end

#get_caller_lineObject



327
328
329
330
331
# File 'lib/awetestlib/regression/utilities.rb', line 327

def get_caller_line
  last_caller = get_callers[0]
  line        = last_caller.split(':', 3)[1]
  line
end

#get_debug_list(dbg = false) ⇒ Object



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

def get_debug_list(dbg = false)
  calls = get_call_array(10)
  puts "#{calls.to_yaml}" if dbg
  arr = []
  calls.each_index do |ix|
    if ix > 1 # skip this method and the logging method
      arr << calls[ix]
    end
  end
  puts "#{arr.to_yaml}" if dbg
  if arr.length > 0
    list = 'TRACE:'
    arr.reverse.each { |l| list << "=>#{l}" }
    " [[#{list}]]"
  else
    nil
  end
end

#get_mdyy(t = Time.now) ⇒ Object



63
64
65
# File 'lib/awetestlib/regression/utilities.rb', line 63

def get_mdyy(t = Time.now)
  "#{t.month}/#{t.day}/#{t.year}"
end

#get_prefix(strg, offset) ⇒ Object



67
68
69
70
# File 'lib/awetestlib/regression/utilities.rb', line 67

def get_prefix(strg, offset)
  a_slice = strg.slice(0, offset)
  a_slice.downcase
end

#get_save_file_path(root, filename) ⇒ Object



593
594
595
596
# File 'lib/awetestlib/regression/utilities.rb', line 593

def get_save_file_path(root, filename)
  filespec = "#{root}/file/#{filename}"
  filespec.gsub!('/', '\\')
end

#get_timestamp(format = 'long', offset = nil, offset_unit = :years) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/awetestlib/regression/utilities.rb', line 72

def get_timestamp(format = 'long', offset = nil, offset_unit = :years)
  t = DateTime.now
  if offset
    t = t.advance(offset_unit => offset)
  end
  case format
    when 'dateonly'
      t.strftime("%m/%d/%Y")
    when 'condensed'
      t.strftime("%Y%m%d%H%M")
    when 'condensed_seconds'
      t.strftime("%Y%m%d%H%M%S")
    when 'long'
      t.strftime("%m/%d/%Y %I:%M %p")
    when 'mdyy'
      get_mdyy(t)
    when 'm/d/y'
      get_mdyy(t)
    else
      Time.now.strftime("%m/%d/%Y %H:%M:%S")
  end
end

#get_trace(lnbr) ⇒ Object Also known as: dump_caller



53
54
55
56
57
58
59
# File 'lib/awetestlib/regression/utilities.rb', line 53

def get_trace(lnbr)
  callertrace = "\nCaller trace: (#{lnbr})\n"
  Kernel.caller.each_index do |x|
    callertrace << '    >> ' + Kernel.caller[x].to_s + "\n"
  end
  callertrace
end

#get_variables(file, login = :role, dbg = true) ⇒ Object



99
100
101
102
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/awetestlib/regression/utilities.rb', line 99

def get_variables(file,  = :role, dbg = true)
  debug_to_log("#{__method__}: file = #{file}")
  debug_to_log("#{__method__}: role = #{}")

  @var                   = Hash.new
  workbook               = Excel.new(file)
  data_index             = find_sheet_with_name(workbook, 'Data')
  workbook.default_sheet = workbook.sheets[data_index]
  var_col                = 0

  2.upto(workbook.last_column) do |col|
    scriptName = workbook.cell(1, col)
    if scriptName == @myName
      var_col = col
      break
    end
  end

  2.upto(workbook.last_row) do |line|
    name       = workbook.cell(line, 'A')
    value      = workbook.cell(line, var_col).to_s.strip
    @var[name] = value
  end

  @var.keys.sort.each do |name|
    message_tolog("@var #{name}: '#{@var[name]}'")
  end if dbg

  @login       = Hash.new
      = 0
  role_col     = 0
  userid_col   = 0
  password_col = 0
  url_col      = 0
  name_col     = 0
  role_index   = find_sheet_with_name(workbook, 'Login')
  if role_index >= 0
    workbook.default_sheet = workbook.sheets[role_index]

    1.upto(workbook.last_column) do |col|
      a_cell = workbook.cell(1, col)
      case a_cell
        when @myName
           = col
          break
        when 'role'
          role_col = col
        when 'userid'
          userid_col = col
        when 'password'
          password_col = col
        when 'url'
          url_col = col
        when 'name'
          name_col = col
      end
    end

    2.upto(workbook.last_row) do |line|
      role     = workbook.cell(line, role_col)
      userid   = workbook.cell(line, userid_col)
      password = workbook.cell(line, password_col)
      url      = workbook.cell(line, url_col)
      username = workbook.cell(line, name_col)
      enabled  = workbook.cell(line, ).to_s

      case 
        when :id
          key = userid
        when :role
          key = role
        else
          key = role
      end

      @login[key]             = Hash.new
      @login[key]['role']     = role
      @login[key]['userid']   = userid
      @login[key]['password'] = password
      @login[key]['url']      = url
      @login[key]['name']     = username
      @login[key]['enabled']  = enabled

    end

    @login.keys.sort.each do |key|
      message_tolog("@login (by #{}): #{key}=>'#{@login[key].to_yaml}'")
    end if dbg
  end

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

#grab_window_list(strg) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/awetestlib/regression/utilities.rb', line 205

def grab_window_list(strg)
  @ai.AutoItSetOption("WinTitleMatchMode", 2)
  list    = @ai.WinList(strg)
  stuff   = ''
  names   = list[0]
  handles = list[1]
  max     = names.length - 1
  rng     = Range.new(0, max)
  rng.each do |idx|
    window_handle = "[HANDLE:#{handles[idx]}]"
    full_text     = @ai.WinGetText(window_handle)
    stuff << "[#{handles[idx]}]=>#{names[idx]}=>'#{full_text}'\n"
  end
  debug_to_log("\n#{stuff}")
  @ai.AutoItSetOption("WinTitleMatchMode", 1)
  stuff
end

#nice_array(arr, space_to_underscore = false) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
# File 'lib/awetestlib/regression/utilities.rb', line 260

def nice_array(arr, space_to_underscore = false)
  new_arr = Array.new
  if space_to_underscore
    arr.each do |nty|
      new_arr << nty.gsub(/\s/, '_')
    end
  else
    new_arr = arr
  end
  "['#{new_arr.join("','")}']"
end

#parse_cookies(browser) ⇒ Object



532
533
534
535
536
537
538
539
540
541
# File 'lib/awetestlib/regression/utilities.rb', line 532

def parse_cookies(browser)
  cookies = Hash.new
  strg    = browser.document.cookie
  ary     = strg.split(';')
  ary.each do |c|
    key, value          = c.split('=')
    cookies[key.lstrip] = value
  end
  cookies
end

#pdf_to_text(file, noblank = true) ⇒ Object



558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/awetestlib/regression/utilities.rb', line 558

def pdf_to_text(file, noblank = true)
  spec = file.sub(/\.pdf$/, '')
  `pdftotext #{spec}.pdf`
  file = File.new("#{spec}.txt")
  text = []
  file.readlines.each do |l|
    l.chomp! if noblank
    if l.length > 0
      text << l
    end
  end
  file.close
  text
end

#rescue_me(e, me = nil, what = nil, where = nil, who = nil) ⇒ Object



277
278
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/utilities.rb', line 277

def rescue_me(e, me = nil, what = nil, where = nil, who = nil)
  #TODO: these are rescues from exceptions raised in Watir/Firewatir
  debug_to_log("#{__method__}: Begin rescue")
  ok = false
  begin
    gaak    = who.inspect
    located = gaak =~ /located=true/i
  rescue
    debug_to_log("#{__method__}: gaak: '#{gaak}'")
  end
  msg = e.message
  debug_to_log("#{__method__}: msg = #{msg}")
  if  msg =~ /undefined method\s+.join.\s+for/i # firewatir to_s implementation error
    ok = true
  elsif msg =~ /undefined method\s+.match.\s+for.+WIN32OLERuntimeError/i # watir and firewatir
    ok = true
  elsif msg =~ /undefined method\s+.match.\s+for.+UnknownObjectException/i # watir
    ok = true
  elsif msg =~ /window\.getBrowser is not a function/i # firewatir
    ok = true
  elsif msg =~ /WIN32OLERuntimeError/i # watir
    ok = true
  elsif msg =~ /undefined method\s+.match.\s+for/i # watir
    ok = true
  elsif msg =~ /wrong number of arguments \(1 for 0\)/i
    ok = true
  elsif (msg =~ /unable to locate element/i)
    if located
      ok = true
    elsif where == 'Watir::Div'
      ok = true
    end
  elsif (msg =~ /HRESULT error code:0x80070005/)
    ok = true
                                                #elsif msg =~ /missing\s+\;\s+before statement/
                                                #  ok = true
  end
  if ok
    debug_to_log("#{__method__}: RESCUED: \n#{who.to_yaml}=> #{what} in #{me}()\n=> '#{$!}'")
    debug_to_log("#{__method__}: #{who.inspect}") if who
    debug_to_log("#{__method__}: #{where.inspect}")
    debug_to_log("#{__method__}: #{get_callers(6, true)}")
  else
    debug_to_log("#{__method__}: NO RESCUE: #{e.message}")
    debug_to_log("#{__method__}: NO RESCUE: \n#{get_callers(6, true)}")
  end
  debug_to_log("#{__method__}: Exit")
  ok
end

#save_file(filepath, download_title = "File Download - Security Warning") ⇒ Object

method for handling save dialog use click_no_wait on the action that triggers the save dialog



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# File 'lib/awetestlib/regression/utilities.rb', line 677

def save_file(filepath, download_title = "File Download - Security Warning")
  # TODO need version for Firefox
  # TODO need to handle first character underline, e.g. 'Cancel' and '&Cancel'
  download_title   = translate_popup_title(download_title)
  download_text    = ''
  download_control = "&Save"
  saveas_title     = 'Save As'
  saveas_text      = ''
  saveas_control   = "Edit1"
  dnld_cmplt_title = "Download Complete"
  dnld_cmplt_title = translate_popup_title(dnld_cmplt_title)
  dnld_cmplt_text  = ""
  #    save_title = ""
  side             = 'primary'
  msgdl            = "Window '#{download_title}':"
  msgsa            = "Window '#{saveas_title}':"
  msgdc            = "Window '#{dnld_cmplt_title}':"
  begin
    if @ai.WinWait(download_title, download_text, WAIT)
      @ai.WinActivate(download_title, download_text)
      if @ai.WinActive(download_title, download_text)
        dl_title = @ai.WinGetTitle(download_title, download_text)
        #          dl_hndl  = @ai.WinGetHandle(download_title, download_text)
        #          dl_text  = @ai.WinGetText(download_title, download_text)
        #          dl_sv_hndl = @ai.ControlGetHandle(dl_title, '', download_control)
        #          dl_op_hndl = @ai.ControlGetHandle(dl_title, '', '&Open')
        #          dl_cn_hndl = @ai.ControlGetHandle(dl_title, '', 'Cancel')
        debug_to_log("#{msgdl} activated. (#{__LINE__})")

        if @ai.ControlFocus(dl_title, download_text, download_control)
          debug_to_log("#{msgdl} focus gained. (#{__LINE__})")

          @ai.Send("S")
          #              @ai.ControlSend(dl_Stitle, download_text, download_control, "{ENTER}")
          sleep_for 1

          if @ai.ControlClick(dl_title, download_text, download_control, side)
            debug_to_log("#{msgdl} click succeeded on '#{download_control}'. (#{__LINE__})")

            if @ai.WinWait(saveas_title, saveas_text, WAIT)
              debug_to_log("#{msgsa} appeared. (#{__LINE__})")
              sleep_for 1
              if @ai.ControlSend(saveas_title, saveas_text, saveas_control, filepath)
                debug_to_log("#{msgsa} controlsend of '#{saveas_control}' succeeded. (#{__LINE__})")

                @ai.Send("S")
                @ai.ControlSend(saveas_title, saveas_text, saveas_control, "{ENTER}")
                sleep_for 1

                if @ai.ControlClick(saveas_title, saveas_text, saveas_control, side)
                  passed_to_log("#{msgsa} click succeeded on '#{saveas_control}'. (#{__LINE__})")
                  if @ai.WinWait(dnld_cmplt_title, dnld_cmplt_text, WAIT)
                    debug_to_log("#{msgdc} appeared. (#{__LINE__})")
                    sleep_for 1
                    if @ai.ControlClick(dnld_cmplt_title, dnld_cmplt_text, "Close", side)
                      passed_to_log("Save file for #{filepath} succeeded.")
                    else
                      failed_to_log("#{msgdc} click failed on 'Close'. (#{__LINE__})")
                    end
                  else
                    failed_to_log("#{msgdc} did not appear after #{WAIT} seconds. (#{__LINE__})")
                  end
                else
                  failed_to_log("#{msgsa} click failed on '#{saveas_control}'. (#{__LINE__})")
                end
              else
                failed_to_log("#{msgsa} controlsend of '#{saveas_control}' failed. (#{__LINE__})")
              end
            else
              failed_to_log("#{msgsa} did not appear after #{WAIT} seconds. (#{__LINE__})")
            end
          else
            failed_to_log("#{msgdl} click failed on '#{download_control}'. (#{__LINE__})")
          end
        else
          failed_to_log("#{msgdl} Unable to gain focus on control '#{dl_title}'. (#{__LINE__})")
        end
      else
        failed_to_log("#{msgdl} Unable to activate. (#{__LINE__})")
      end
    else
      failed_to_log("#{msgdl} did not appear after #{WAIT} seconds. (#{__LINE__})")
    end
  rescue
    failed_to_log("Save file failed: '#{$!}'. (#{__LINE__})")
  end
end

#save_file1(filepath, title = "File Download", desc = '', wait = WAIT) ⇒ Object

TODO This and save_file2 have to be combined somehow.



614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'lib/awetestlib/regression/utilities.rb', line 614

def save_file1(filepath, title = "File Download", desc = '', wait = WAIT)
  title = translate_popup_title(title)
  @ai.WinWait(title, '', wait)
  @ai.WinActivate(title, '')
  sleep 1
  @ai.ControlFocus(title, "", "&Save")
  sleep 3
  @ai.ControlClick(title, "", "&Save", "primary")
  sleep 2
  @ai.ControlClick(title, "", "Save", "primary")

  @ai.WinWait("Save As", "", wait)
  sleep 1
  @ai.ControlSend("Save As", "", "Edit1", filepath)
  @ai.ControlFocus("Save As", "", "&Save")
  @ai.ControlClick("Save As", "", "&Save", "primary")
  @ai.ControlClick("Save As", "", "Save", "primary")

  @ai.WinWait("Download complete", "", wait)
  passed_to_log("Save file '#{filepath}' succeeded. #{desc}")
  @ai.ControlClick("Download complete", "", "Close")
rescue
  failed_to_log("Save file failed: #{desc} '#{$!}'. (#{__LINE__})")
end

#save_file2(filepath, title = "File Download - Security Warning", desc = '', wait = WAIT) ⇒ Object



639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# File 'lib/awetestlib/regression/utilities.rb', line 639

def save_file2(filepath, title = "File Download - Security Warning", desc = '', wait = WAIT)
  title = translate_popup_title(title)
  sleep(1)
  @ai.WinWait(title, '', wait)
  dl_hndl    = @ai.WinGetHandle(title, '')
  dl_sv_hndl = @ai.ControlGetHandle(title, '', "&Save")
  @ai.WinActivate(title, '')
  sleep 1
  @ai.ControlFocus(title, "", "&Save")
  sleep 1
  @ai.ControlFocus(title, "", "Save")
  sleep 1
  @ai.ControlClick(title, "", "&Save", "primary")
  sleep 1
  @ai.ControlClick(title, "", "Save", "primary")
  sleep 1
  w = WinClicker.new
  w.clickButtonWithHandle(dl_sv_hndl)
  sleep 1
  w.clickWindowsButton_hwnd(dl_hndl, "Save")
  sleep 1
  w.clickWindowsButton_hwnd(dl_hndl, "&Save")

  @ai.WinWait("Save As", "", wait)
  sleep 1
  @ai.ControlSend("Save As", "", "Edit1", filepath)
  @ai.ControlFocus("Save As", "", "&Save")
  @ai.ControlClick("Save As", "", "&Save", "primary")

  @ai.WinWait("Download complete", "", wait)
  passed_to_log("Save file '#{filepath}' succeeded. #{desc}")
  @ai.ControlClick("Download complete", "", "Close")
rescue
  failed_to_log("Save file failed: #{desc} '#{$!}'. (#{__LINE__})")
end

#save_file_orig(filepath, desc = '', wait = WAIT) ⇒ Object



598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/awetestlib/regression/utilities.rb', line 598

def save_file_orig(filepath, desc = '', wait = WAIT)
  #    title = translate_popup_title(title)
  @ai.WinWait("File Download", "", wait)
  @ai.ControlFocus("File Download", "", "&Save")
  sleep 1
  @ai.ControlClick("File Download", "", "&Save", "left")
  @ai.WinWait("Save As", "", wait)
  sleep 1
  @ai.ControlSend("Save As", "", "Edit1", filepath)
  @ai.ControlClick("Save As", "", "&Save", "left")
  sleep 1
  @ai.WinWait("Download complete", "", wait)
  @ai.ControlClick("Download complete", "", "Close")
end

#sec2hms(s) ⇒ Object



228
229
230
# File 'lib/awetestlib/regression/utilities.rb', line 228

def sec2hms(s)
  Time.at(s.to_i).gmtime.strftime('%H:%M:%S')
end

#set_script_variablesObject

Place holder to prevent method not found error in scripts



6
7
8
# File 'lib/awetestlib/regression/utilities.rb', line 6

def set_script_variables
  # TODO: replace with method_missing?
end

#setupObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/awetestlib/regression/utilities.rb', line 10

def setup
  #    if @os_sysname =~ /Windows.+Server\s+2003/
  ##  'Microsoft(R) Windows(R) Server 2003, Enterprise Edition'
  #      @vertical_hack_ie   = 110
  #      @vertical_hack_ff   = 138
  #      @horizontal_hack_ie = 5
  #      @horizontal_hack_ff = 4
  #    elsif @os_sysname =~ /Windows XP Professional/
  #  'Microsoft Windows XP Professional'
  @vertical_hack_ie        = 118
  @vertical_hack_ff        = 144
  @horizontal_hack_ie      = 5
  @horizontal_hack_ff      = 4
  #end

  @settings_display_ids    = Hash[
      "Currency"       => "row-currencyName",
      "Description"    => "row-description",
      "Tx Date"        => "row-fmtDate",
      "Total"          => "row-amount",
      "[currencyCode]" => "row-currencyCode",
      "Date in Millis" => "row-dateInMilliseconds",
  ]
  @column_data_display_ids = Hash[
      "Currency"       => "yui-dt0-th-currencyName",
      "Description"    => "yui-dt0-th-description",
      "Tx Date"        => "yui-dt0-th-fmtDate",
      "Total"          => "yui-dt0-th-fmtDate",
      "[currencyCode]" => "yui-dt0-th-currencyCode",
      "Date in Millis" => "yui-dt0-th-dateInMilliseconds",
  ]
  @settings_panel_index    = 0
  @x_tolerance             = 4
  @y_tolerance             = 4
end

#string_count_in_string(strg, substrg) ⇒ Object



272
273
274
275
# File 'lib/awetestlib/regression/utilities.rb', line 272

def string_count_in_string(strg, substrg)
  count = strg.scan(substrg).length
  count
end

#translate_var_list(key) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/awetestlib/regression/utilities.rb', line 193

def translate_var_list(key)
  if @var[key] and @var[key].length > 0
    list = @var[key].dup
    unless list =~ /^\[.+\]$/
      list = "[#{list}]"
    end
    eval(list)
  end
rescue
  failed_to_log("#{__method__}: '#{$!}'")
end

#upload_file(data_path) ⇒ Object



851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
# File 'lib/awetestlib/regression/utilities.rb', line 851

def upload_file(data_path)
  limit = 180 # .seconds
  Timeout::timeout(limit) {
    wait = 20
    @ai.WinWait("Choose File to Upload", "", wait)
    sleep 1
    @ai.ControlSend("Choose File to Upload", "", "Edit1", data_path)
    @ai.ControlClick("Choose File to Upload", "", "[CLASS:Button; INSTANCE:2]", "left")
    sleep 4
    #sleep 1
  }
  failed_to_log("Choose File to Upload not found after #{limit} '#{$!}'")
rescue Timeout::Error
  failed_to_log("File Upload timeout after #{limit} '#{$!}'")
end