Class: CapybaraScreenshotDiff::ScreenshotAssertion
- Inherits:
-
Object
- Object
- CapybaraScreenshotDiff::ScreenshotAssertion
- Defined in:
- lib/capybara_screenshot_diff/screenshot_assertion.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#caller ⇒ Object
Returns the value of attribute caller.
-
#compare ⇒ Object
Returns the value of attribute compare.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.assert_image_not_changed(backtrace, name, comparison) ⇒ String?
Asserts that an image has not changed compared to its baseline.
- .from(screenshot_job) ⇒ Object
-
.verify_screenshots!(screenshots) ⇒ Array?
Verifies that all scheduled screenshots do not show any unintended differences.
Instance Method Summary collapse
-
#initialize(name, **args) {|_self| ... } ⇒ ScreenshotAssertion
constructor
A new instance of ScreenshotAssertion.
- #validate ⇒ Object
Constructor Details
#initialize(name, **args) {|_self| ... } ⇒ ScreenshotAssertion
Returns a new instance of ScreenshotAssertion.
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
#args ⇒ Object (readonly)
Returns the value of attribute args.
7 8 9 |
# File 'lib/capybara_screenshot_diff/screenshot_assertion.rb', line 7 def args @args end |
#caller ⇒ Object
Returns the value of attribute caller.
8 9 10 |
# File 'lib/capybara_screenshot_diff/screenshot_assertion.rb', line 8 def caller @caller end |
#compare ⇒ Object
Returns the value of attribute compare.
8 9 10 |
# File 'lib/capybara_screenshot_diff/screenshot_assertion.rb', line 8 def compare @compare end |
#name ⇒ Object (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?
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.}\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?
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
#validate ⇒ Object
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 |