Module: Awetestlib::Regression::Utilities

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

Overview

Miscellaneous helper methods. Includes file save/upload as well as debug methods. Rdoc work in progress.

Instance Method Summary collapse

Instance Method Details

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



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

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

#calc_index(index, every = 1) ⇒ Object



104
105
106
# File 'lib/awetestlib/regression/utilities.rb', line 104

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

#capture_screen(browser, ts) ⇒ Object



567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/awetestlib/regression/utilities.rb', line 567

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)



790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
# File 'lib/awetestlib/regression/utilities.rb', line 790

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



243
244
245
246
# File 'lib/awetestlib/regression/utilities.rb', line 243

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

#do_taskkill(severity, pid) ⇒ Object



917
918
919
920
921
922
923
924
# File 'lib/awetestlib/regression/utilities.rb', line 917

def do_taskkill(severity, pid)
  if pid and pid > 0 and pid < 538976288
    info_to_log("Executing taskkill for pid #{pid}")
    log_message(severity, %x[taskkill /t /f /pid #{pid}])
  end
rescue
  error_to_log("#{$!}  (#{__LINE__})")
end

#dump_all_tables(browser, to_report = false) ⇒ Object



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/awetestlib/regression/utilities.rb', line 487

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



426
427
428
429
430
431
432
433
434
# File 'lib/awetestlib/regression/utilities.rb', line 426

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



448
449
450
451
452
453
454
455
456
457
458
# File 'lib/awetestlib/regression/utilities.rb', line 448

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



460
461
462
463
464
465
466
467
468
469
470
# File 'lib/awetestlib/regression/utilities.rb', line 460

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



436
437
438
439
440
441
442
443
444
445
446
# File 'lib/awetestlib/regression/utilities.rb', line 436

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



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

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, report = false) ⇒ Object



472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/awetestlib/regression/utilities.rb', line 472

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

#dump_table_and_rows(table, to_report = false) ⇒ Object



512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/awetestlib/regression/utilities.rb', line 512

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



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

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



822
823
824
825
826
827
828
829
830
831
832
# File 'lib/awetestlib/regression/utilities.rb', line 822

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



837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/awetestlib/regression/utilities.rb', line 837

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



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

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



608
609
610
611
612
613
614
# File 'lib/awetestlib/regression/utilities.rb', line 608

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

Deprecated.


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

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)
  passed_to_log(msg)
  true
rescue
  failed_to_log("Unable to #{msg} '#{$!}'")
end

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

Deprecated.


902
903
904
905
906
907
908
909
910
911
912
913
914
915
# File 'lib/awetestlib/regression/utilities.rb', line 902

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)
    passed_to_log(msg)
    true
  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



890
891
892
893
894
895
896
897
898
899
# File 'lib/awetestlib/regression/utilities.rb', line 890

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)
  tf.focus
  passed_to_log(msg)
  true
rescue
  failed_to_log("Unable to #{msg} '#{$!}'")
end

#get_call_array(depth = 9) ⇒ Object



395
396
397
398
399
400
401
402
403
404
405
# File 'lib/awetestlib/regression/utilities.rb', line 395

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

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



353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/awetestlib/regression/utilities.rb', line 353

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
    myCaller =~ /([\(\)\w_\_\-\.]+\:\d+\:?.*?)$/
    myList << "[#{$1.gsub(/eval/, @myName)}] "
    break if x > depth or myCaller =~ /:in .run.$/
  end
  myList
end

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



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/awetestlib/regression/utilities.rb', line 368

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
    if myCaller.include? @myName
      myCaller =~ /([\(\)\w_\_\-\.]+\:\d+\:?.*?)$/
      myList << "[#{$1.gsub(/eval/, @myName)}] "
      break
    end
    break if x > depth or myCaller =~ /:in .run.$/  # this break causes error in Ruby 1.9.x
  end
  if @projName
    call_list.each_index do |x|
      myCaller = call_list[x].to_s
      if myCaller.include? @projName
        myCaller =~ /([\(\)\w_\_\-\.]+\:\d+\:?.*?)$/
        myList << "[#{$1.gsub(/eval/, @projName)}] "
        break
      end
    end
    break if x > depth or myCaller =~ /:in .run.$/  # this break causes error in Ruby 1.9.
  end
  myList
end

#get_caller_lineObject



347
348
349
350
351
# File 'lib/awetestlib/regression/utilities.rb', line 347

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

#get_debug_list(dbg = false) ⇒ Object



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

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



72
73
74
# File 'lib/awetestlib/regression/utilities.rb', line 72

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

#get_osObject



986
987
988
989
990
991
992
993
994
# File 'lib/awetestlib/regression/utilities.rb', line 986

def get_os
  @os = OpenStruct.new(
    :name     => Sys::Uname.sysname,
    :version  => Sys::Uname.version,
    :release  => Sys::Uname.release,
    :nodename => Sys::Uname.nodename,
    :machine  => Sys::Uname.machine
  )
end

#get_prefix(strg, offset) ⇒ Object



76
77
78
79
# File 'lib/awetestlib/regression/utilities.rb', line 76

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

#get_save_file_path(root, filename) ⇒ Object



616
617
618
619
# File 'lib/awetestlib/regression/utilities.rb', line 616

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

#get_test_levelObject



959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
# File 'lib/awetestlib/regression/utilities.rb', line 959

