Class: Scoutui::Eyes::Utils

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/scoutui/eyes/utils.rb

Instance Method Summary collapse

Instance Method Details

#download_diffs(results, view_key, destination) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/scoutui/eyes/utils.rb', line 57

def download_diffs(results, view_key, destination)
  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " download_diffs(#{results}, #{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

#download_images(diff_urls, destination) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/scoutui/eyes/utils.rb', line 68

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

#get_batch_id(url) ⇒ Object



34
35
36
# File 'lib/scoutui/eyes/utils.rb', line 34

def get_batch_id(url)
  /sessions\/(?<batchId>\d+)/.match(url)[1]
end

#get_diff_urls(batch_id, session_id, view_key) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/scoutui/eyes/utils.rb', line 77

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}"
    if (!elem.nil?)
      puts __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) ⇒ Object



30
31
32
# File 'lib/scoutui/eyes/utils.rb', line 30

def get_session_id(url)
  /sessions\/\d+\/(?<sessionId>\d+)/.match(url)[1]
end


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/scoutui/eyes/utils.rb', line 38

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

#sanitize_filename(filename) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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