require 'singleton' require 'faker' module Scoutui::Base class UserVars include Singleton attr_accessor :globals attr_accessor :userVars def initialize @userVars={} @globals={ :accounts => '/tmp/qa/accounts.yaml', :browser => 'chrome', :userid => nil, :password => nil, :host => nil, :localization => 'en-us' } end def dump() @globals.each_pair do |k, v| Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " #{k} => #{v}" end @userVars.each_pair do |k, v| Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " #{k} => #{v}" end end def getViewPort() _viewport = Scoutui::Base::UserVars.instance.getVar('eyes.viewport') if _viewport.nil? || _viewport.empty? return nil end arr=Scoutui::Base::UserVars.instance.getVar('eyes.viewport').match(/^\s*(\d+)\s*x\s*(\d+)\s*$/i) _sz={} if arr.size==3 _sz = {width: arr[1].to_i, height: arr[2].to_i} end _sz end def getBrowserType() @globals[:browser].to_sym end def getHost() @globals[:host].to_s end def isTestReport?(_val) # _val.match(/^\s*TestData\(.*\)\.getReport\(.*\)\s*$/) _val.match(/^\s*TestData\(.*?\)\.getReport\(.*?\)((\.get\(.*?\))*)\s*$/) end def getTestReportGetter(_val) _m = isTestReport?(_val) if _m return _m[1] end nil end def isTestData?(_val) return false if _val.nil? _val.match(/^\s*TestData\(.*\)\.getData\(.*\)\s*$/) end def normalize(s) if s.nil? || s.empty? return s end begin if Scoutui::Base::UserVars.instance.isTestData?(s) _dataID=s.match(/^\s*TestData\((.*)\)\.getData\(.*\)\s*$/)[1].to_s _getter=s.match(/^\s*TestData\((.*)\)\.(getData\(.*\))\s*$/)[2].to_s return Scoutui::Base::TestData.instance.get(_dataID, _getter) end _vars = s.scan(/(\$\{.*?\})/) _vars.each do | _v| if _v.length==1 _u = Scoutui::Base::UserVars.instance.get(_v[0].to_s) puts __FILE__ + (__LINE__).to_s + " Normalize(#{_v}) => #{_u}" s.gsub!(_v[0].to_s, _u) end end rescue => ex puts __FILE__ + (__LINE__).to_s + " #{ex.class}" puts ex.backtrace end s end def get(_k) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " get(#{_k})" foundKey=true k=_k if _k.is_a?(Array) k=_k[0].to_s end v=k _rc = k.match(/\$\{(.*)\}$/) # Needs refactoring! if k=='${userid}' || k=='__USERID__' k=:userid elsif k=='${password}' || k=='__PASSWORD__' k=:password elsif k=='${host}' || k=='__HOST__' k=:host elsif k=='${lang}' || k=='${__LOC__}' || k=='${LOC}' || k=='__LOC__' k=:lang elsif k=='${browser}' || k=='__BROWSER__' k=:browser elsif k=='${role}' || k=='__ROLE__' k=:role elsif k.is_a?(Symbol) foundKey=true elsif k=='__random_email__' return Faker::Internet.email elsif !_rc.nil? k=_rc[1].to_s Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " User Var found => #{k}" if Scoutui::Utils::TestUtils.instance.getTestConfig().has_key?("user_vars") if Scoutui::Utils::TestUtils.instance.getTestConfig()["user_vars"].has_key?(k) v=Scoutui::Utils::TestUtils.instance.getTestConfig()["user_vars"][k].to_s end end elsif @globals.has_key?(k) foundKey=true else foundKey=false end Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " get(#{k}) => #{@globals.has_key?(k)}" if @globals.has_key?(k) && foundKey v=@globals[k] end Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " get(#{k}) => #{@globals.has_key?(k)} ==> #{v.to_s}" if Scoutui::Utils::TestUtils.instance.isDebug? v end def assignUserVar(_k, _v) @userVars[_k] = _v end def getUserVar(_k) rc=nil if @userVars.has_key?(_k) rc=@userVars[_k] end rc end def removeUserVar(_k) if @userVars.has_key?(_k) @userVars.delete(_k) end end def set(k, v) setVar(k, v) v end def getVar(k) rc=nil if @globals.has_key?(k) rc=@globals[k].to_s end ## TODO: refactor! if rc.nil? rc = get(k) end rc end def setVar(k, v) @globals[k]=v v end end end