-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsystem_test_case.rb
More file actions
95 lines (72 loc) · 3.35 KB
/
system_test_case.rb
File metadata and controls
95 lines (72 loc) · 3.35 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
# frozen_string_literal: true
require "test_helper"
require "capybara_screenshot_diff/minitest"
require "support/setup_capybara_drivers"
class SystemTestCase < ActiveSupport::TestCase
self.test_order = :random
# TODO: Handle race conditions in system tests by introducing git steps
parallelize(workers: 0) # Disable parallelization for system tests to prevent race conditions
setup do
Capybara.current_driver = Capybara.javascript_driver
Capybara.page.current_window.resize_to(*SCREEN_SIZE)
Capybara::Screenshot.enabled = true
Capybara::Screenshot::Diff.enabled = true
# TODO: Reset original settings to previous values
@orig_root = Capybara::Screenshot.root
Capybara::Screenshot.root = Rails.root / "../test/fixtures/app"
@orig_save_path = Capybara::Screenshot.save_path
Capybara::Screenshot.save_path = "./doc/screenshots"
Capybara::Screenshot::Diff.driver = ENV.fetch("SCREENSHOT_DRIVER", "chunky_png").to_sym
# TODO: Makes configurations copying and restoring much easier
@orig_add_os_path = Capybara::Screenshot.add_os_path
Capybara::Screenshot.add_os_path = true
@orig_add_driver_path = Capybara::Screenshot.add_driver_path
Capybara::Screenshot.add_driver_path = true
# NOTE: Only works before `include Capybara::Screenshot::Diff` line
@orig_window_size = Capybara::Screenshot.window_size
Capybara::Screenshot.window_size = SCREEN_SIZE
# NOTE: For small screenshots we should have pixel perfect comparisons
@orig_tolerance = Capybara::Screenshot::Diff.tolerance
Capybara::Screenshot::Diff.tolerance = nil
end
include Capybara::Screenshot::Diff
include CapybaraScreenshotDiff::Minitest::Assertions
teardown do
# Restore to previous values
Capybara::Screenshot.root = @orig_root
Capybara::Screenshot.save_path = @orig_save_path
Capybara::Screenshot.add_os_path = @orig_add_os_path
Capybara::Screenshot.add_driver_path = @orig_add_driver_path
Capybara::Screenshot.window_size = @orig_window_size
Capybara::Screenshot::Diff.tolerance = @orig_tolerance
Capybara.current_driver = Capybara.default_driver
if Capybara::Screenshot::Diff.driver == :vips
Vips.cache_set_max(0)
Vips.cache_set_max(1000)
end
end
private
def rollback_comparison_runtime_files(screenshot_assert)
comparison = screenshot_assert.is_a?(CapybaraScreenshotDiff::ScreenshotAssertion) ? screenshot_assert.compare : screenshot_assert[2]
return unless comparison
save_annotations_for_debug(comparison)
screenshot_path = comparison.image_path
Vcs.restore_git_revision(screenshot_path, root: Capybara::Screenshot.root)
if comparison.difference
comparison.reporter.clean_tmp_files
end
end
def save_annotations_for_debug(comparison)
debug_diffs_save_path = Pathname.new(Capybara.save_path) / "screenshots-diffs" / name
debug_diffs_save_path.mkpath unless debug_diffs_save_path.exist?
if File.exist?(comparison.image_path)
FileUtils.cp(comparison.image_path, debug_diffs_save_path)
end
if comparison.reporter.annotated_base_image_path.exist?
FileUtils.mv(comparison.reporter.annotated_base_image_path, debug_diffs_save_path, force: true)
end
if comparison.reporter.annotated_image_path.exist?
FileUtils.mv(comparison.reporter.annotated_image_path, debug_diffs_save_path, force: true)
end
end
end