Module: Awetestlib::Regression::Utilities

Defined in:
lib/awetestlib/regression/utilities.rb,
lib/awetestlib/regression/awetest_dsl.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

#analyse_element_presence(container, element, how, what, max_seconds = 30, interval = 0.25) ⇒ Object



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

def analyse_element_presence(container, element, how, what, max_seconds = 30, interval = 0.25)
  duration = 0
  code     = build_webdriver_fetch(element, how, what) + '.present?'
  debug_to_log("#{__method__}: code=>[#{code}")
  until duration > max_seconds do
    begin
      debug_to_log("#{eval(code)} #{duration}")
    rescue => e
      debug_to_log("#{__method__}: #{e.inspect}")
    end
    duration += interval
    sleep(interval)
  end
end

#array_neighbors(arr, target) ⇒ Object



1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
# File 'lib/awetestlib/regression/utilities.rb', line 1479

def array_neighbors(arr, target)
  less_than    = []
  greater_than = []
  arr.each do |elmt|
    if elmt < target
      less_than << elmt
    elsif elmt > target
      greater_than << elmt
    end
  end
  [less_than.max, greater_than.min]
end

#array_to_list(arr, delim = ',') ⇒ Object Also known as: arr2list



1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
# File 'lib/awetestlib/regression/utilities.rb', line 1492

def array_to_list(arr, delim = ',')
  list = ''
  arr.each do |entry|
    if entry =~ /#{delim}/
      list << "\"#{entry}\""
    else
      list << entry
    end
    list << "#{delim} " unless entry == arr.last
  end
  list
end

#awetestlib?Boolean

Returns:

  • (Boolean)


1507
1508
1509
1510
1511
# File 'lib/awetestlib/regression/utilities.rb', line 1507

def awetestlib?
  defined? Awetestlib::Runner
rescue
  return false
end

#build_message(strg1, *strings) ⇒ Object Also known as: build_msg, bld_msg



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/awetestlib/regression/utilities.rb', line 53

def build_message(strg1, *strings)
  msg = "#{strg1}"
  strings.each do |strg|
    if strg.is_a?(Array)
      strg.each do |str|
        msg << " #{str}" if str and str.length > 0
      end
    else
      msg << " #{strg}" if strg and strg.length > 0
    end
  end if strings
  msg
rescue
  failed_to_log(unable_to)
end

#build_webdriver_fetch(element, how, what, more = {}) ⇒ Object



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
# File 'lib/awetestlib/regression/utilities.rb', line 720

def build_webdriver_fetch(element, how, what, more = {})
  code = "container.#{element}(:#{how} => "
  what = escape_stuff(what) unless how == :index
  if what.is_a?(Regexp)
    code << "/#{what.source}/"
  elsif how == :index
    code << "#{what}"
  else
    code << "'#{what}'"
  end
  if more and not more.empty?
    more.each do |key, vlu|
      next if key == :desc or key == :flash or key == :exists_only
      code << ", :#{key} => "
      if vlu.is_a?(Regexp)
        code << "/#{vlu}/"
      elsif vlu.is_a?(String)
        code << "'#{vlu.gsub('/', '\/')}'"
      else
        code << "#{vlu}"
      end
    end
  end
  code << ')'
  #debug_to_log("code: '#{code}'")
  code
rescue
  failed_to_log(unable_to)
end

#calc_index(index, every = 1) ⇒ Object



87
88
89
# File 'lib/awetestlib/regression/utilities.rb', line 87

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

#capture_screen(browser, ts) ⇒ Object



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

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)



1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
# File 'lib/awetestlib/regression/utilities.rb', line 1139

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

#convert_color_value(value, rgba = false) ⇒ Object



541
542
543
544
545
546
547
# File 'lib/awetestlib/regression/utilities.rb', line 541

def convert_color_value(value, rgba = false)
  if value =~ /^#/
    html_to_rgb(value, rgba)
  else
    rgb_to_html(value)
  end
end

#count_duplicates(arr) ⇒ Object



366
367
368
369
370
371
372
373
374
# File 'lib/awetestlib/regression/utilities.rb', line 366

def count_duplicates(arr)
  counts = {}
  dups   = {}
  arr.each do |id|
    counts[id] = counts[id] ? counts[id] + 1 : 1
    dups[id]   = counts[id] if counts[id] > 1
  end
  [dups, counts]
