require 'singleton' module Scoutui::Commands class Utils include Singleton attr_accessor :totalCommands attr_accessor :timeout attr_accessor :hwnds attr_accessor :useFrameSearch def initialize @command_list=['pause', 'assignvar', 'assignments', 'existsAlert', 'clickJsAlert', 'fillform', 'frame', 'highlight', 'submitform', 'type', 'click', 'loaddata', 'connect', 'loaddb', 'executecommands', 'definecommands', 'loadrequirements', 'mouseover', 'navigate', 'select', 'select_window', 'sleep', 'uploadfiles', 'verifyelt', 'verifyelement', 'verifyform'] @totalCommands={} @timeout=30 @command_list.each do |c| @totalCommands[c]=0 end @useFrameSearch = false @transList={ '__CHAR10__' => lambda { }, '__CITY__' => lambda { Faker::Address.city.to_s }, '__COMPANY.NAME__' => lambda { Faker::Company.name.to_s }, '__COMPANY.EIN__' => lambda { Faker::Company.ein.to_s }, '__COUNTRY__' => lambda { Faker::Address.country.to_s }, '__DATE__' => lambda { Time.now().to_s }, '__EMAIL__' => lambda { Faker::Internet.email.to_s}, '__DDMMYY__' => lambda { Date.parse(Time.now.to_s).strftime("%d%m%Y") }, '__MM/DD/YY__' => lambda { Date.parse(Time.now.to_s).strftime("%m/%d/%y") }, '__MM/DD/YY\+\d+__' => lambda { |s| puts __FILE__ + (__LINE__).to_s + " Process s => #{s}" d=s.match(/__MM\/DD\/YY\+(\d+)__/)[1] d=(Date.today + d.to_i).strftime("%m/%d/%y") puts __FILE__ + (__LINE__).to_s + " new date: #{d}" d }, '__MMDDYY__' => lambda { Date.parse(Time.now.to_s).strftime("%m%d%y") }, '__MMDDYY\+\d+__' => lambda { |s| puts __FILE__ + (__LINE__).to_s + " Process s => #{s}" d=s.match(/__MMDDYY\+(\d+)__/)[1] d=(Date.today + d.to_i).strftime("%m%d%y") puts __FILE__ + (__LINE__).to_s + " new date: #{d}" d }, '__MM/DD/YYYY__' => lambda { Date.parse(Time.now.to_s).strftime("%m/%d/%Y") }, '__MM/DD/YYYY\+\d+__' => lambda { |s| puts __FILE__ + (__LINE__).to_s + " Process s => #{s}" d=s.match(/__MM\/DD\/YYYY\+(\d+)__/)[1] d=(Date.today + d.to_i).strftime("%m/%d/%Y") puts __FILE__ + (__LINE__).to_s + " new date: #{d}" d }, '__MMDDYYYY__' => lambda { Date.parse(Time.now.to_s).strftime("%m%d%Y") }, '__MMDDYYYY\+\d+__' => lambda { |s| puts __FILE__ + (__LINE__).to_s + " Process s => #{s}" d=s.match(/__MMDDYYYY\+(\d+)__/)[1] d=(Date.today + d.to_i).strftime("%m%d%Y") puts __FILE__ + (__LINE__).to_s + " new date: #{d}" d }, '__PARAGRAPH__' => lambda { Faker::Lorem.paragraph.to_s }, '__PASSWORD__' => lambda { Faker::Internet.password.to_s }, '__PHONENUMBER__' => lambda { Faker::PhoneNumber.phone_number.to_s }, '__PHONENUMBER.AREACODE__' => lambda { Faker::Config.locale = 'en-US'; Faker::PhoneNumber.area_code.to_s }, '__PHONENUMBER.EXCHANGE__' => lambda { Faker::Config.locale = 'en-US'; Faker::PhoneNumber.exchange_code.to_s }, '__PHONENUMBER.SUBSCRIBER__' => lambda { Faker::PhoneNumber.subscriber_number.to_s }, '__PHONENUMBER.EXT__' => lambda { Faker::PhoneNumber.extension.to_s }, '__NUMBER__' => lambda { Faker::Number.number(5).to_s}, '__NUMBER(1)__' => lambda { Faker::Number.number(1).to_s}, '__NUMBER(2)__' => lambda { Faker::Number.number(2).to_s}, '__NUMBER(5)__' => lambda { Faker::Number.number(5).to_s}, '__NUMBER(8)__' => lambda { Faker::Number.number(8).to_s}, '__NUMBER(10)__' => lambda { Faker::Number.number(10).to_s}, '__NUMBER(1,28)__' => lambda { Faker::Number.between(1, 28).to_s }, '__NUMBER(1, 28)__' => lambda { Faker::Number.between(1, 28).to_s }, '__NUMBER(1,29)__' => lambda { Faker::Number.between(1, 29).to_s }, '__NUMBER(1, 29)__' => lambda { Faker::Number.between(1, 29).to_s }, '__NUMBER(1,30)__' => lambda { Faker::Number.between(1, 30).to_s }, '__NUMBER(1, 30)__' => lambda { Faker::Number.between(1, 30).to_s }, '__NUMBER(1,31)__' => lambda { Faker::Number.between(1, 31).to_s }, '__NUMBER(1, 31)__' => lambda { Faker::Number.between(1, 31).to_s }, '__SENTENCE__' => lambda { Faker::Lorem.sentence.to_s }, '__STREET__' => lambda { Faker::Address.street_address.to_s}, '__STATE__' => lambda { Faker::Address.state.to_s }, '__TIME__' => lambda { Time.now.to_s }, '__TIME.MMDDYY__' => lambda { Time.now.strftime "%m%d%y" }, '__TIME.MMDDYY.HM__' => lambda { Time.now.strftime "%m%d%y.%H%M" }, '__TIME.MMDDYY.HMS__' => lambda { Time.now.strftime "%m%d%y.%H%M%S" }, '__USERNAME__' => lambda { Faker::Internet.user_name.to_s }, '__ZIP__' => lambda { Faker::Address.zip_code.to_s }, } @hwnds = { :current => nil, :previous => nil, :handles => [] } end def expandMacro(_text_to_type) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " expandMacro(#{_text_to_type})" if _text_to_type.nil? || _text_to_type.empty? return "" end @transList.each_pair do |_k, _v| if _text_to_type.include?(_k.to_s) _dynamicStr = _v.call.to_s _text_to_type.gsub!(_k.to_s, _dynamicStr) elsif _text_to_type.match(/#{_k.to_s}/) _dynamicStr = _v.call(_text_to_type.to_s) _text_to_type.gsub!(/#{_k.to_s}/, _dynamicStr) end end puts __FILE__ + (__LINE__).to_s + " expandMacro : #{_text_to_type}" _text_to_type.to_s end def reset(stanza=nil) setEnableFrameSearch(false) if stanza.is_a?(Hash) && stanza.has_key?('page') && stanza['page'].has_key?('frames') puts __FILE__ + (__LINE__).to_s + " frames => #{stanza['page']['frames']}"; setEnableFrameSearch(stanza['page']['frames']) # setEnableFrameSearch(!stanza['page']['frames'].to_s.match(/true/i).nil?) end end def resetFrameSearch() setEnableFrameSearch(false) end def setEnableFrameSearch(b) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " setEnabledFrameSearch(#{b})" @userFrameSearch=b end def getFrameSearch() @userFrameSearch end def isSwitchFrame?() @userFrameSearch.is_a?(String) && @userFrameSearch.match(/^\s*frame\s*\(/i) end def enableFrameSearch() Scoutui::Logger::LogMgr.instance.debug "EnableFrameSearch(true)" setEnableFrameSearch(true) end def isFrameSearch?() @userFrameSearch.is_a?(TrueClass) || (@userFrameSearch.is_a?(String) && @userFrameSearch.match(/true/i)) end def isCSS(_locator) rc=nil if _locator.match(/^css\=/i) rc = _locator.match(/\s*(css\=.*)/i)[1].to_s.strip elsif _locator.match(/^#/i) rc=_locator.strip end rc end def setCurrentWindow(_w) if @hwnds[:previous].nil? @hwnds[:previous]=_w else @hwnds[:previous]=@hwnds[:current] end @hwnds[:current]=_w end def resetTimeout(t=30) setTimeout(t) end def setTimeout(_t) @timeout=_t.to_i end def getTimeout() @timeout.to_i end def isAssignments?(_action) !_action.match(/^\s*(assignments)\s*$/).nil? end def isAssignVar?(_action) !_action.match(/assign\(([\w]+)\s*\,(.*)\)\s*$/i).nil? end def isSelectWindow?(_action) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " isSelectWindow?(#{_action})" !_action.match(/^\s*select_window/i).nil? end def isExistsAlert?(_action) !_action.match(/(exist[s]*_*alert|existAlert|existsAlert|existsJsAlert|existsJsConfirm|existsJsPrompt)\(/i).nil? end def isFrame?(_action) _action.match(/^\s*(frame|switchframe|switch_frame)\s*\(/i) end def isHighlight?(_action) _action.match(/^\s*(highlight)/i) end def isVerifyElt?(_action) !_action.match(/(verifyelt|verifyelement)\(/i).nil? end def isClick?(_action) !_action.match(/click\(/i).nil? end def isGetAlert?(_action) !_action.match(/(get_*alert|clickjsconfirm|clickjsprompt|clickjsalert)/i).nil? end def isFillForm?(_action) !_action.match(/fillform\(/i).nil? end def isConnect?(_action) !_action.match(/^\s*connect\s*$/i).nil? end def isLoadDB?(_action) !_action.match(/^\s*loaddb\s*$/i).nil? end def isExecuteCommands?(_action) !_action.match(/^\s*executecommand[s]*\s*$/i).nil? end def isDefineCommands?(_action) !_action.match(/^\s*definecommand[s]*\s*$/i).nil? end def isLoadData?(_action) !_action.match(/^\s*loaddata\s*$/i).nil? end def isLoadRequirements?(_action) !_action.match(/^\s*loadrequirements\s*$/i).nil? end def isMouseOver?(_action) !_action.match(/mouseover\(/).nil? end def isType?(_action) !_action.match(/type[\!]*\(/).nil? end def isSubmitForm?(_action) !_action.match(/submitform\(/).nil? end def isVerifyForm?(_action) !_action.match(/verifyform\(/).nil? end def isPause?(_action) !_action.match(/pause/).nil? end def isSelect?(_action) !_action.nil? && _action.match(/^\s*select\s*\(/i) end def isSleep?(_action) !_action.nil? && _action.match(/^\s*sleep\s*\(\s*\d+\s*\)\s*$/i) end def isUploadFiles?(_action) !_action.nil? && _action.match(/^\s*uploadfiles\s*$/i) end def isNavigate?(_action) !_action.nil? && _action.match(/(navigate|url)\(/i) end def isValid?(cmd) rc=true if isPause?(cmd) @totalCommands['pause']+=1 elsif isAssignVar?(cmd) @totalCommands['assignvar']+=1 elsif isAssignments?(cmd) @totalCommands['assignments']+=1 elsif isExistsAlert?(cmd) @totalCommands['existsAlert']+=1 elsif isFrame?(cmd) @totalCommands['frame']+=1 elsif isGetAlert?(cmd) @totalCommands['clickJsAlert']+=1 elsif isHighlight?(cmd) @totalCommands['highlight']+=1 elsif isVerifyElt?(cmd) @totalCommands['verifyelt']+=1 elsif isVerifyForm?(cmd) @totalCommands['verifyform']+=1 elsif isFillForm?(cmd) @totalCommands['fillform']+=1 elsif isSubmitForm?(cmd) @totalCommands['submitform']+=1 elsif isType?(cmd) @totalCommands['type']+=1 elsif isClick?(cmd) @totalCommands['click']+=1 elsif isMouseOver?(cmd) @totalCommands['mouseover']+=1 elsif isConnect?(cmd) @totalCommands['connect']+=1 elsif isExecuteCommands?(cmd) @totalCommands['executecommands']+=1 elsif isDefineCommands?(cmd) @totalCommands['definecommands']+=1 elsif isLoadDB?(cmd) @totalCommands['loaddb']+=1 elsif isLoadData?(cmd) @totalCommands['loaddata']+=1 elsif isLoadRequirements?(cmd) @totalCommands['loadrequirements']+=1 elsif isSelect?(cmd) @totalCommands['select']+=1 elsif isNavigate?(cmd) @totalCommands['navigate']+=1 elsif isSelectWindow?(cmd) @totalCommands['select_window']+=1 elsif isSleep?(cmd) @totalCommands['sleep']+=1 elsif isUploadFiles?(cmd) @totalCommands['uploadfiles']+=1 else rc=false end rc end end end