Class: Appom::Screenshot::ScreenshotComparison
- Inherits:
-
Object
- Object
- Appom::Screenshot::ScreenshotComparison
- Includes:
- Logging
- Defined in:
- lib/appom/screenshot.rb
Overview
Screenshot comparison utilities
Instance Method Summary collapse
-
#compare(image1_path, image2_path, output_path: nil) ⇒ Object
Compare two screenshots and return similarity percentage.
-
#initialize(tolerance: 0.1, highlight_differences: true) ⇒ ScreenshotComparison
constructor
A new instance of ScreenshotComparison.
-
#similar?(image1_path, image2_path, tolerance: @tolerance) ⇒ Boolean
Check if images are similar within tolerance.
Methods included from Logging
level, level=, #log_debug, #log_element_action, #log_error, #log_info, #log_wait_end, #log_wait_start, #log_warn, #logger
Constructor Details
#initialize(tolerance: 0.1, highlight_differences: true) ⇒ ScreenshotComparison
Returns a new instance of ScreenshotComparison.
249 250 251 252 |
# File 'lib/appom/screenshot.rb', line 249 def initialize(tolerance: 0.1, highlight_differences: true) @tolerance = tolerance @highlight_differences = highlight_differences end |
Instance Method Details
#compare(image1_path, image2_path, output_path: nil) ⇒ Object
Compare two screenshots and return similarity percentage
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/appom/screenshot.rb', line 255 def compare(image1_path, image2_path, output_path: nil) begin require 'mini_magick' rescue LoadError log_error('MiniMagick gem required for image comparison') return nil end begin img1 = MiniMagick::Image.open(image1_path) img2 = MiniMagick::Image.open(image2_path) # Resize images to same dimensions if needed if img1.dimensions != img2.dimensions log_warn('Images have different dimensions, resizing for comparison') img2.resize("#{img1.width}x#{img1.height}") end # Compare images diff = img1.compare(img2, 'mae') # Mean Absolute Error similarity = (1.0 - diff) * 100 # Generate difference image if requested generate_diff_image(img1, img2, output_path) if output_path && @highlight_differences log_info("Image comparison: #{similarity.round(2)}% similar") similarity rescue StandardError => e log_error('Image comparison failed', { error: e. }) nil end end |
#similar?(image1_path, image2_path, tolerance: @tolerance) ⇒ Boolean
Check if images are similar within tolerance
289 290 291 292 293 294 295 |
# File 'lib/appom/screenshot.rb', line 289 def similar?(image1_path, image2_path, tolerance: @tolerance) similarity = compare(image1_path, image2_path) return false unless similarity difference = 100 - similarity difference <= tolerance end |