Class: IFD_Assertion

Inherits:
Object
  • Object
show all
Defined in:
lib/anhpham/methods/IFD_Assertion_methods.rb

Class Method Summary collapse

Class Method Details

.assert_string_contain(expected, actual) ⇒ Object



83
84
85
86
87
# File 'lib/anhpham/methods/IFD_Assertion_methods.rb', line 83

def self.assert_string_contain(expected, actual)
  unless (actual.to_s).include? (expected.to_s)
    raise ("*** ASSERTION ERROR: \nExpected: #{expected}. \nGot: #{actual}.")
  end
end

.assert_string_equal(expected, actual) ⇒ Object



89
90
91
92
93
# File 'lib/anhpham/methods/IFD_Assertion_methods.rb', line 89

def self.assert_string_equal(expected, actual)
  if expected.to_s != actual.to_s
    raise ("*** ASSERTION ERROR: \nExpected: #{expected}. \nGot: #{actual}.")
  end
end

.compare_image(actual_img_access_type, actual_img_access_name, excp_img_access_type, excp_img_access_name) ⇒ Object

Method to compare two images param 1 : String : Locator type (id, name, class, xpath, css, url) param 2 : String : Locator value param 3 : String : Locator type (id, name, class, xpath, css, url, image_name) param 4 : String : Locator value



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/anhpham/methods/IFD_Assertion_methods.rb', line 108

def self.compare_image(actual_img_access_type, actual_img_access_name, excp_img_access_type, excp_img_access_name)
  if actual_img_access_type == 'url'
    actual_img_url = actual_img_access_name
  elsif actual_img_access_type == 'image_name'
    actual_img_url = $test_data_dir + '/actual_images/' + actual_img_access_name
  else
    actual_img_url = get_element_attribute(actual_img_access_type, actual_img_access_name, 'src')
  end

  if excp_img_access_type == 'url'
    expected_img_url = excp_img_access_name
  elsif excp_img_access_type == 'image_name'
    expected_img_url = $test_data_dir + '/expected_images/' + excp_img_access_name
  else
    expected_img_url = get_element_attribute(excp_img_access_type, excp_img_access_name, 'src')
  end

  # replace 'https' with 'http' from actual image url
  if actual_img_url.include? 'https'
    actual_img_url['https'] = 'http'
  end

  # replace 'https' with 'http' from expected image url
  if expected_img_url.include? 'https'
    expected_img_url['https'] = 'http'
  end

  if expected_img_url.include? '.png'
    image_type = 'png'
  else
    image_type = 'jpg'
  end

  # Storing actual image locally
  open($test_data_dir + '/actual_images/actual_image.' + image_type, 'wb') do |file|
    file << open(actual_img_url).read
  end

  if actual_img_access_type == 'url'
    actual_img_url = $test_data_dir + '/actual_images/actual_image.' + image_type
  end

  # Storing Expected image locally
  if excp_img_access_type != 'image_name'
    open($test_data_dir + '/expected_images/expected_image.' + image_type, 'wb') do |file|
      file << open(expected_img_url).read
    end
    expected_img_url = $test_data_dir + '/expected_images/expected_image.' + image_type
  end

  # Verify image extension and call respective compare function
  if image_type == 'png'
    return compare_png_images(expected_img_url, actual_img_url)
  end

  compare_jpeg_images(expected_img_url, actual_img_url)
end

.compare_jpeg_images(expected_img_url, actual_img_url) ⇒ Object

Comparing jpg images



167
168
169
170
171
172
173
174
# File 'lib/anhpham/methods/IFD_Assertion_methods.rb', line 167

def self.compare_jpeg_images(expected_img_url, actual_img_url)
  if open(expected_img_url).read == open(actual_img_url).read
    return true
  else
    puts 'Difference in images'
    return false
  end
end

.compare_png_images(expected_img_url, actual_img_url) ⇒ Object

Comparing png images



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/anhpham/methods/IFD_Assertion_methods.rb', line 177

def self.compare_png_images(expected_img_url, actual_img_url)
  images = [
      ChunkyPNG::Image.from_file(expected_img_url),
      ChunkyPNG::Image.from_file(actual_img_url)
  ]

  diff = []

  images.first.height.times do |y|
    images.first.row(y).each_with_index do |pixel, x|
      diff << [x, y] unless pixel == images.last[x, y]
    end
  end

  if diff.length != 0
    puts "\npixels (total):     #{images.first.pixels.length}"
    puts "pixels changed:     #{diff.length}"
    puts "pixels changed (%): #{(diff.length.to_f / images.first.pixels.length) * 100}%"

    x, y = diff.map { |xy| xy[0] }, diff.map { |xy| xy[1] }
    images.last.rect(x.min, y.min, x.max, y.max, ChunkyPNG::Color.rgb(0, 255, 0))
    cur_time = Time.now.strftime('%Y%m%d%H%M%S%L')
    images.last.save($test_data_dir + "/image_difference/difference_#{cur_time}.png")

    puts "\nDifference between images saved as : difference_#{cur_time}.png\n"
    return false
  end
  true
