Class: Zucchini::Screenshot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, device, unmatched_pending = false) ⇒ Screenshot

Returns a new instance of Screenshot.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/screenshot.rb', line 11

def initialize(file_path, device, unmatched_pending = false)
  @file_path = file_path
  @file_name = File.basename(@file_path)
  @device    = device
  
  unless unmatched_pending
    @file_base_path = File.dirname(@file_path)
    
    @masks_paths = {
      :global   => "#{@file_base_path}/../../../support/masks/#{@device[:screen]}.png",
      :specific => "#{@file_base_path}/../../masks/#{@device[:screen]}/#{@file_name}" 
    }
  
    masked_path   = "#{@file_base_path}/../Masked/actual/#{@file_name}"
    @masked_paths = { :globally => masked_path, :specifically => masked_path }
  
    @test_path = nil
    @pending   = false
    @diff_path = "#{@file_base_path}/../Diff/#{@file_name}"
  end 
end

Instance Attribute Details

#diffObject

Returns the value of attribute diff.



5
6
7
# File 'lib/screenshot.rb', line 5

def diff
  @diff
end

#diff_pathObject

Returns the value of attribute diff_path.



9
10
11
# File 'lib/screenshot.rb', line 9

def diff_path
  @diff_path
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



3
4
5
# File 'lib/screenshot.rb', line 3

def file_name
  @file_name
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



2
3
4
# File 'lib/screenshot.rb', line 2

def file_path
  @file_path
end

#masked_pathsObject

Returns the value of attribute masked_paths.



7
8
9
# File 'lib/screenshot.rb', line 7

def masked_paths
  @masked_paths
end

#masks_pathsObject

Returns the value of attribute masks_paths.



6
7
8
# File 'lib/screenshot.rb', line 6

def masks_paths
  @masks_paths
end

#test_pathObject

Returns the value of attribute test_path.



8
9
10
# File 'lib/screenshot.rb', line 8

def test_path
  @test_path
end

Instance Method Details

#compareObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/screenshot.rb', line 42

def compare
  mask_reference
  
  if @test_path
    FileUtils.mkdir_p(File.dirname(@diff_path))
    
    out = `compare -metric AE -fuzz 2% -subimage-search \"#{@masked_paths[:specifically]}\" \"#{@test_path}\" \"#{@diff_path}\" 2>&1`
    @diff = (out == "0\n") ? [:passed, nil] : [:failed, out]
    @diff = [:pending, @diff[1]] if @pending
  else
    @diff = [:failed, "no reference or pending screenshot for #{@device[:screen]}\n"]
  end
end

#maskObject



33
34
35
36
37
38
39
40
# File 'lib/screenshot.rb', line 33

def mask
  @masked_paths.each { |name, path| FileUtils.mkdir_p(File.dirname(path)) }
  `convert -page +0+0 \"#{@file_path}\" -page +0+0 \"#{@masks_paths[:global]}\" -flatten \"#{@masked_paths[:globally]}\"`
  
  if File.exists?(@masks_paths[:specific])
    `convert -page +0+0 \"#{@masked_paths[:globally]}\" -page +0+0 \"#{@masks_paths[:specific]}\" -flatten \"#{@masked_paths[:specifically]}\"`
  end
end

#mask_referenceObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/screenshot.rb', line 64

def mask_reference
  %W(reference pending).each do |reference_type|
    reference_file_path = "#{@file_base_path}/../../#{reference_type}/#{@device[:screen]}/#{@file_name}"
    output_path         = "#{@file_base_path}/../Masked/#{reference_type}/#{@file_name}"
    
    if File.exists?(reference_file_path)
      @test_path = output_path
      @pending   = (reference_type == "pending") 
      FileUtils.mkdir_p(File.dirname(output_path))
      
      reference = Zucchini::Screenshot.new(reference_file_path, @device)
      reference.masks_paths  = @masks_paths
      reference.masked_paths = { :globally => output_path, :specifically => output_path }
      reference.mask
    end
  end
end

#result_imagesObject



56
57
58
59
60
61
62
# File 'lib/screenshot.rb', line 56

def result_images
  @result_images ||= {
    :actual     => @masked_paths && File.exists?(@masked_paths[:specifically]) ? @masked_paths[:specifically] : nil,
    :expected   => @test_path    && File.exists?(@test_path) ? @test_path : nil,
    :difference => @diff_path    && File.exists?(@diff_path) ? @diff_path : nil
  }
end