Module: Capybara::Screenshot::Diff::TestMethods

Includes:
Stabilization, Vcs
Defined in:
lib/capybara/screenshot/diff/test_methods.rb

Constant Summary

Constants included from Vcs

Vcs::SILENCE_ERRORS

Constants included from Stabilization

Stabilization::IMAGE_WAIT_SCRIPT

Constants included from Os

Os::ON_LINUX, Os::ON_MAC, Os::ON_WINDOWS

Instance Method Summary collapse

Methods included from Vcs

#checkout_vcs, #restore_git_revision

Methods included from Stabilization

#take_stable_screenshot

Methods included from Os

#os_name

Instance Method Details

#assert_image_not_changed(caller, name, comparison) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 111

def assert_image_not_changed(caller, name, comparison)
  return unless comparison.different?

  # TODO(uwe): Remove check when we stop supporting Ruby 2.3 and older
  max_color_distance = if RUBY_VERSION >= '2.4'
                         comparison.max_color_distance.ceil(1)
                       else
                         comparison.max_color_distance.ceil
                       end
  # ODOT

  max_shift_distance = comparison.max_shift_distance
  "Screenshot does not match for '#{name}' (area: #{comparison.size}px #{comparison.dimensions}" \
    ", max_color_distance: #{max_color_distance}" \
    "#{", max_shift_distance: #{max_shift_distance}" if max_shift_distance})\n" \
    "#{comparison.new_file_name}\n#{comparison.annotated_old_file_name}\n" \
    "#{comparison.annotated_new_file_name}\n" \
    "at #{caller}"
end

#current_capybara_driver_classObject



44
45
46
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 44

def current_capybara_driver_class
  Capybara.drivers[Capybara.current_driver].call({}).class
end

#full_name(name) ⇒ Object



36
37
38
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 36

def full_name(name)
  File.join group_parts.<<(name).map(&:to_s)
end

#group_partsObject



29
30
31
32
33
34
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 29

def group_parts
  parts = []
  parts << @screenshot_section if @screenshot_section.present?
  parts << @screenshot_group if @screenshot_group.present?
  parts
end

#initializeObject



20
21
22
23
24
25
26
27
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 20

def initialize(*)
  super
  @screenshot_counter = nil
  @screenshot_group = nil
  @screenshot_section = nil
  @test_screenshot_errors = nil
  @test_screenshots = nil
end

#poltergeist?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 52

def poltergeist?
  return false unless defined?(Capybara::Poltergeist::Driver)

  current_capybara_driver_class <= Capybara::Poltergeist::Driver
end

#screenshot(name, color_distance_limit: Diff.color_distance_limit, shift_distance_limit: Diff.shift_distance_limit, area_size_limit: Diff.area_size_limit, skip_area: nil) ⇒ Boolean

Returns wether a screenshot was taken.

Returns:

  • (Boolean)

    wether a screenshot was taken



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
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 71

def screenshot(name, color_distance_limit: Diff.color_distance_limit,
    shift_distance_limit: Diff.shift_distance_limit, area_size_limit: Diff.area_size_limit,
    skip_area: nil)
  return unless Screenshot.active?
  return if window_size_is_wrong?

  if @screenshot_counter
    name = "#{format('%02i', @screenshot_counter)}_#{name}"
    @screenshot_counter += 1
  end
  name = full_name(name)
  file_name = "#{Screenshot.screenshot_area_abs}/#{name}.png"

  FileUtils.mkdir_p File.dirname(file_name)
  comparison = ImageCompare.new(file_name,
      dimensions: Screenshot.window_size, color_distance_limit: color_distance_limit,
      area_size_limit: area_size_limit, shift_distance_limit: shift_distance_limit,
      skip_area: skip_area)
  checkout_vcs(name, comparison)
  take_stable_screenshot(comparison, color_distance_limit: color_distance_limit,
                                     shift_distance_limit: shift_distance_limit,
                                     area_size_limit: area_size_limit,
                                     skip_area: skip_area)
  return unless comparison.old_file_exists?

  (@test_screenshots ||= []) << [caller(1..1).first, name, comparison]
  true
end

#screenshot_dirObject



40
41
42
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 40

def screenshot_dir
  File.join [Screenshot.screenshot_area] + group_parts
end

#screenshot_group(name) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 62

def screenshot_group(name)
  @screenshot_group = name.to_s
  @screenshot_counter = 0
  return unless Screenshot.active? && name.present?

  FileUtils.rm_rf screenshot_dir
end

#screenshot_section(name) ⇒ Object



58
59
60
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 58

def screenshot_section(name)
  @screenshot_section = name.to_s
end

#selenium?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 48

def selenium?
  current_capybara_driver_class <= Capybara::Selenium::Driver
end

#window_size_is_wrong?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
109
# File 'lib/capybara/screenshot/diff/test_methods.rb', line 100

def window_size_is_wrong?
  selenium? && Screenshot.window_size &&

    # FIXME(uwe): This happens with headless chrome.  Why?!
    page.driver.browser.manage.window.size.width &&
    # EMXIF

    page.driver.browser.manage.window.size !=
      ::Selenium::WebDriver::Dimension.new(*Screenshot.window_size)
end