class Juxtapose::Screenshotter
Constants
- M_PI
- M_PI_2
Attributes
context[R]
fuzz_factor[R]
project_root[RW]
strategy[R]
template[R]
Public Class Methods
new(context, template, fuzz_factor = 0, project_root = nil)
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 40 def initialize(context, template, fuzz_factor = 0, project_root = nil) @context = context @template = template.gsub(' ', '-') @strategy = strategy_for_context(context) @fuzz_factor = fuzz_factor @project_root = project_root || default_project_root end
Public Instance Methods
accept_current()
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 98 def accept_current `cp #{filename(:current)} #{filename(:accepted)}` end
attempt_verify(max_attempts)
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 102 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
default_project_root()
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 60 def default_project_root if defined? Rails Rails.root else ENV["RUBYMOTION_PROJECT_DIR"] end end
dir()
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 72 def dir @dir ||= begin parts = [ project_root, strategy.spec_dir, strategy.device_name, strategy.version, test_name, template ].map {|p| p.to_s.gsub(/ /, '-')} File.join(*parts).tap do |dir| `mkdir -p #{dir}` end end end
ensure_imagemagick_installed()
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 131 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)
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 89 def filename(base) raise "unknown filename" unless [:current, :accepted, :diff].include?(base) File.join dir, [base, "png"].join('.') end
imagemagick_installed?()
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 137 def imagemagick_installed? if RUBY_PLATFORM =~ /darwin/ `command -v convert` else `which convert` end $?.success? end
strategy_for_context(context)
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 48 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) elsif defined?(Appium) Juxtapose::AppiumStrategy.new(context) end end
test_name()
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 68 def test_name strategy.current_spec_description.downcase.gsub(/ /, '-').gsub(/[^\w-]/, '') end
timestamp()
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 94 def timestamp @timestamp ||= Time.now.to_f.to_s.gsub(/\./, '') end
verify()
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 114 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
Private Instance Methods
cleanup()
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 159 def cleanup `rm #{filename(:current)}` `rm #{filename(:diff)}` end
create_diff()
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 155 def create_diff `compare -fuzz #{fuzz_factor}% -dissimilarity-threshold 1 -subimage-search \"#{filename :current}\" \"#{filename :accepted}\" \"#{filename :diff}\" 2>&1` end
identical_images?()
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 164 def identical_images? matcher = ImageMatcher.new(fuzz_factor: fuzz_factor, diff_file_name: filename(:diff)) matcher.identical?(filename(:current), filename(:accepted)) end
same_size?()
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 148 def same_size? identify_command = "identify -format '%wx%h '" out = `#{identify_command} \"#{filename :current}\" \"#{filename :accepted}\" 2>&1` sizes = out.split sizes.length == 2 && sizes.uniq.length == 1 end
screenshots_match?()
click to toggle source
# File lib/juxtapose/screenshotter.rb, line 169 def screenshots_match? if identical_images? cleanup true else create_diff false end end