Class: Juxtapose::Screenshotter

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

Constant Summary collapse

M_PI =
Math::PI
M_PI_2 =
Math::PI / 2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, template, fuzz_factor = 0) ⇒ Screenshotter

Returns a new instance of Screenshotter.



39
40
41
42
43
44
# File 'lib/juxtapose/screenshotter.rb', line 39

def initialize(context, template, fuzz_factor = 0)
  @context = context
  @template = template.gsub(' ', '-')
  @strategy = strategy_for_context(context)
  @fuzz_factor = fuzz_factor
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



34
35
36
# File 'lib/juxtapose/screenshotter.rb', line 34

def context
  @context
end

#fuzz_factorObject (readonly)

Returns the value of attribute fuzz_factor.



37
38
39
# File 'lib/juxtapose/screenshotter.rb', line 37

def fuzz_factor
  @fuzz_factor
end

#strategyObject (readonly)

Returns the value of attribute strategy.



35
36
37
# File 'lib/juxtapose/screenshotter.rb', line 35

def strategy
  @strategy
end

#templateObject (readonly)

Returns the value of attribute template.



36
37
38
# File 'lib/juxtapose/screenshotter.rb', line 36

def template
  @template
end

Instance Method Details

#accept_currentObject



88
89
90
# File 'lib/juxtapose/screenshotter.rb', line 88

def accept_current
  `cp #{filename(:current)} #{filename(:accepted)}`
end

#attempt_verify(max_attempts) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/juxtapose/screenshotter.rb', line 92

def attempt_verify(max_attempts)
  ensure_imagemagick_installed

  attempts = 0
  while attempts < max_attempts
    return true if verify
    attempts += 1
    sleep 0.25
  end
  raise "Screenshot did not match '#{template}'"
end

#dirObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/juxtapose/screenshotter.rb', line 68

def dir
  @dir ||= File.join(project_root,
                     strategy.spec_dir,
                     strategy.device_name,
                     strategy.version,
                     test_name,
                     template).tap do |dir|
    `mkdir -p #{dir}`
  end
end

#ensure_imagemagick_installedObject



121
122
123
124
125
# File 'lib/juxtapose/screenshotter.rb', line 121

def ensure_imagemagick_installed
  unless imagemagick_installed?
    raise "Executable for 'convert' not installed or not found on $PATH. Please install Imagemagick or add it to your $PATH."
  end
end

#filename(base) ⇒ Object



79
80
81
82
# File 'lib/juxtapose/screenshotter.rb', line 79

def filename(base)
  raise "unknown filename" unless [:current, :accepted, :diff].include?(base)
  File.join dir, [base, "png"].join('.')
end

#imagemagick_installed?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/juxtapose/screenshotter.rb', line 127

def imagemagick_installed?
  `command -v convert`.length > 0
end

#project_rootObject



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

def project_root
  if defined? Rails
    Rails.root
  else
    ENV["RUBYMOTION_PROJECT_DIR"]
  end
end

#strategy_for_context(context) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/juxtapose/screenshotter.rb', line 46

def strategy_for_context(context)
  if defined?(::Bacon::Context) && context.is_a?(::Bacon::Context)
    Juxtapose::MacBaconStrategy.new(context)
  elsif context.respond_to? :frankly_ping
    Juxtapose::FrankStrategy.new(context)
  elsif defined?(Capybara)
    Juxtapose::CapybaraStrategy.new(context)
  end
end

#test_nameObject



64
65
66
# File 'lib/juxtapose/screenshotter.rb', line 64

def test_name
  strategy.current_spec_description.downcase.gsub(/ /, '-').gsub(/[^\w-]/, '')
end

#timestampObject



84
85
86
# File 'lib/juxtapose/screenshotter.rb', line 84

def timestamp
  @timestamp ||= Time.now.to_f.to_s.gsub(/\./, '')
end

#verifyObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/juxtapose/screenshotter.rb', line 104

def verify
  ensure_imagemagick_installed

  strategy.save_current filename(:current)
  accept_current if ENV['ACCEPT_ALL_SCREENSHOTS']

  success = true
  if File.exists? filename(:accepted )
    raise "Screenshots are different sizes" unless same_size?
    success = screenshots_match?
  else
    raise "No accepted screen shot for #{filename :accepted}"
  end

  success
end