lib/element.rb in gridium-0.1.13 vs lib/element.rb in gridium-0.1.14

- old
+ new

@@ -7,10 +7,11 @@ def initialize(name, by, locator) @name = name @by = by @locator = locator + @element_screenshot = nil #used to store the path of element screenshots for comparison # wrapped driver @driver = Driver.driver # selenium web element @@ -237,10 +238,49 @@ image = ChunkyPNG::Image.from_file(screenshot_path.to_s) image1 = image.crop(location_x, location_y, element_width, element_height) image2 = image1.to_image element_screenshot_path = File.join($current_run_dir, "#{name}__#{timestamp}.png") image2.save(element_screenshot_path) + @element_screenshot = element_screenshot_path SpecData.screenshots_captured.push("#{name}__#{timestamp}.png") + end + + def compare_element_screenshot(base_image_path) + #Returns TRUE if there are no differences, FALSE if there are + begin + Log.debug("Doing Image Comparison...") + images = [ + ChunkyPNG::Image.from_file(base_image_path), + ChunkyPNG::Image.from_file(@element_screenshot) + ] + #used to store image x,y diff + diff = [] + Log.debug("Loaded Images into array...") + 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 + + Log.debug("Pixels total: #{images.first.pixels.length}") + Log.debug("Pixels changed: #{diff.length}") + Log.debug("Pixels changed: #{(diff.length.to_f / images.first.pixels.length) * 100}%") + + x, y = diff.map{|xy| xy[0]}, diff.map{|xy| xy[1]} + + if x.any? && y.any? + name = self.name.gsub(' ', '_') + #timestamp = Time.now.strftime("%Y_%m_%d__%H_%M_%S") + element_screenshot_path = File.join($current_run_dir, "#{name}__diff_.png") + images.last.rect(x.min, y.min, x.max, y.max, ChunkyPNG::Color(0,255,0)) + images.last.save(element_screenshot_path) + return false + else + return true + end + rescue Exception => e + Log.error("There was a problem comparing element images. #{e.to_s}") + end end def method_missing(method_sym, *arguments, &block) Log.debug("called #{method_sym} on element #{@locator} by #{@by_type}") if @element.respond_to?(method_sym) \ No newline at end of file