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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Object



42
43
44
# 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) ⇒ Object



33
34
35
# 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) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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|
    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



38
39
40
# 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) ⇒ Object



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

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


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/scoutui/eyes/utils.rb', line 46

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