end

.do_assertion_csv_tab_non_order(expected_obj, actual_obj) ⇒ Object

Assert two files, rows not in order and REMOVE 1 COLUMN OF ID



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/anhpham/methods/IFD_Assertion_methods.rb', line 4

def self.do_assertion_csv_tab_non_order(expected_obj, actual_obj)
  for i in (1..expected_obj.length - 1)
    expected_row = expected_obj[i].drop(1).to_s.split("\t")
    found = false
    for j in (1..actual_obj.length - 1)
      actual_row = actual_obj[j].drop(1).to_s.split("\t")
      if (expected_row == actual_row)
        found = true
        break
      end
    end
    assert(found, "Expected Record: [#{expected_obj[i].to_s}] is not included in reporting file")
  end
end

.do_assertion_json(expected_obj, actual_obj, options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/anhpham/methods/IFD_Assertion_methods.rb', line 49

def self.do_assertion_json(expected_obj, actual_obj, options = {})
  # puts "\n\n actual_obj.class: #{actual_obj.class}"
  # puts "\n\n expected_obj.class: #{expected_obj.class}"

  if expected_obj.kind_of? Hash
    # if options['isIncludedAssertion'].nil? or options['isIncludedAssertion'] == false
    #   do_assertion(expected_obj.keys.size, actual_obj.keys.size)
    # end
    expected_obj.keys.each do |key|
      # puts "\n\n key: #{key}"
      # puts "\n\n value: #{expected_obj[key]}"
      # puts "\n\n value: #{actual_obj[key]}"
      if actual_obj[key].nil?
        raise "[%s] expected [%s] but actual value was nil." % [key, expected_obj[key].to_s]
      else
        IFD_Assertion.do_assertion_json(expected_obj[key], actual_obj[key], options)
      end
    end
  elsif expected_obj.kind_of? Array
    if options['isIncludedAssertion'].nil? or options['isIncludedAssertion'] == false
      IFD_Assertion.do_assertion_json(expected_obj.size, actual_obj.size)
    end
    for i in (0..expected_obj.length-1)
      IFD_Assertion.do_assertion_json(expected_obj[i], actual_obj[i], options)
    end
  else
    begin
      IFD_Assertion.assert_string_equal(expected_obj.to_s, actual_obj.to_s)
    rescue => e
      raise("Assert fail. \n\n Expected: '#{expected_obj}' \n\n Got: '#{actual_obj}'. Detail info: #{e.message}")
    end
  end
end

.does_images_similar?(actual_img_access_type, actual_img_access_name, excp_img_access_type, excp_img_access_name) ⇒ Boolean

Method to find difference between images

Returns:

  • (Boolean)


97
98
99
100
101
# File 'lib/anhpham/methods/IFD_Assertion_methods.rb', line 97

def self.does_images_similar?(actual_img_access_type, actual_img_access_name, excp_img_access_type, excp_img_access_name)
  if !compare_image(actual_img_access_type, actual_img_access_name, excp_img_access_type, excp_img_access_name)
    raise TestCaseFailed, 'Actual image is different from expected image'
  end
end

.reg_compare(sActual, regValue, isSpecialChar = false) ⇒ Object

Assert 2 values



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/anhpham/methods/IFD_Assertion_methods.rb', line 20

def self.reg_compare sActual, regValue, isSpecialChar=false
  begin
    if !isSpecialChar
      sActual = sActual.strip
      regValue = regValue.strip.gsub("[", "\\[").gsub("]", "\\]").gsub("(", "\\(").gsub(")", "\\)").gsub(">", "\\>")
    end
  rescue StandardError => myStandardError
    put_log "\n>>> Error: #{myStandardError}"
  end

  # put_log "\nsActual:#{sActual}, regValue:#{regValue}"
  if ((sActual.nil? and regValue.nil?) or (!sActual.nil? and sActual.empty? and !regValue.nil? and regValue.empty?))
    return true
  end

  if ((sActual.nil? and !regValue.nil?) or (!sActual.nil? and regValue.nil?))
    return false
  end

  if (!sActual.nil? and !sActual.empty?)
    sCookActual = sActual.gsub(/\n|\r/, " ")
    if (sCookActual == regValue or (isSpecialChar and sCookActual.include? regValue) or (!isSpecialChar and sCookActual =~ /^.*#{regValue}.*$/))
      return true
    end
  end

  return false
end