end

#debug_call_list(msg) ⇒ Object



376
377
378
379
# File 'lib/awetestlib/regression/utilities.rb', line 376

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



1267
1268
1269
1270
1271
1272
1273
1274
# File 'lib/awetestlib/regression/utilities.rb', line 1267

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_array(arr, space_to_underscore = false) ⇒ Object



614
615
616
617
618
619
620
621
622
# File 'lib/awetestlib/regression/utilities.rb', line 614

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



636
637
638
639
640
641
642
643
644
645
646
# File 'lib/awetestlib/regression/utilities.rb', line 636

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



648
649
650
651
652
653
654
655
656
657
658
# File 'lib/awetestlib/regression/utilities.rb', line 648

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



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

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_option_array(options, desc = '', to_report = false) ⇒ Object



675
676
677
678
679
680
681
682
683
684
685
# File 'lib/awetestlib/regression/utilities.rb', line 675

def dump_option_array(options, desc = '', to_report = false)
  msg = with_caller(desc, "\n")
  options.each do |option|
    msg << "text: '#{option.text}' value: '#{option.value}' selected: #{option.selected?}\n"
  end
  if to_report
    debug_to_report(msg)
  else
    debug_to_log(msg)
  end
end

#dump_select_list_options(element, report = false) ⇒ Object



660
661
662
663
664
665
666
667
668
669
670
671
672
673
# File 'lib/awetestlib/regression/utilities.rb', line 660

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

#element_action_message(element, action, how = nil, what = nil, value = nil, desc = '', refs = '') ⇒ Object



750
751
752
753
754
755
756
# File 'lib/awetestlib/regression/utilities.rb', line 750

def element_action_message(element, action, how = nil, what = nil, value = nil, desc = '', refs = '')
  name      = element.respond_to?(:tag_name) ? element.tag_name.upcase : element.to_s
  how, what = extract_locator(element, how)[1, 2] unless how and what
  build_message(desc, action, "#{name}",
                (what ? "with #{how}=>'#{what}'" : nil),
                (value ? "and value=>'#{value}'" : nil), refs)
end

#element_query_message(element, query, how = nil, what = nil, value = nil, desc = '', refs = '', tag = '') ⇒ Object



758
759
760
761
762
763
764
765
766
767
768
769
770
# File 'lib/awetestlib/regression/utilities.rb', line 758

def element_query_message(element, query, how = nil, what = nil, value = nil, desc = '', refs = '')
  if element.exists?
    name = element.respond_to?(:tag_name) ? element.tag_name.upcase : element.to_s
  else
    name = '(unknown)'
  end
  build_message(desc, "#{name}",
                (what ? "with #{how}=>' #{what}'" : nil),
                (value ? "and value=>'#{value}'" : nil),
                query, refs)
rescue
  failed_to_log(unable_to)
end

#end_processes(*names) ⇒ Object



772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
# File 'lib/awetestlib/regression/utilities.rb', line 772

def end_processes(*names)
  pattern = ''
  names.each { |n| pattern << "#{n}|" }
  pattern.chop!
  # puts pattern
  targets = {}

  if USING_OSX
    p_io = IO.popen("ps axo comm,pid,sess,fname")
  else
    p_io = IO.popen("tasklist /nh")
  end

  p_io.readlines.each do |prc|
    # puts prc.chop
    if prc =~ /#{pattern}/
      name, pid    = prc.split(/\s+/)[0, 2]
      # puts "#{name} #{pid}"
      base         = File.basename(name)
      targets[pid] = base
    end
  end

  debug_to_log("End these processes:\n#{targets.to_yaml}")

  if USING_OSX
    kill_cmd = 'kill -9 @@@@@'
  else
    kill_cmd = 'taskkill /f /pid @@@@@'
  end

  targets.each do |pid, name|
    cmd = kill_cmd.sub('@@@@@', pid)
    debug_to_log("[#{cmd}]")
    kill_io = IO.popen(cmd, :err => :out)
    debug_to_log(kill_io.read.chomp)
  end

  if targets.length > 0
    sleep_for(10)
  end
end

#escape_stuff(strg) ⇒ Object



815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
# File 'lib/awetestlib/regression/utilities.rb', line 815