def get_test_level
  arr       = []
  each_line = 0
  call_list = Kernel.caller
  #debug_to_log("#{call_list.to_yaml}")
  call_list.each_index do |x|
    myCaller = call_list[x].to_s
    myCaller =~ /([\(\)\w_\_\-\.]+\:\d+\:?.*?)$/
    string = $1
    unless string =~ /logging\.rb|mark_testlevel|mark_test_level|debug_to_report|debug_toreport/
      if string.length > 0
        if string =~ /each|each_key/
          each_line = string.match(/\:(\d+)\:/)[1]
        elsif string.match(/\:(\d+)\:/)[1] == each_line
          next
        else
          arr << string.gsub(/eval/, @myName)
        end
      end
    end
    break if myCaller =~ /:in .run.$|runner\.rb/
  end
  #debug_to_log("#{arr.length} #{nice_array(arr)}")
  [arr.length, arr]
end

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/awetestlib/regression/utilities.rb', line 81

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



62
63
64
65
66
67
68
# File 'lib/awetestlib/regression/utilities.rb', line 62

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, key_type = :role, dbg = true) ⇒ Object



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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/awetestlib/regression/utilities.rb', line 108

def get_variables(file, key_type = :role, dbg = true)
  #TODO refactor this
  debug_to_log("#{__method__}: file = #{file}")
  debug_to_log("#{__method__}: key  = #{key_type}")

   = false
  script_found_in_data  = false

  @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
      script_found_in_data = true
      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
    = find_sheet_with_name(workbook, 'Login')
  if  and  >= 0
    workbook.default_sheet = workbook.sheets[]

    1.upto(workbook.last_column) do |col|
      a_cell = workbook.cell(1, col)
      case a_cell
        when @myName
                       = col
           = true
          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 key_type
        when :id, :userid
          key = userid
        when :role
          key = role
        else
          key = userid
      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_type}): #{key}=>'#{@login[key].to_yaml}'")
    end if dbg
  end

  if  and script_found_in_data
    true
  else
    failed_to_log("Script found: in Login = #{}; in Data = #{script_found_in_data}")
  end
rescue
  failed_to_log("#{__method__}: '#{$!}'")
end

#grab_window_list(strg) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/awetestlib/regression/utilities.rb', line 225

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

#method_to_title(method, no_sub = false) ⇒ Object



933
934
935
936
937
938
939
# File 'lib/awetestlib/regression/utilities.rb', line 933

def method_to_title(method, no_sub = false)
  title = method.to_s.titleize
  title.gsub!(/And/, '&') unless no_sub
  title
rescue
  debug_to_log("#{__method__}: #{method} #{$!}")
end

#nice_array(arr, space_to_underscore = false) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
# File 'lib/awetestlib/regression/utilities.rb', line 280

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_caller(caller) ⇒ Object



950
951
952
953
954
955
956
957
# File 'lib/awetestlib/regression/utilities.rb', line 950

def parse_caller(caller)
  call_script, call_line, call_meth = caller.split(':')
  call_script.gsub!(/\.rb/, '')
  call_script = call_script.camelize
  call_meth =~ /in .([\w\d_]+)./
  call_meth = $1
  [call_script, call_line, call_meth]
end

#parse_cookies(browser) ⇒ Object



556
557
558
559
560
561
562
563
564
565
# File 'lib/awetestlib/regression/utilities.rb', line 556

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



582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/awetestlib/regression/utilities.rb', line 582

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



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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/awetestlib/regression/utilities.rb', line 297

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

#rescue_me_command(element, how, what, command = nil, param = nil, container = :browser) ⇒ Object



926
927
928
929
930
931
# File 'lib/awetestlib/regression/utilities.rb', line 926

def rescue_me_command(element, how, what, command = nil, param = nil, container = :browser)
  loc = "#{container}.#{element}(#{how}, #{what})"
  loc << ".#{command}" if command
  loc << "(#{param})" if param
  loc
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



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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
# File 'lib/awetestlib/regression/utilities.rb', line 700

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.



637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/awetestlib/regression/utilities.rb', line 637

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



662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# File 'lib/awetestlib/regression/utilities.rb', line 662

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



621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/awetestlib/regression/utilities.rb', line 621

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



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

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



13
14
15
# File 'lib/awetestlib/regression/utilities.rb', line 13

def set_script_variables
  # TODO: replace with method_missing?
end

#setupObject



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
45
46
47
48
49
50
51
# File 'lib/awetestlib/regression/utilities.rb', line 17

def setup
  #    if @os.name =~ /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.name =~ /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



292
293
294
295
# File 'lib/awetestlib/regression/utilities.rb', line 292

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

#translate_var_list(key) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
# File 'lib/awetestlib/regression/utilities.rb', line 213

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

#unable_to(message = '', no_dolbang = false) ⇒ Object



941
942
943
944
945
946
947
948
# File 'lib/awetestlib/regression/utilities.rb', line 941

def unable_to(message = '', no_dolbang = false)
  call_arr                          = get_call_array()
  call_script, call_line, call_meth = parse_caller(call_arr[1])
  strg                              = "Unable to #{call_meth.titleize}:"
  strg << " #{message}" if message.length > 0
  strg << " '#{$!}'" unless no_dolbang
  strg
end

#upload_file(data_path) ⇒ Object



874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
# File 'lib/awetestlib/regression/utilities.rb', line 874

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