class CapybaraScreenshotDiff::ScreenshotAssertion
Attributes
Public Class Methods
Source
# 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
Asserts that an image has not changed compared to its baseline.
@param backtrace [Array(String)] The caller context, used for error reporting. @param name [String] The name of the screenshot being verified. @param comparison [Object] The comparison object containing the result and details of the comparison. @return [String, nil] Returns an error message if the screenshot differs from the baseline, otherwise nil. @note This method is used internally to verify individual screenshots.
Source
# 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
Source
# 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
Source
# 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
Verifies that all scheduled screenshots do not show any unintended differences.
@param screenshots [Array(Array(Array(String), String, ImageCompare))] The list of match screenshots jobs. Defaults to all screenshots taken during the test. @return [Array, nil] Returns an array of error messages if there are screenshot differences, otherwise nil. @note This method is typically called at the end of a test to assert all screenshots are as expected.
Public Instance Methods
Source
# 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