def escape_stuff(strg)
  if strg.respond_to?(:dup)
    rslt = strg.dup
    unless rslt.is_a?(Regexp)
      if rslt.match(/[\/\(\)]/)
        rslt.gsub!('/', '\/')
        rslt.gsub!('(', '\(')
        rslt.gsub!(')', '\)')
        rslt = Regexp.new(rslt)
      end
    end
  else
    rslt = strg
  end
  rslt
rescue
  failed_to_log(unable_to("#{rslt}"))
end

#extract_locator(element, how = nil) ⇒ Object



849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
# File 'lib/awetestlib/regression/utilities.rb', line 849

def extract_locator(element, how = nil)
  # html_to_log(element)
  if element.respond_to?(:tag_name)
    tag = element.tag_name.to_sym
  else
    element = element.body.elements[0]
    tag     = element.tag_name.to_sym
  end
  what = nil
  case how
    when nil
      [:id, :name, :title, :class, :value].each do |attr|
        what = element.attribute_value(attr.to_s)
        if what and what.length > 0
          how = attr
          break
        end
      end
    else
      what = element.attribute_value(how.to_s)
  end
  # debug_to_log(with_caller("#{tag}:#{how}:#{what}"))
  [tag, how, what]
rescue
  failed_to_log(unable_to(build_message(":#{tag}, :#{how}='#{what}'")))
end

#extract_selected(selected_options, which = :text) ⇒ Object



834
835
836
837
838
839
840
841
842
843
844
845
846
847
# File 'lib/awetestlib/regression/utilities.rb', line 834

def extract_selected(selected_options, which = :text)
  arr = Array.new
  selected_options.each do |so|
    case which
      when :text
        arr << so.text
      else
        arr << so.value
    end
  end
  arr.sort
rescue
  failed_to_log(unable_to)
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



1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
# File 'lib/awetestlib/regression/utilities.rb', line 1171

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, title = 'Choose File', text = '', button = '&Open', control = 'Edit1', side = 'primary') ⇒ Object

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



1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
# File 'lib/awetestlib/regression/utilities.rb', line 1186

def file_upload(filepath, title = 'Choose File', text = '', button = '&Open',
                control = 'Edit1', side = 'primary')
  title = translate_popup_title(title)
  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



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/awetestlib/regression/utilities.rb', line 381

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(container, element, how, what, value = nil, desc = '', refs = '', options = {}) ⇒ Object



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

def flash(container, element, how, what, value = nil, desc = '', refs = '', options = {})
  if @flash
    value, desc, refs, options = capture_value_desc(value, desc, refs, options) # for backwards compatibility
    code                       = build_webdriver_fetch(element, how, what, options)
    target                     = eval(code)
    flash_element(target, desc, refs)
  end
rescue
  failed_to_log(unable_to)
end

#flash_element(element, desc = '', refs = '') ⇒ Object



928
929
930
931
932
933
934
935
936
937
938
939
940
941
# File 'lib/awetestlib/regression/utilities.rb', line 928

def flash_element(element, desc = '', refs = '')
  if @flash
    if element
      element.wd.location_once_scrolled_into_view
      # scroll_to(element.browser, element, desc, refs)
      if element.respond_to?(:flash)
        # sleep(0.1)
        element.flash
      end
    end
  end
rescue
  failed_to_log(unable_to)
end

#flash_id(browser, strg, count) ⇒ Object

Deprecated.


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

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.


1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
# File 'lib/awetestlib/regression/utilities.rb', line 1252

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



1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
# File 'lib/awetestlib/regression/utilities.rb', line 1240

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

#force_regexp(target) ⇒ Object



1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
# File 'lib/awetestlib/regression/utilities.rb', line 1443

def force_regexp(target)
  if target.respond_to?(:dup)
    rslt = target.dup
    unless rslt.is_a?(Regexp)
      rslt = Regexp.new(Regexp.escape(target.to_s))
    end
  else
    rslt = target
  end
  rslt
rescue
  failed_to_log(unable_to("'#{target}'"))
end

#force_string(target, slash_regexp = true) ⇒ Object



1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
# File 'lib/awetestlib/regression/utilities.rb', line 1457

def force_string(target, slash_regexp = true)
  if target
    if target.respond_to?(:dup)
      rslt = target.dup
      if rslt.is_a?(Regexp)
        if slash_regexp
          rslt = "/#{rslt.source}/"
        else
          rslt = rslt.source
        end
      end
    else
      rslt = target.to_s
    end
  else
    rslt = ''
  end
  rslt
