Sha256: 6aa606792c20179f05ac6c56135976e0ff5fc2b676e0c03473a32ce42f1a16e7

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

require "gatling/version"
require 'RMagick'
include Magick

# The Gatling Comparison class allows you to build an Rspec / Capybara custom matcher comparing a :css or :xpath element to a referenced screenshot:
# Example:
# -----------
# RSpec::Matchers.define :look_like do |expected|
#   match do |actual|
#     Gatling::Comparison.new(expected, actual).matches?
#   end
# end
# -----------

module Gatling
    class Comparison

      def initialize expected, actual
        @expected = expected
        @actual = actual
      end

      def capture
        page.driver.browser.save_screenshot('temp.png')
        temp_screenshot = Image.read('temp.png').first
      end

      def crop_element
        element = @actual.native
        location = element.location
        size = element.size
        capture.crop(location.x, location.y, size.width, size.height)
      end

      def save_crop_as_reference(cropped_element)   
        filename = "#{@expected}".sub(/\.[a-z]*/,'')
        cropped_element.write("#{filename}_candidate.png")
        return filename
      end   


      def matches?
        cropped_element = crop_element
        if File.exists?(@expected)
          expected_img = Image.read(@expected).first
          diff_metric = cropped_element.compare_channel(expected_img, MeanAbsoluteErrorMetric)
          matches = diff_metric[1] == 0.0
          diff_metric.first.write('diff.png') unless matches
          matches
        else
          filename = save_crop_as_reference(cropped_element)
          raise "The design reference #{@expected} does not exist, #{filename}_candidate.png is now available to be used as a reference"     
        end    
      end      
    end
  end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gatling-0.0.2 lib/gatling.rb
gatling-0.0.1 lib/gatling.rb