Module: Capybara::Screenshot::Diff::TestMethods

Defined in:
lib/capybara/screenshot/diff/test_methods.rb

Instance Method Summary collapse

Instance Method Details

#assert_image_not_changed(caller, name, comparison) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 91

def assert_image_not_changed(caller, name, comparison)
  result = comparison.different?

  # Cleanup after comparisons
  if !result && comparison.base_image_path.exist?
    FileUtils.mv(comparison.base_image_path, comparison.image_path, force: true)
  else
    FileUtils.rm_rf(comparison.base_image_path)
  end

  return unless result

  "Screenshot does not match for '#{name}' #{comparison.error_message}\n#{caller}"
end

#build_full_name(name) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 32

def build_full_name(name)
  if @screenshot_counter
    name = format("%02i_#{name}", @screenshot_counter)
    @screenshot_counter += 1
  end

  File.join(*group_parts.push(name.to_s))
end

#group_partsObject



62
63
64
65
66
67
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 62

def group_parts
  parts = []
  parts << @screenshot_section if @screenshot_section.present?
  parts << @screenshot_group if @screenshot_group.present?
  parts
end

#initializeObject



23
24
25
26
27
28
29
30
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 23

def initialize(*)
  super
  @screenshot_counter = nil
  @screenshot_group = nil
  @screenshot_section = nil
  @test_screenshot_errors = nil
  @test_screenshots = []
end

#schedule_match_job(job) ⇒ Object



57
58
59
60
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 57

def schedule_match_job(job)
  (@test_screenshots ||= []) << job
  true
end

#screenshot(name, skip_stack_frames: 0, **options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 69

def screenshot(name, skip_stack_frames: 0, **options)
  return false unless Screenshot.active?

  screenshot_full_name = build_full_name(name)
  job = build_screenshot_matches_job(screenshot_full_name, options)

  return false unless job

  job.prepend(caller[skip_stack_frames])

  if Screenshot::Diff.delayed
    schedule_match_job(job)
  else
    error_msg = assert_image_not_changed(*job)
    if error_msg
      error = ASSERTION.new(error_msg)
      error.set_backtrace(caller(2))
      raise error
    end
  end
end

#screenshot_dirObject



41
42
43
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 41

def screenshot_dir
  File.join(*([Screenshot.screenshot_area] + group_parts))
end

#screenshot_group(name) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 49

def screenshot_group(name)
  @screenshot_group = name.to_s
  @screenshot_counter = @screenshot_group.present? ? 0 : nil
  return unless Screenshot.active? && name.present?

  FileUtils.rm_rf screenshot_dir
end

#screenshot_section(name) ⇒ Object



45
46
47
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 45

def screenshot_section(name)
  @screenshot_section = name.to_s
end