require 'singleton' require 'pp' require 'optparse' module Scoutui::Eyes class Utils include Singleton 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 get_session_id(url) /sessions\/\d+\/(?\d+)/.match(url)[1] end def get_batch_id(url) /sessions\/(?\d+)/.match(url)[1] end def print_results(results, view_key) if results.is_passed print "Your test was passed!\n" elsif results.is_new print "Created new baseline, this is a new test or/and new configuration!" else print "Your test was failed!\n" print "#{results.mismatches} out of #{results.steps} steps failed \n" print "Here are the failed steps:\n" session_id = get_session_id(results.url) batch_id = get_batch_id(results.url) diff_urls = get_diff_urls(batch_id, session_id, view_key) diff_urls.each do |index, diff_url| print "Step #{index} --> #{diff_url} \n" end print "For more details please go to #{results.url} to review the differences! \n" end end def download_diffs(results, view_key, destination) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " download_diffs(#{destination}" session_id = get_session_id(results.url) batch_id = get_batch_id(results.url) diff_urls = get_diff_urls(batch_id, session_id, view_key) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " session_id : #{session_id}, batch_id : #{batch_id}" download_images(diff_urls, destination) end def download_images(diff_urls, destination) diff_urls.each do |index, elem| save_name = sanitize_filename(elem[:tag]) + ".#{elem[:isMatching].to_s}.step_#{elem[:index]}_diff.png" File.open("#{destination}/#{save_name}", 'wb') do |file| file.write HTTParty.get(elem[:url]) end end end def get_diff_urls(batch_id, session_id, view_key) info = "https://eyes.applitools.com/api/sessions/batches/#{batch_id}/#{session_id}/?ApiKey=#{view_key}&format=json" print "\r\n info:" + info + "\r\n" diff_template = "https://eyes.applitools.com/api/sessions/batches/#{batch_id}/#{session_id}/steps/%s/diff?ApiKey=#{view_key}" print "\r\n Template:" + diff_template + "\r\n" diff_urls = Hash.new response = HTTParty.get(info) data = JSON.parse(response.body) print (data) index = 1 data['actualAppOutput'].each do |elem| puts __FILE__ + (__LINE__).to_s + " elem => #{elem}" puts __FILE__ + (__LINE__).to_s + " | o isMatching : #{elem['isMatching']}" if (!elem.nil?) # diff_urls[index] = diff_template % [index] diff_urls[index] = { :tag => elem['tag'].to_s, :isMatching => elem['isMatching'], :url => diff_template % [index], :index => index } end index+=1 end diff_urls end end end