Module: LooksGood

Defined in:
lib/looks_good.rb,
lib/looks_good/image.rb,
lib/looks_good/version.rb,
lib/looks_good/comparison.rb,
lib/looks_good/configuration.rb,
lib/looks_good/capture_element.rb

Defined Under Namespace

Modules: CaptureElement, Configuration Classes: Comparison, Image, ImageFromElement, ImageFromFile

Constant Summary collapse

VERSION =
"1.1.5"

Class Method Summary collapse

Class Method Details

.check(expected_reference_filename, actual_element, within: LooksGood::Configuration.default_within) ⇒ Object



14
15
16
17
# File 'lib/looks_good.rb', line 14

def check(expected_reference_filename, actual_element, within: LooksGood::Configuration.default_within)
   result = match_result(expected_reference_filename, actual_element, within: within)
   result
end

.cleanupObject



82
83
84
85
86
# File 'lib/looks_good.rb', line 82

def cleanup
  FileUtils.remove_dir(LooksGood::Configuration.path(:tmp)) if File.directory?(LooksGood::Configuration.path(:tmp))
  FileUtils.remove_dir(LooksGood::Configuration.path(:diff)) if File.directory?(LooksGood::Configuration.path(:diff))
  FileUtils.remove_dir(LooksGood::Configuration.path(:candidate)) if File.directory?(LooksGood::Configuration.path(:candidate))
end

.compare_until_match(actual_element, reference_file, within:, max_no_tries: LooksGood::Configuration.max_no_tries, sleep_time: LooksGood::Configuration.sleep_between_tries) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/looks_good.rb', line 54

def compare_until_match(actual_element, reference_file, within:, max_no_tries: LooksGood::Configuration.max_no_tries, sleep_time: LooksGood::Configuration.sleep_between_tries)
  max_no_tries.times do |i|
    actual_image = LooksGood::ImageFromElement.new(actual_element, reference_file.file_name)
    @comparison = LooksGood::Comparison.new(actual_image, reference_file, within)
    match = @comparison.matches?
    if !match
      sleep sleep_time
      #TODO: Send to logger instead of puts
      i += 1
      puts "Tried to match #{i} times"
    else
      return(@comparison) 
    end
  end
  @comparison
end

.config(&block) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/looks_good.rb', line 89

def config(&block)
  begin
    config_class = LooksGood::Configuration
    raise "No block provied" unless block_given?
    block.call(config_class)
  rescue
     raise "Config block has changed. Example: LooksGood.config {|c| c.reference_image_path = 'some/path'}. Please see README"
  end
end

.match_result(expected_reference_filename, actual_element, within:) ⇒ Object



19
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
48
49
50
51
52
# File 'lib/looks_good.rb', line 19

def match_result(expected_reference_filename, actual_element, within:)
  result_hash = {}
  @actual_element = actual_element
  @expected_reference_filename = expected_reference_filename
  @expected_reference_file = (File.join(LooksGood::Configuration.path(:reference), expected_reference_filename))


  if !File.exists?(@expected_reference_file) || ENV["LOOKS_GOOD"]
    save_reference
    result_hash[:result] = true
    return result_hash
  else
    reference_file = LooksGood::ImageFromFile.new(expected_reference_filename)
    comparison = compare_until_match(actual_element, reference_file, within: within)
    result_hash[:comparison] = comparison
    result_hash[:percent_difference] = comparison.percent_difference
    matches = comparison.matches?
    if !matches
      comparison.actual_image.save(:candidate)
      result_hash[:message] = %Q[view a visual diff image: open #{comparison.diff_image.path(:diff)}\n
HOW TO FIX:\n 
- cp #{comparison.diff_image.path(:candidate)} #{@expected_reference_file}
or
- LOOKS_GOOD=true rspec ...
--
#{LooksGood::LooksGood::Configuration.custom_failure_message}]
      result_hash[:result] = false
      result_hash
    else
      result_hash[:result] = true
      result_hash
    end
  end
end

.save_image_as_candidate(image) ⇒ Object



71
72
73
74
75
# File 'lib/looks_good.rb', line 71

def save_image_as_candidate(image)
  image.save(:candidate)
  raise "The design reference #{image.file_name} does not exist, #{image.path(:candidate)} " +
  "is now available to be used as a reference. Copy candidate to root reference_image_path to use as reference"
end

.save_referenceObject



77
78
79
80
# File 'lib/looks_good.rb', line 77

def save_reference
  sleep 0.5 # ensures page/browser stability of focus effects etc inherent in browsers
  ImageFromElement.new(@actual_element,@expected_reference_filename).verify_and_save
end