Class: Goggles::Comparison

Inherits:
Object
  • Object
show all
Defined in:
lib/goggles/comparison.rb

Overview

Generates a diff image and data file from two images.

Results are saved to a new directory derived from the configured directory setting,

a description given to `Watir::Browser.grab_screenshot`, browser size, and the
browsers used to take the screenshots.

Examples:

Results

Goggles.configure do |c|
  c.directory = "/path/results"
  c.browsers = [:chrome, :firefox]
  c.sizes = [100]
end

Goggles.each do |b|
  b.goto "www.google.com"
  b.grab_screenshot "search"
end

#=> /path/results
#   |- /search_100
#    |- chrome_firefox_data.txt
#    |- chrome_firefox_diff.png

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Comparison

Returns a new instance of Comparison.



32
33
34
35
36
37
# File 'lib/goggles/comparison.rb', line 32

def initialize config
  @directory = config.directory
  @fuzzing   = config.fuzzing
  @color     = config.color
  @groups    = config.groups
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



30
31
32
# File 'lib/goggles/comparison.rb', line 30

def color
  @color
end

#directoryObject (readonly)

Returns the value of attribute directory.



30
31
32
# File 'lib/goggles/comparison.rb', line 30

def directory
  @directory
end

#fuzzingObject (readonly)

Returns the value of attribute fuzzing.



30
31
32
# File 'lib/goggles/comparison.rb', line 30

def fuzzing
  @fuzzing
end

#groupsObject (readonly)

Returns the value of attribute groups.



30
31
32
# File 'lib/goggles/comparison.rb', line 30

def groups
  @groups
end

#results_dirObject

Returns the value of attribute results_dir.



30
31
32
# File 'lib/goggles/comparison.rb', line 30

def results_dir
  @results_dir
end

Instance Method Details

#cut_to_common_sizenil

Sizes comparable screenshots to a common height and width.

Returns:

  • (nil)


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/goggles/comparison.rb', line 54

def cut_to_common_size
  groups.each_with_object([]) do |group, sizes|
    collection = find_comparable group
    
    collection.each do |img|
      File.open(img, 'rb'){ |file| sizes << read_size(file) }
    end

    cut! collection, sizes
  end
end

#find_common_height(array) ⇒ Fixnum

Finds the smallest second element for all arrays in the given array. This is

the screenshot height attribute. Used for resizing.

Parameters:

  • array (Array<Array>)

    collection of screenshot sizes

Returns:

  • (Fixnum)

    smallest screenshot height



106
107
108
# File 'lib/goggles/comparison.rb', line 106

def find_common_height array
  array.collect(&:last).sort.first
end

#find_common_width(array) ⇒ Fixnum

Finds the smallest first element for all arrays in the given array. This is

the screenshot width attribute. Used for resizing.

Parameters:

  • array (Array<Array>)

    collection of screenshot sizes

Returns:

  • (Fixnum)

    smallest width



95
96
97
# File 'lib/goggles/comparison.rb', line 95

def find_common_width array
  array.collect(&:first).sort.first
end

#find_comparable(description) ⇒ Array<String>

Finds comparable screenshots for sizing and comparing.

Parameters:

  • description (String)

    screenshot description

Returns:

  • (Array<String>)

    paths to comparable screenshots



84
85
86
# File 'lib/goggles/comparison.rb', line 84

def find_comparable description
  Dir.glob("#{directory}/*.png").grep(/#{description}_/).sort
end

#highlight_differencesnil

Generates diff images for comparable screenshots.

Returns:

  • (nil)


71
72
73
74
75
76
# File 'lib/goggles/comparison.rb', line 71

def highlight_differences
  groups.each do |desc|
    ensure_result_directory desc
    find_comparable(desc).combination(2).to_a.each { |imgs| diff imgs[0], imgs[1]  }
  end
end

#make!nil

Sizes and compare screenshots.

Returns:

  • (nil)


44
45
46
47
# File 'lib/goggles/comparison.rb', line 44

def make!
  cut_to_common_size
  highlight_differences
end