require 'httparty' module Scoutui::Eyes class EyeScout attr_accessor :eyes attr_accessor :testResults attr_accessor :strategy def teardown() @strategy.quit() end def navigate(url) @strategy.navigate(url) end def drv() @strategy.getDriver() end def getStrategy() @strategy end def eyes() @eyes end #---- 5150 ----- def get_session_id(url) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " -- get_session_id(#{url}) ==" _s=/sessions\/(?\d+)/.match(url)[1] end def get_accountid(url) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " -- get_account_id(#{url}) ==" uri = URI(url) queryString = CGI::parse(uri.query) accountid = queryString['accountId'] || queryString['accountid'] Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " => accountId : #{accountid}" # _s=/sessions\/(?\d+)/.match(url)[1] accountid.is_a?(Array) ? accountid[0] : accountid end def get_diff_urls(session_id, account_id, view_key) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " == get_diffs_url(#{session_id} ==" info = "https://eyes.applitools.com/api/sessions/#{session_id}?ApiKey=#{view_key}&format=json" #info = "https://eyes.applitools.com/api/sessions/#{session_id}?accountId=#{account_id}&format=json" diff_template = "https://eyes.applitools.com/api/sessions/#{session_id}/steps/%s/diff?ApiKey=#{view_key}" diff_template = "https://eyes.applitools.com/api/sessions/#{session_id}?accountId=#{account_id}&format=json" Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " diff_template => #{diff_template}" diff_urls = Hash.new Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " info => #{info}" response = HTTParty.get(info) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " RESP => #{response.to_s}" begin data = JSON.parse(response.body) index = 1 data['actualOutput'].each do |elem| if !elem.nil? && (elem['isMatching'] == false) #diff_urls[index] = diff_template % [index] Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " #{index.to_s} :#{elem['tag'].to_s}" #diff_urls[index] = diff_template % [index] diff_urls[index] = { :tag => elem['tag'].to_s, :url => diff_template % [index] } index+=1 end end diff_urls rescue JSON::ParserError ; end end def sanitize_filename(filename) # Split the name when finding a period which is preceded by some # character, and is followed by some character other than a period, # if there is no following period that is followed by something # other than a period fn = filename.split /(?<=.)\.(?=[^.])(?!.*\.[^.])/m # We now have one or two parts (depending on whether we could find # a suitable period). For each of these parts, replace any unwanted # sequence of characters with an underscore fn.map! { |s| s.gsub /[^a-z0-9\-]+/i, '_' } # Finally, join the parts with a period and return the result return fn.join '.' end def download_images(diff_urls, destination) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " download_images(#{diff_urls}, #{destination}" diff_urls.each do |index, elem| save_name = sanitize_filename(elem[:tag]) + ".step_#{index}_diff.png" Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " => download : #{save_name}" File.open("#{destination}/#{save_name}", 'wb') do |file| file.write HTTParty.get(elem[:url]) end end end def saveDiffs(_eyes, results, outdir, view_key) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " saveDiffs(#{outdir.to_s})" Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " results => #{results.to_s}" Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " | steps : #{results.steps.to_s}" session_id = get_session_id(results.url) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " session => #{session_id}" account_id = get_accountid(results.url) diffs = get_diff_urls(session_id, view_key) diffs.each do |d| Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " #{d.to_s}" end download_images(diffs, outdir) end #---- END 5150 def closeOut() # Scoutui::Base::QHarMgr.instance.stop('/tmp/scoutui.har') return if !Scoutui::Utils::TestUtils.instance.eyesEnabled? @testResults = eyes().close(false) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " @eyes => #{eyes().to_s}" Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "| app_name : #{eyes().app_name}" Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "| match_level: #{eyes().match_level}" Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "| viewport_size: #{eyes().viewport_size.to_s}" eyes().viewport_size.to_hash.each_pair do |k, v| Scoutui::Logger::LogMgr.instance.debug " o " + k.to_s + " : " + v.to_s end Testmgr::TestReport.instance.getReq("EYES").testcase('Images').add(@testResults.steps.to_i > 0, "Verify at least 1 shot taken (#{@testResults.steps.to_s} shots)") Testmgr::TestReport.instance.getReq("EYES").testcase('Images').add(@testResults.missing.to_i==0, "Verify Eyes did not miss any screens (#{@testResults.missing.to_s} screens)") # 5150 _diffdir=Scoutui::Utils::TestUtils.instance.getDiffDir() if ENV.has_key?('APPLITOOLS_VIEW_KEY') && !_diffdir.nil? if Dir.exists?(_diffdir) # Legacy_saveDiffs(eyes(), @testResults, _diffdir, ENV['APPLITOOLS_VIEW_KEY']) Scoutui::Eyes::Utils.instance.download_diffs(@testResults, ENV['APPLITOOLS_VIEW_KEY'], _diffdir) else Scoutui::Logger::LogMgr.instance.warn " Specified Visual Diff folder does not exist - #{_diffdir.to_s}. Unable to download diffs" end else Scoutui::Logger::LogMgr.instance.info " Unable to download diff images - APPLITOOLS_VIEW_KEY not defined" end eyes().abort_if_not_closed if !eyes().nil? end def check_window(tag, region=nil) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " check_window(#{tag.to_s}, #{region})" if Scoutui::Utils::TestUtils.instance.isDebug? return if !Scoutui::Utils::TestUtils.instance.eyesEnabled? if region.nil? Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " check_window(#{tag.to_s})" eyes().check_window(tag.to_s) else Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " check_window.fullshot" f = eyes().force_fullpage_screenshot if region.match(/^css\s*\=\s*/) _css = region.gsub(/^css\s*\=\s*/,"").to_s Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " snapshot(region.css) : #{_css}" eyes().check_region(:css, _css, tag) else Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "check_window(:xpath, #{region}, #{tag})" eyes().check_region(:xpath, region, tag) end eyes().force_fullpage_screenshot = f end end def generateReport() if Scoutui::Utils::TestUtils.instance.eyesEnabled? Scoutui::Logger::LogMgr.instance.debug " testResults.methods = #{@testResults.methods.sort.to_s}" Scoutui::Logger::LogMgr.instance.info " Eyes.TestResults.isNew : #{@testResults.is_new.to_s}" Scoutui::Logger::LogMgr.instance.info " Eyes.TestResults.Matches : #{@testResults.matches.to_s}" Scoutui::Logger::LogMgr.instance.info " Eyes.TestResults.Mismatches : #{@testResults.mismatches.to_s}" Scoutui::Logger::LogMgr.instance.info " Eyes.TestResults.Passed : #{@testResults.passed?.to_s}" Scoutui::Logger::LogMgr.instance.info " Eyes.TestResults.Steps : #{@testResults.steps.to_s}" end Testmgr::TestReport.instance.getReq("Execution").testcase('Status').add(!Scoutui::Utils::TestUtils.instance.abort?, "Verify execution did not abort. #{Scoutui::Utils::TestUtils.instance.currentMessage}") Scoutui::Logger::LogMgr.instance.info " TestReport.Tap => #{@testResults.to_json}" Testmgr::TestReport.instance.report() # Testmgr::TestReport.instance.generateReport() metrics=Testmgr::TestReport.instance.getMetrics() Scoutui::Logger::LogMgr.instance.info "Metrics => #{metrics}" Scoutui::Utils::TestUtils.instance.setMetrics(metrics) end def getResults() @testResults end def initialize(browserType) @testResults=nil browserType = Scoutui::Base::UserVars.instance.getBrowserType() viewport_size = Scoutui::Base::UserVars.instance.getViewPort() Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " browserType => #{browserType}" Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " viewport : #{viewport_size}" Testmgr::TestReport.instance.setDescription('ScoutUI Test') Testmgr::TestReport.instance.setEnvironment(:qa, Scoutui::Utils::TestUtils.instance.getHost()) Testmgr::TestReport.instance.setHost(Scoutui::Base::UserVars.instance.get(:host)) Testmgr::TestReport.instance.setBrowserUnderTest(browserType) Testmgr::TestReport.instance.addRequirement('UI') Testmgr::TestReport.instance.getReq('UI').add(Testmgr::TestCase.new('visible_when', "visible_when")) Testmgr::TestReport.instance.addRequirement('Command') Testmgr::TestReport.instance.getReq('Command').add(Testmgr::TestCase.new('isValid', "isValid")) Testmgr::TestReport.instance.addRequirement('DB') Testmgr::TestReport.instance.getReq('DB').add(Testmgr::TestCase.new('DB', "DB")) Testmgr::TestReport.instance.addRequirement('Execution') Testmgr::TestReport.instance.getReq('Execution').add(Testmgr::TestCase.new('Status', "Status")) Testmgr::TestReport.instance.getReq('UI').add(Testmgr::TestCase.new('expectJsAlert', 'expectJsAlert')) if Scoutui::Utils::TestUtils.instance.isDebug? Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " setup() : #{browserType}" Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " eyes => " + Scoutui::Utils::TestUtils.instance.eyesEnabled?.to_s Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " viewport => #{viewport_size}" Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " eyes cfg => #{@eyesRecord}" Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " eyes.title => " + Scoutui::Base::UserVars.instance.getVar('eyes.title') Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " eyes.app => " + Scoutui::Base::UserVars.instance.getVar('eyes.app') Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " eyes.match_level => " + Scoutui::Base::UserVars.instance.getVar('eyes.match_level') end begin # Scoutui::Base::QHarMgr.instance.start() # @profile.proxy = Scoutui::Base::QHarMgr.instance.getSeleniumProfile() @strategy = Scoutui::Commands::Strategy.new() @eyes=Scoutui::Eyes::EyeFactory.instance.createEyes() Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " eyes => #{eyes}" if Scoutui::Utils::TestUtils.instance.isDebug? ## TBD - move the following into eye_scout ?? if Scoutui::Utils::TestUtils.instance.eyesEnabled? @driver = @eyes.open( app_name: Scoutui::Base::UserVars.instance.getVar('eyes.app'), # @eyesRecord['app'], test_name: Scoutui::Base::UserVars.instance.getVar('eyes.title'), # @eyesRecord['title'], viewport_size: viewport_size, # viewport_size: {width: 800, height: 600}, driver: @strategy.getDriver()) end rescue => ex Scoutui::Logger::LogMgr.instance.warn ex.backtrace end end end end