require 'singleton' require 'pp' require 'optparse' module Scoutui::Utils class TestUtils include Singleton attr_accessor :options attr_accessor :app_model attr_accessor :currentTest attr_accessor :metrics attr_accessor :final_rc attr_accessor :coverage attr_accessor :execution_status def initialize @currentMessage="" @coverage={:pages => []} @final_rc=false @metrics=nil @execution_status = :ready @env_list={:accounts => 'SCOUTUI_ACCOUNTS', :browser => 'SCOUTUI_BROWSER', :applitools_api_key => 'APPLITOOLS_API_KEY'} @options={} @currentTest={:reqid => 'UI', :testcase => '00' } [:accounts, :browser, :capabilities, :diffs_dir, :test_file, :host, :loc, :title, :viewport, :userid, :password, :role, :json_config_file, :page_model, :test_config, :debug].each do |o| @options[o]=nil end @options[:include_expected_as_asserts]=false @options[:role]=nil @options[:sauce_name]='unnamed' @options[:enable_eyes]=false @options[:enable_fullscreen]=true @options[:enable_sauce]=false @options[:log_level]=:info # :debug, :info, :warn, :error, :fatal @options[:match_level]='layout' @options[:default_wait]=30 # seconds @options[:debug]=false @options[:fail_fast]=false @app_model=nil Scoutui::Base::UserVars.instance.set('eyes.viewport', '1024x768') Scoutui::Base::UserVars.instance.set('eyes.viewport', nil) end def setState(s, _d=nil) @execution_status=s @currentMessage=_d if !_d.nil? end def currentMessage @currentMessage end def abort? @execution_status==:abort end def getState() @execution_status end def getPageCoverage() @coverage[:pages] end def addPageCoverage(p) @coverage[:pages] << p end def isFailFast? @options[:fail_fast] end def getFinalRc() @final_rc end def setFinalRc(b) @final_rc=b end def getMetrics() @metrics end def setMetrics(_m) @metrics=_m end def getReq() @currentTest[:reqid] end def setReq(_r='UI') @currentTest[:reqid]=_r end def loadModel(f=nil) if f.nil? return nil end begin @app_model = Scoutui::ApplicationModel::QModel.new(f) @app_model.getAppModel() rescue => ex raise "ErrorLoad : #{f.to_s}" end @app_model end def getForm(s) _h = getPageElement(s) Scoutui::Base::QForm.new(_h) end def getPageElement(s) rc=nil if !@app_model.nil? rc=@app_model.getPageElement(s) end rc end def getAppModel() @app_model end def getViewPort() rc=nil if !options[:viewport].nil? arr=options[:viewport].match(/(\d+)\s*x\s*(\d+)$/i) if arr.size==3 rc = {width: arr[1].to_i, height: arr[2].to_i} end end rc end def parseCommandLine() OptionParser.new do |opt| opt.on('-c', '--config TESTFILE') { |o| if !o.nil? @options[:json_config_file]=o jFile = File.read(@options[:json_config_file]) @options[:test_config]=jsonData=JSON.parse(jFile) end } opt.on('--accounts [Account]') { |o| @options[:accounts] = o } opt.on('-b', '--browser [TYPE]', [:chrome, :firefox, :ie, :safari, :phantomjs], "Select browser (chrome, ie, firefox, safari)") { |o| @options[:browser] = o } opt.on('--capabilities CAP') { |o| @options[:capabilities]=o jFile = File.read(o) @options[:capabilities]=jsonData=JSON.parse(jFile, :symbolize_names => true) } opt.on('--loglevel Level') { |o| if o.match(/error/i) @options[:log_level] = :error elsif o.match(/fatal/i) @options[:log_level] = :fatal elsif o.match(/info/i) @options[:log_level] = :info elsif o.match(/warn/i) @options[:log_level] = :warn elsif o.match(/debug/i) @options[:log_level] = :debug end Scoutui::Logger::LogMgr.instance.setLevel(@options[:log_level]) } opt.on('--diffs Full Path') { |o| @options[:diffs_dir] = o } opt.on('-d', '--debug', 'Enable debug') { |o| @options[:debug] = true @options[:log_level] = :debug } opt.on('--dut DUT') { |o| @options[:dut]=o } opt.on('-h', '--host HOST') { |o| @options[:host] = o } opt.on('-i', '--include_expectations') { |o| @options[:include_expected_as_asserts] = true} opt.on('-l', '--lang LOCAL') { |o| @options[:loc] = o Scoutui::Base::UserVars.instance.setVar(:lang, @options[:loc].to_s) } opt.on('-k', '--key EyesLicense') { |o| options[:license_file] = o } opt.on('-a', '--app AppName') { |o| @options[:app] = o } opt.on('--match [LEVEL]', [:layout2, :layout, :strict, :exact, :content], "Select match level (layout, strict, exact, content)") { |o| @options[:match_level] = o } opt.on('--eyes:match [LEVEL]', [:layout2, :layout, :strict, :exact, :content], "Select match level (layout, strict, exact, content)") { |o| @options[:match_level] = o } opt.on('--failfast Bool', [:true, :false]) { |o| @options[:fail_fast] = !o.match(/true/i).nil? } opt.on("--pages a,b,c", Array, "List of page models") do |list| Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " list => #{list}" @options[:pages]=list loadModel(@options[:pages]) end opt.on('--pagemodel [PageModel]') { |o| @options[:page_model] = o loadModel(@options[:page_model].to_s) } opt.on('-w', '--wait WaitOnElement') { |o| if o.match(/\d+/) @options[:default_wait] = o.to_i end } opt.on('-t', '--title TITLE') { |o| @options[:title] = o } opt.on('-u', '--user USER_ID') { |o| @options[:userid] = o Scoutui::Base::UserVars.instance.setVar(:user, @options[:userid].to_s) } opt.on('-p', '--password PASSWORD') { |o| @options[:password] = o } opt.on('-e', '--eyes', "Toggle eyes") { @options[:enable_eyes]=true } opt.on('--eyes:run Bool', [:true, :false]) { |o| @options[:enable_eyes] = !o.match(/true/i).nil? } opt.on('--eyes:fullscreen Bool', [:true, :false]) { |o| @options[:enable_fullscreen] = !o.match(/true/i).nil? } opt.on('--eyes:app AppName') { |o| @options[:app] = o } opt.on('--eyes:title Title') { |o| @options[:title] = o } opt.on('--eyes:viewport [resolution]') { |o| @options[:viewport] = o } opt.on('--role ROLE') { |o| @options[:role]=o } opt.on('-s', '--sauce', "Toggle SauceLabs") { @options[:enable_sauce]=true } opt.on('--sauce:run NAME', [:true, :false]) { |o| @options[:enable_sauce] = !o.match(/true/i).nil? } opt.on('--sauce_name NAME') { |o| @options[:sauce_name] = o } opt.on('--sauce:name NAME') { |o| @options[:sauce_name] = o } opt.on('--sauce:build id') { |o| @options[:sauce_build] = o } opt.on('--sauce:browser Browser') { |o| @options[:sauce_browser] = o } opt.on('--sauce:browsername BrowserName') { |o| @options[:sauce_browsername] = o } opt.on('--sauce:platform Platform') { |o| @options[:sauce_platform] = o } opt.on('--sauce:resolution Resolution') { |o| @options[:sauce_resolution] = o } opt.on('--sauce:version Version') { |o| @options[:sauce_version] = o } opt.on('--viewport [resolution]') { |o| @options[:viewport] = o Scoutui::Base::UserVars.instance.set('eyes.viewport', @options[:viewport]) } end.parse! if Scoutui::Utils::TestUtils.instance.isDebug? Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " " + @options.to_s Scoutui::Logger::LogMgr.instance.info "Test file => #{@options[:test_file]}" Scoutui::Logger::LogMgr.instance.info "Host => #{@options[:host]}" Scoutui::Logger::LogMgr.instance.info "Loc => #{@options[:loc]}" Scoutui::Logger::LogMgr.instance.info "Title => #{@options[:title]}" Scoutui::Logger::LogMgr.instance.info "Browser => #{@options[:browser]}" Scoutui::Logger::LogMgr.instance.info "UserID => #{@options[:userid]}" Scoutui::Logger::LogMgr.instance.info "Password => #{@options[:password]}" Scoutui::Logger::LogMgr.instance.info "Role => #{@options[:role]}" Scoutui::Logger::LogMgr.instance.info "Eyes => #{@options[:enable_eyes]}" Scoutui::Logger::LogMgr.instance.info "Eyes.Fullscreen => #{@options[:enable_fullscreen]}" Scoutui::Logger::LogMgr.instance.info "Test Cfg => #{@options[:json_config_file]}" Scoutui::Logger::LogMgr.instance.info "Match Level => #{@options[:match_level]}" Scoutui::Logger::LogMgr.instance.info "Accounts => #{@options[:accounts]}" Scoutui::Logger::LogMgr.instance.info "Viewport => #{@options[:viewport]}" Scoutui::Logger::LogMgr.instance.info "Viewport (Var) => #{Scoutui::Base::UserVars.instance.getViewPort().to_s}" Scoutui::Logger::LogMgr.instance.info "PageModel file => #{@options[:page_model].to_s}" Scoutui::Logger::LogMgr.instance.info "Sauce.Run => #{@options[:sauce_run].to_s}" Scoutui::Logger::LogMgr.instance.info "Sauce.Platform => #{@options[:sauce_platform].to_s}" Scoutui::Logger::LogMgr.instance.info "Failfast => #{@options[:fail_fast]}" end @options end def getCapabilities() @options[:capabilities]=Hash.new if @options.has_key?(:capabilities) && @options[:capabilities].nil? if @options.has_key?(:sauce_platform) && !@options[:sauce_platform].nil? @options[:capabilities][:platform]=@options[:sauce_platform] end if !sauceResolution().nil? @options[:capabilities][:screenResolution]=sauceResolution() end if @options.has_key?(:sauce_browser) @options[:capabilities][:browser] = @options[:sauce_browser] end if @options.has_key?(:sauce_version) @options[:capabilities][:version] = @options[:sauce_version] end if @options.has_key?(:sauce_name) @options[:capabilities][:name] = @options[:sauce_name] end if @options.has_key?(:sauce_build) @options[:capabilities][:build] = @options[:sauce_build] end if @options.has_key?(:sauce_browsername) @options[:capabilities][:browserName] = @options[:sauce_browsername] end Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " caps => #{@options[:capabilities].to_s}" @options[:capabilities][:tags]=[@options[:sauce_name], "ScoutUI"] @options[:capabilities] end def getHost() @options[:host].to_s end def setDebug(b) @options[:debug]=b end def isDebug? @options[:debug] end def eyesEnabled? @options[:enable_eyes] end def eyesFullscreen? @options[:enable_fullscreen] end def sauceEnabled? @options[:enable_sauce] end def saucePlatform() @options[:sauce_platform] end def getLicenseFile() @options[:license_file].to_s end def sauceResolution() @options[:sauce_resolution] end def getRole() @options[:role] end def getSauceName() @options[:sauce_name].to_s end def getSauceBuild() @options[:sauce_build] || nil end def getBrowser() getBrowserType() end def getBrowserType() @options[:browser] end def getDefaultWait() @options[:default_wait] end def assertExpected? @options[:include_expected_as_asserts] end def hasTestConfig? !@options[:json_config_file].nil? end def testConfigFile() @options[:json_config_file] end # Returns JSON file contents/format def getTestSettings() Scoutui::Logger::LogMgr.instance.setLevel(@options[:log_level]) [:accounts, :browser, :dut, :host, :role, :userid, :password].each do |k| Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " opt[test_config].has_key(#{k.to_s}) => #{@options[:test_config].has_key?(k.to_s)}" if Scoutui::Utils::TestUtils.instance.isDebug? Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " options[#{k}] : #{@options[k].to_s}" if Scoutui::Utils::TestUtils.instance.isDebug? if @options.has_key?(k) && !@options[k].nil? Scoutui::Base::UserVars.instance.set(k, @options[k].to_s) elsif @options[:test_config].has_key?(k.to_s) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " opts[#{k}].nil => #{@options[k].nil?}" if Scoutui::Utils::TestUtils.instance.isDebug? # Ensure commnand line takes precedence if !@options[k].nil? Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " opt[#{k.to_s} => #{@options[k].to_s}" if Scoutui::Utils::TestUtils.instance.isDebug? Scoutui::Base::UserVars.instance.set(k, @options[k].to_s) else Scoutui::Base::UserVars.instance.set(k, @options[:test_config][k.to_s].to_s) end elsif @env_list.has_key?(k) # If an ENV is available, use it. Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " #{k} => ENV(#{@env_list[k]}) = #{ENV[@env_list[k].to_s]}" if Scoutui::Utils::TestUtils.instance.isDebug? Scoutui::Base::UserVars.instance.set(k, ENV[@env_list[k].to_s]) end end Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " test_config => #{@options[:test_config]}" if Scoutui::Utils::TestUtils.instance.isDebug? if @options[:test_config].has_key?('dut') && @options.has_key?(:dut) @options[:test_config]['dut']=@options[:dut] end # Applitools Eyes settings if @options[:test_config].has_key?('eyes') ['match_level', 'title', 'app', 'viewport'].each do |k| _v=nil if @options[:test_config]['eyes'].has_key?(k) _v=@options[:test_config]['eyes'][k].to_s end if !@options[k.to_sym].nil? Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " eyes[#{k.to_s}] override with #{@options[k.to_sym]}" _v=@options[k.to_sym].to_s end Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " #{k} => #{_v}" Scoutui::Base::UserVars.instance.set('eyes.' + k, _v) if !_v.nil? end end @options[:test_config] end def getTestConfig() @options[:test_config] end def match_level() @options[:match_level] end def getDiffDir() @options[:diffs_dir] end def getUserId() @options[:userid] end def getUser() getUserId() end def getPassword() @options[:password] end def testFile() @options[:test_file] end def host() @options[:host] end def loc() @options[:loc] end def localization() loc() end def appName() @options[:app].to_s end def title() @options[:title] end def transpile(_val) _uservars = _val.scan(/\$\{(.*?)\}/) _v2 = _val for i in 0 .. (_uservars.size - 1) _t = Scoutui::Base::UserVars.instance.get(_uservars[i][0]) puts __FILE__ + (__LINE__).to_s + " translate(#{_uservars[i][0]}) => #{_t}" if !_t.nil? && _uservars[i][0]!=_t _v2.gsub!("\$\{#{_uservars[i][0]}\}", _t) end puts __FILE__ + (__LINE__).to_s + " value => #{_v2}" end ## Global replaces _text_to_type = Scoutui::Base::UserVars.instance.get(_v2) puts __FILE__ + (__LINE__).to_s + " rawText : #{_text_to_type}" _text_to_type = Scoutui::Commands::Utils.instance.expandMacro(_text_to_type) puts __FILE__ + (__LINE__).to_s + " Text to type : #{_text_to_type}" _text_to_type end def expandVars(h) puts __FILE__ + (__LINE__).to_s + " expandVars(#{h})" h.each do |k, v| if v.is_a?(Hash) || v.is_a?(Array) expandVars(v) else puts("k is #{k}, value is #{v}") h[k]=transpile(v) end end h end end end