rescue
  failed_to_log(unable_to("'#{target}'"))
end

#format_refs(list) ⇒ Object



1433
1434
1435
1436
1437
1438
1439
1440
1441
# File 'lib/awetestlib/regression/utilities.rb', line 1433

def format_refs(list)
  refs = ''
  if list
    list.split(/,\s*/).each do |ref|
      refs << "*** #{ref} *** "
    end
  end
  refs
end

#get_awetestlib_metadataObject



288
289
290
# File 'lib/awetestlib/regression/utilities.rb', line 288

def 
  $metadata = YAML.load(`gem spec awetestlib metadata`.chomp)
end

#get_basic_auth_control_indexesObject



1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
# File 'lib/awetestlib/regression/utilities.rb', line 1335

def get_basic_auth_control_indexes
  case $win_major
    when '5'
      case @browserAbbrev
        when 'IE'
          ['2', '3', '1', 'Connect to']
        when 'FF'
          ['2', '3', '1', 'Authentication Required']
        when 'GC', 'C'
          ['2', '1', '2', 'Authentication Required']
      end
    when '6'
      case @browserAbbrev
        when 'IE'
          ['1', '2', '2', 'Windows Security']
        when 'FF'
          ['2', '1', '2', 'Authentication Required']
        when 'GC', 'C'
          ['2', '1', '2', 'Authentication Required']
      end
  end
end

#get_element_properties(element, list = []) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/awetestlib/regression/awetest_dsl.rb', line 284

def get_element_properties(element, list = [])
  hash = {}
  list.each do |prop|
    style = nil
    attr  = nil
    begin
      style = element.style(prop)
    rescue
      debug_to_log(with_caller("Can't find style '#{prop}"))
    end
    begin
      attr = element.attribute_value(prop)
    rescue
      debug_to_log(with_caller("Can't find attribute '#{prop}"))
    end

    hash[prop] = style ? style : attr
  end

  hash
rescue
  failed_to_log(unable_to)
end

#get_osObject



1513
1514
1515
1516
1517
1518
1519
1520
1521
# File 'lib/awetestlib/regression/utilities.rb', line 1513

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



82
83
84
85
# File 'lib/awetestlib/regression/utilities.rb', line 82

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

#get_project_git(proj_name, proj_dir = Dir.pwd) ⇒ Object



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
# File 'lib/awetestlib/regression/utilities.rb', line 298

def get_project_git(proj_name, proj_dir = Dir.pwd)
  debug_to_log(with_caller(proj_dir))
  sha    = nil
  branch = nil
  date   = nil

  curr_dir = Dir.pwd
  version_file = "#{proj_name.downcase.gsub(' ', '_')}_version"

  if Dir.exists?(proj_dir)

    Dir.chdir(proj_dir) unless proj_dir == curr_dir

    if Dir.exists?('.git')
      require 'git'
      git    = Git.open(Dir.pwd)
      branch = git.current_branch
      commit = git.gblob(branch).log(5).first
      sha    = commit.sha
      date   = commit.date

      version_file = File.join(proj_dir, version_file)
      file         = File.open(version_file, 'w')
      file.puts "#{branch}, #{date}, #{sha}"
      file.close

    end

    Dir.chdir(curr_dir) unless proj_dir == curr_dir

  end

  unless branch
    version_file = File.join(Dir.pwd, version_file)
    if File.exists?(version_file)
      vers              = File.open(version_file).read
      branch, date, sha = parse_list(vers.chomp)
    end
  end

  [branch, date, sha]
end

#get_save_file_path(root, filename) ⇒ Object



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

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

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



72
73
74
75
76
77
78
# File 'lib/awetestlib/regression/utilities.rb', line 72

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_upload_file_control_indexesObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/awetestlib/regression/utilities.rb', line 91

def get_upload_file_control_indexes
  case $win_major
    when '5'
      case @browserAbbrev
        when 'IE'
          ['1', '1', '2', 'Choose File to Upload']
        when 'FF'
          ['1', '1', '2', 'File Upload']
        when 'GC', 'C'
          ['1', '1', '2', 'Open']
      end
    when '6'
      case @browserAbbrev
        when 'IE'
          ['1', '1', '2', 'Choose File to Upload']
        when 'FF'
          ['1', '1', '2', 'File Upload']
        when 'GC', 'C'
          ['1', '1', '2', 'Open']
      end
  end
