class IFD_Assertion

Public Class Methods

assert_string_contain(expected, actual) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source

Comparing jpg images

# 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) click to toggle source

Comparing png images

# 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) click to toggle source

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

# 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 = {}) click to toggle source
# 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) click to toggle source

Method to find difference between images

# 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) click to toggle source

Assert 2 values

# 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