Class: CapybaraScreenshotDiff::ScreenshotAssertion

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, **args) {|_self| ... } ⇒ ScreenshotAssertion

Returns a new instance of ScreenshotAssertion.

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
15
# File 'lib/capybara_screenshot_diff/screenshot_assertion.rb', line 10

def initialize(name, **args, &block)
  @name = name
  @args = args

  yield(self) if block_given?
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#callerObject

Returns the value of attribute caller.



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

def caller
  @caller
end

#compareObject

Returns the value of attribute compare.



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

def compare
  @compare
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.assert_image_not_changed(backtrace, name, comparison) ⇒ String?

Note:

This method is used internally to verify individual screenshots.

Asserts that an image has not changed compared to its baseline.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/capybara_screenshot_diff/screenshot_assertion.rb', line 59

def self.assert_image_not_changed(backtrace, 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)
  elsif !comparison.dimensions_changed?
    FileUtils.rm_rf(comparison.base_image_path)
  end

  return unless result

  "Screenshot does not match for '#{name}': #{comparison.error_message}\n#{backtrace.join("\n")}"
end

.from(screenshot_job) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/capybara_screenshot_diff/screenshot_assertion.rb', line 17

def self.from(screenshot_job)
  return screenshot_job if screenshot_job.is_a?(ScreenshotAssertion)

  caller, name, compare = screenshot_job
  ScreenshotAssertion.new(name).tap do |it|
    it.caller = caller
    it.compare = compare
  end
end

.verify_screenshots!(screenshots) ⇒ Array?

Note:

This method is typically called at the end of a test to assert all screenshots are as expected.

Verifies that all scheduled screenshots do not show any unintended differences.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/capybara_screenshot_diff/screenshot_assertion.rb', line 38

def self.verify_screenshots!(screenshots)
  return unless ::Capybara::Screenshot.active? && ::Capybara::Screenshot::Diff.fail_on_difference

  test_screenshot_errors = screenshots.map do |assertion|
    assertion.validate
  end

  test_screenshot_errors.compact!

  test_screenshot_errors.empty? ? nil : test_screenshot_errors
ensure
  screenshots&.clear
end

Instance Method Details

#validateObject



27
28
29
30
31
# File 'lib/capybara_screenshot_diff/screenshot_assertion.rb', line 27

def validate
  return unless compare

  self.class.assert_image_not_changed(caller, name, compare)
end