end

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



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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/awetestlib/regression/utilities.rb', line 114

def get_variables(file, key_type = :role, enabled_only = true, dbg = true)

  #TODO: support for xlsx files
  #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
  script_col   = 0
  role_col     = 0
  userid_col   = 0
  company_col  = 0
  password_col = 0
  url_col      = 0
  env_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).downcase
      case a_cell
        when @myName.downcase
          script_col            = col
           = true
          break
        when 'role'
          role_col = col
        when 'userid', 'user_id'
          userid_col = col
        when 'companyid', 'company_id'
          company_col = col
        when 'password'
          password_col = col
        when 'url'
          url_col = col
        when 'environment'
          env_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)
      env       = workbook.cell(line, env_col)
      username  = workbook.cell(line, name_col)
      companyid = workbook.cell(line, company_col)
      enabled   = workbook.cell(line, script_col).to_s

      case key_type
        when :id, :userid
          key = userid
        when :environment
          key = env
        when :role
          key = role
        else
          key = userid
      end

      if enabled_only and enabled.length == 0
        next
      end

      @login[key]              = Hash.new
      @login[key]['role']      = role
      @login[key]['userid']    = userid
      @login[key]['companyid'] = companyid
      @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(unable_to)
end

#get_windows_versionObject

def where_am_i?(index = 1)

get_call_list_new[index].to_s

end



1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
# File 'lib/awetestlib/regression/utilities.rb', line 1374

def get_windows_version
  ver          = `ver`.gsub("\n", '')
  mtch         = ver.match(/(.*)\s\[Version\s*(\d+)\.(\d+)\.(\d+)\]/)
  $win_name    = mtch[1]
  $win_major   = mtch[2]
  $win_minor   = mtch[3]
  $win_build   = mtch[4]
  $win_version = "#{$win_major}.#{$win_minor}.#{$win_build}"
rescue
  failed_to_log(unable_to)
end

#git_sha1(file) ⇒ Object



341
342
343
344
345
346
# File 'lib/awetestlib/regression/utilities.rb', line 341

def git_sha1(file)
  if File.exists?(file)
    size, sha1 = `ruby git_sha1.rb #{file}`.chomp.split(/\n/)
    debug_to_log("#{file} #{size} sha1 is #{sha1}")
  end
end

#grab_window_list(strg) ⇒ Object



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

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

#html_to_rgb(html, a = true) ⇒ Object



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/awetestlib/regression/utilities.rb', line 572

