class Scoutui::Eyes::Utils

Public Instance Methods

download_diffs(results, view_key, destination) click to toggle source
# File lib/scoutui/eyes/utils.rb, line 65
def download_diffs(results, view_key, destination)
  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " download_diffs(#{results}, #{destination})"

  # Check if BATCH
  if  /\/batches\/\d+\/(?<batchId>\d+)/.match(results.url)
    session_id = get_session_id_from_batch(results.url)
    batch_id = get_batch_id_from_batch(results.url)
  else
    session_id = get_session_id(results.url)
    batch_id = get_batch_id(results.url)
  end

  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
download_images(diff_urls, destination) click to toggle source
# File lib/scoutui/eyes/utils.rb, line 84
def download_images(diff_urls, destination)
  rc=true

  begin
    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


  rescue => ex
    Scoutui::Logger::LogMgr.instance.warn __FILE__ + (__LINE__).to_s + " #{ex.class}: download_images."
    Scoutui::Logger::LogMgr.instance.info ex.backtrace
    rc=false
  end

  rc

end
get_batch_id(url) click to toggle source
# File lib/scoutui/eyes/utils.rb, line 42
def get_batch_id(url)
  /sessions\/(?<batchId>\d+)/.match(url)[1]
end
get_batch_id_from_batch(url) click to toggle source
# File lib/scoutui/eyes/utils.rb, line 33
def get_batch_id_from_batch(url)
  /batches\/(?<batchId>\d+)/.match(url)[1]
end
get_diff_urls(batch_id, session_id, view_key) click to toggle source
# File lib/scoutui/eyes/utils.rb, line 106
    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|
        Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " elem => #{elem}"
        if (!elem.nil?)
          Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " | o isMatching : #{elem['isMatching']}"
#         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
get_session_id(url) click to toggle source
# File lib/scoutui/eyes/utils.rb, line 38
def get_session_id(url)
  /sessions\/\d+\/(?<sessionId>\d+)/.match(url)[1]
end
get_session_id_from_batch(url) click to toggle source
# File lib/scoutui/eyes/utils.rb, line 29
def get_session_id_from_batch(url)
  /batches\/\d+\/(?<sessionId>\d+)/.match(url)[1]
end
print_results(results, view_key) click to toggle source
sanitize_filename(filename) click to toggle source
# File lib/scoutui/eyes/utils.rb, line 13
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