-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcapybara_screenshot_diff.rb
More file actions
108 lines (91 loc) · 3.43 KB
/
Copy pathcapybara_screenshot_diff.rb
File metadata and controls
108 lines (91 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# frozen_string_literal: true
require "capybara/dsl"
require "capybara/screenshot/diff/version"
require "capybara/screenshot/diff/utils"
require "capybara/screenshot/diff/image_compare"
require "capybara_screenshot_diff/snap_manager"
require "capybara/screenshot/diff/screenshoter"
require "capybara/screenshot/diff/reporters/default"
require "capybara_screenshot_diff/error_with_filtered_backtrace"
module CapybaraScreenshotDiff
RED_RGBA = [255, 0, 0, 255].freeze
ORANGE_RGBA = [255, 192, 0, 255].freeze
class CapybaraScreenshotDiffError < ErrorWithFilteredBacktrace; end
class ExpectationNotMet < CapybaraScreenshotDiffError; end
class UnstableImage < CapybaraScreenshotDiffError; end
class WindowSizeMismatchError < ErrorWithFilteredBacktrace; end
end
module Capybara
module Screenshot
mattr_accessor :add_driver_path
mattr_accessor :add_os_path
mattr_accessor(:blur_active_element) { true }
mattr_accessor :enabled
mattr_accessor(:hide_caret) { true }
mattr_reader(:root) { (defined?(Rails) && defined?(Rails.root) && Rails.root) || Pathname(".").expand_path }
mattr_accessor :stability_time_limit
mattr_accessor :window_size
mattr_accessor(:save_path) { "doc/screenshots" }
mattr_accessor(:use_lfs)
mattr_accessor(:screenshot_format) { "png" }
mattr_accessor(:capybara_screenshot_options) { {} }
class << self
def root=(path)
@@root = Pathname(path).expand_path
end
def active?
enabled || (enabled.nil? && Diff.enabled)
end
def screenshot_area
parts = [Screenshot.save_path]
parts << Os.name if Screenshot.add_os_path
parts << Capybara.current_driver.to_s if Screenshot.add_driver_path
File.join(*parts)
end
def screenshot_area_abs
root / screenshot_area
end
end
# Module to track screenshot changes
module Diff
mattr_accessor(:delayed) { true }
mattr_accessor :area_size_limit
mattr_accessor(:fail_if_new) { ENV["CI"].present? }
mattr_accessor(:fail_on_difference) { true }
mattr_accessor :color_distance_limit
mattr_accessor(:enabled) { true }
mattr_accessor :shift_distance_limit
mattr_accessor :skip_area
mattr_accessor(:driver) { :auto }
mattr_accessor :tolerance
mattr_accessor(:screenshoter) { Screenshoter }
mattr_accessor(:manager) { CapybaraScreenshotDiff::SnapManager }
AVAILABLE_DRIVERS = Utils.detect_available_drivers.freeze
# Configure screenshot and diff settings in one block.
#
# Capybara::Screenshot::Diff.configure do |screenshot, diff|
# screenshot.window_size = [1280, 1024]
# screenshot.stability_time_limit = 1
# diff.driver = :vips
# diff.tolerance = 0.0005
# end
def self.configure
yield Screenshot, self
end
def self.default_options
{
area_size_limit: area_size_limit,
color_distance_limit: color_distance_limit,
driver: driver,
screenshot_format: Screenshot.screenshot_format,
capybara_screenshot_options: Screenshot.capybara_screenshot_options,
shift_distance_limit: shift_distance_limit,
skip_area: skip_area,
stability_time_limit: Screenshot.stability_time_limit,
tolerance: tolerance || ((driver == :vips) ? 0.001 : nil),
wait: Capybara.default_max_wait_time
}
end
end
end
end