def html_to_rgb(html, a = true)
  if html and html.length > 0
    html = html.gsub(%r{[#;]}, '')
    case html.size
      when 3
        colors = html.scan(%r{[0-9A-Fa-f]}).map { |el| (el * 2).to_i(16) }
      when 6
        colors = html.scan(%r<[0-9A-Fa-f]{2}>).map { |el| el.to_i(16) }
    end
    rgb = 'rgb'
    rgb << 'a' if a
    rgb << '('
    colors.each do |c|
      rgb << "#{c}, "
    end
    if a
      rgb << '1)'
    else
      rgb.strip!.chop!
      rgb << ')'
    end
    rgb
  else
    html
  end
end

#insert_id_pswd_in_url(userid, password, url) ⇒ Object



1329
1330
1331
1332
1333
# File 'lib/awetestlib/regression/utilities.rb', line 1329

def insert_id_pswd_in_url(userid, password, url)
  http = url.match(/^(http)(s?)(:\/\/)/)
  path = url.gsub(http[0], '')
  URI.encode("#{http[0]}#{userid}:#{password}@#{path}")
end

#method_to_title(method, no_sub = false, subs = { /And/ => '&', /^Ac / => 'AC ' }) ⇒ Object



1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
# File 'lib/awetestlib/regression/utilities.rb', line 1287

def method_to_title(method, no_sub = false, subs = { /And/ => '&', /^Ac / => 'AC ' })
  title = method.to_s.titleize
  unless no_sub
    subs.each do |ptrn, rplc|
      title.gsub!(ptrn, rplc)
    end
  end
  title
rescue
  debug_to_log(unable_to(": #{method}"))
end

#nice_array(arr, space_to_underscore = false) ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
# File 'lib/awetestlib/regression/utilities.rb', line 399

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

#nice_number(number, decimals = 0, dollars = false) ⇒ Object



411
412
413
414
415
416
# File 'lib/awetestlib/regression/utilities.rb', line 411

def nice_number(number, decimals = 0, dollars = false)
  number.to_s.gsub!(/[,\$]/, '')
  ptrn = "%0.#{decimals}f"
  ptrn = '$' + ptrn if dollars
  sprintf(ptrn, number).gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
end

#normalize_border_style(value, rgba = true) ⇒ Object



221
222
223
224
225
# File 'lib/awetestlib/regression/awetest_dsl.rb', line 221

def normalize_border_style(value, rgba = true)
  weight, style, color = parse_list(value, ' ', 3)
  norm_color           = normalize_color_value(color, rgba)
  "#{weight} #{style} #{norm_color}"
end

#normalize_color_value(value, rgba = true) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/awetestlib/regression/utilities.rb', line 418

def normalize_color_value(value, rgba = true)
  case value
    when /^#/
      html_to_rgb(value, rgba)
    when /^rgba/i
      value
    when /^rgb()/i
      rgb_to_rgba(value)
    when /^transparent/i, /^0$/i
      'rgba(0, 0, 0, 0)'
    when /white/
      'rgba(255, 255, 255, 1)'
    else
      html_to_rgb(translate_color_name(value), rgba)
  end
end

#number_to_word(nbr) ⇒ Object Also known as: nbr2word, nbr2wd



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

def number_to_word(nbr)
  map = { 1  => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five',
          6  => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine', 10 => 'ten',
          11 => 'eleven', 12 => 'twelve', 13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen',
          16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen', 19 => 'nineteen', 20 => 'twenty'
  }
  if nbr > 20
    'more than twenty'
  else
    map[nbr]
  end
end

#pad_id_end_count(id, delim = '_', pad = 6) ⇒ Object



1313
1314
1315
1316
1317
1318
# File 'lib/awetestlib/regression/utilities.rb', line 1313

def pad_id_end_count(id, delim = '_', pad = 6)
  mtch = id.match(/^(.*)#{delim}(\d+)$/)
  mtch[1] + delim + mtch[2].rjust(pad, '0')
rescue
  failed_to_log(unable_to("id: '#{id}' delim: '#{delim}' pad: #{pad}"))
end

#parse_caller(caller) ⇒ Object



1320
1321
1322
1323
1324
1325
1326
1327
# File 'lib/awetestlib/regression/utilities.rb', line 1320

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



687
688
689
690
691
692
693
694
695
696
# File 'lib/awetestlib/regression/utilities.rb', line 687

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

#parse_list(string, delim = ',', limit = -1)) ⇒ Object



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

def parse_list(string, delim = ',', limit = -1)
  string.split(/#{delim}\s*/, limit)
rescue
  failed_to_log(unable_to("string:[#{string}]"))
end

#parse_test_flag(string) ⇒ Object



698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/awetestlib/regression/utilities.rb', line 698

def parse_test_flag(string)
  test = false
  refs = nil
  if string
    if string == true or string == false
      test = string
    else
      if string.length > 0
        unless string =~ /^no$/i
          test = true
          unless string =~ /^yes$/i
            refs = format_refs(string)
          end
        end
      end
    end
  end
  [test, refs]
rescue
  failed_to_log(unable_to)
end

#pdf_to_text(file, noblank = true) ⇒ Object



891
892
893
894
895
896
897
898
899
900
901
902
903
904
# File 'lib/awetestlib/regression/utilities.rb', line 891

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



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/awetestlib/regression/utilities.rb', line 486

def rescue_me(e, me = nil, what = nil, where = nil, who = nil)
  #TODO: these are rescues from exceptions raised in Watir or Watir-webdriver
  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 =~ /(The SafariDriver does not interact with modal dialogs)/i
    to_report = $1
    ok        = true
  elsif msg =~ /HRESULT error code:0x80070005/
    ok = true
    #elsif msg =~ /missing\s+\;\s+before statement/
    #  ok = true
  end
  call_list = get_call_list(6, true)
  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__}: #{call_list}")
    failed_to_log("#{to_report}  #{call_list}")
  else
    debug_to_log("#{__method__}: NO RESCUE: #{e.message}")
    debug_to_log("#{__method__}: NO RESCUE: \n#{call_list}")
  end
  debug_to_log("#{__method__}: Exit")
  ok
end

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



1276
1277
1278
1279
1280
1281
# File 'lib/awetestlib/regression/utilities.rb', line 1276

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

#rescue_msg_for_validation(desc, refs = nil) ⇒ Object



1283
1284
1285
# File 'lib/awetestlib/regression/utilities.rb', line 1283

def rescue_msg_for_validation(desc, refs = nil)
  failed_to_log(unable_to(build_message(desc, refs), NO_DOLLAR_BANG, VERIFY_MSG), 2)
end

#rgb_to_html(rgb) ⇒ Object



549
550
551
552
553
554
555
556
557
# File 'lib/awetestlib/regression/utilities.rb', line 549

def rgb_to_html(rgb)
  rgb =~ /rgba?\((.+)\)/
  if $1
    r, g, b, a = $1.split(/,\s*/)
    "#%02x%02x%02x" % [r, g, b]
  else
    rgb
  end
end

#rgb_to_rgba(rgb) ⇒ Object



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

def rgb_to_rgba(rgb)
  if rgb =~ /^rgb\(\s*(\d+),\s*(\d+),\s*(\d+)\s*\)/i
    r    = $1
    g    = $2
    b    = $3
    op   = rgb =~ /[1-9]/ ? '1' : '0'
    rtrn = "rgba(#{r}, #{g}, #{b}, #{op})" #waft-1148
  else
    rtrn = rgb
  end
  rtrn
end

#rotate_array(arr, stop = 0, index = 0, target = '') ⇒ Object



1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
# File 'lib/awetestlib/regression/utilities.rb', line 1025

def rotate_array(arr, stop = 0, index = 0, target = '')
  rotated = arr.dup
  length  = rotated.size
  (1..length).each do |itr|
    rotated.push(rotated.shift)
    if stop > 0
      break if itr == stop
    else
      break if rotated[index] == target
    end
  end
  rotated
rescue
  failed_to_log(unable_to)
end

#running_thread_countObject



1041
1042
1043
1044
1045
# File 'lib/awetestlib/regression/utilities.rb', line 1041

def running_thread_count
  running = Thread.list.select { |thread| thread.status == "run" }.count
  asleep  = Thread.list.select { |thread| thread.status == "sleep" }.count
  [running, asleep]
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



1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
# File 'lib/awetestlib/regression/utilities.rb', line 1049

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.



964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
# File 'lib/awetestlib/regression/utilities.rb', line 964

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



989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
# File 'lib/awetestlib/regression/utilities.rb', line 989

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



948
949
950
951
952
953
954
955
956
957
958
959
960
961
# File 'lib/awetestlib/regression/utilities.rb', line 948

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

#set_env_name(xls = @xls_path, fix = :prefix, strg = 'toad') ⇒ Object



1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
# File 'lib/awetestlib/regression/utilities.rb', line 1386

def set_env_name(xls = @xls_path, fix = :prefix, strg = 'toad')
  if fix == :prefix
    pattern = /#{strg}_([\w\d]+)\.xls$/
  else
    pattern = /([\w\d]+)_#{strg}\.xls$/
  end
  if awetestlib?
    if @runenv
      @env_name = @myAppEnv.name.downcase.underscore
    else
      @env_name = 'dev'
      #if xls
      #  xls =~ pattern
      #  @env_name = $1
      #else
      #  @env_name = 'sit'
      #end
    end
  else
    @env_name = @myAppEnv.name.downcase # .underscore #.gsub(/^toad./, '')
  end
  debug_to_report("#{__LINE__}: @env_name=#{@env_name}")
rescue
  failed_to_log(unable_to)
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

#set_xls_spec(proj_acro = 'unknown', env = @env_name.downcase.underscore, fix = :prefix, xlsx = @xlsx) ⇒ Object



1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
# File 'lib/awetestlib/regression/utilities.rb', line 1412

def set_xls_spec(proj_acro = 'unknown', env = @env_name.downcase.underscore, fix = :prefix, xlsx = @xlsx)
  env = env.split(/:[\s_]*/)[1] if env =~ /:/
  case fix
    when :prefix
      xls_name = "#{proj_acro}_#{env}.xls"
    when :suffix
      xls_name = "#{env}_#{proj_acro}.xls"
    when :none
      xls_name = "#{env.gsub('-', '_')}.xls"
    else
      failed_to_log(with_caller("Unknown fix type: '#{fix}'.  Must be 'prefix', 'suffix', or 'none'."))
      return nil
  end
  spec = "#{@myRoot}/#{xls_name}"
  spec << 'x' if xlsx
  debug_to_log("#{where_am_i?}: #{spec}")
  spec
rescue
  failed_to_log(unable_to)
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_array_numeric_sort(arr) ⇒ Object Also known as: strg_arr_numeric_sort



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

def string_array_numeric_sort(arr)
  #TODO: almost certainly a more 'rubyish' and less clunky way to do this
  trgt = arr.dup
  narr = []
  trgt.each do |n|
    narr << n.to_i
  end
  narr.sort!
  sarr = []
  narr.each do |n|
    sarr << n.to_s
  end
  sarr
end

#string_count_in_string(strg, substrg) ⇒ Object



467
468
469
470
# File 'lib/awetestlib/regression/utilities.rb', line 467

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

#string_to_hex(strg, format = 'U') ⇒ Object



472
473
474
475
476
477
# File 'lib/awetestlib/regression/utilities.rb', line 472

def string_to_hex(strg, format = 'U')
  strg.unpack(format*strg.length)
  # strg.split(//).collect do |x|
  #   x.match(/\d/) ? x : x.unpack('U')[0].to_s(16)
  # end
end

#strip_regex_mix(strg) ⇒ Object



479
480
481
482
483
484
# File 'lib/awetestlib/regression/utilities.rb', line 479

def strip_regex_mix(strg)
  rslt = strg.dup
  mtch = rslt.match(/(\(\?-mix:(.+)\))/)
  rslt.sub!(mtch[1], "/#{mtch[2]}/")
  rslt
end

#translate_color_name(color) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/awetestlib/regression/utilities.rb', line 237

def translate_color_name(color)
  if color and color.length > 0
    HTML_COLORS[color.camelize.downcase].downcase
  else
    color
  end
end

#translate_tag_name(element) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/awetestlib/regression/utilities.rb', line 245

def translate_tag_name(element)
  rtrn = ''
  tag  = ''
  typ  = ''
  if element.respond_to?(:tag_name)
    tag  = element.tag_name
    typ  = element.type if element.respond_to?(:type)
    rtrn = tag
    case tag
      when 'a'
        rtrn = 'link'
      when 'input'
        case typ
          when 'text'
            rtrn = 'textfield'
          when 'textarea'
            rtrn = 'textarea'
          when 'submit', 'button'
            rtrn = 'button'
          else
            rtrn = tag
        end
      else
        rtrn = tag
    end
  end
  rtrn
rescue
  failed_to_log(unable_to(tag, typ))
end

#translate_var_list(key) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
# File 'lib/awetestlib/regression/utilities.rb', line 276

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, verify_that = false, caller_index = 1) ⇒ Object



1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
# File 'lib/awetestlib/regression/utilities.rb', line 1299

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

#upload_file(browser, data_path, wait = 20) ⇒ Object



1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
# File 'lib/awetestlib/regression/utilities.rb', line 1222

def upload_file(browser, data_path, wait = 20)
  #mark_test_level(data_path)
  message_to_report(with_caller(data_path))
  data_path.gsub!('/', '\\') if USING_WINDOWS

  file, open, cancel, title = get_upload_file_control_indexes

  @ai.WinWait(title, "", wait)
  sleep_for(1)
  @ai.ControlSend(title, '', "[CLASS:Edit; INSTANCE:#{file}]", '!u')
  @ai.ControlSetText(title, '', "[CLASS:Edit; INSTANCE:#{file}]", data_path)
  sleep_for(0.5)
  @ai.ControlClick(title, "", "[CLASS:Button; INSTANCE:#{open}]", "primary")

rescue
  failed_to_log(unable_to)
end

#windows_to_log(browser) ⇒ Object



1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
# File 'lib/awetestlib/regression/utilities.rb', line 1358

def windows_to_log(browser)
  msg = ("===== Current windows (#{where_am_i?(2)})")
  idx = 0
  browser.windows.each do |w|
    msg << "\n  #{idx}: #{w.title} current?=#{w.current?}" #" (#{w.url})"
    idx += 1
  end
  debug_to_log(msg)
rescue => e
  debug_to_log(unable_to("#{where_am_i?(2)}: #{e.inspect}"))
end