require 'capybara' require 'capybara/screenshot/diff/image_compare' require 'action_controller' require 'action_dispatch' # TODO(uwe): Move this code to module Capybara::Screenshot::Diff::TestMethods, # and use Module#prepend/include to insert. # Add the `screenshot` method to ActionDispatch::IntegrationTest # rubocop:disable Metrics/ClassLength module ActionDispatch class IntegrationTest ON_WINDOWS = RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ SILENCE_ERRORS = ON_WINDOWS ? '2>nul' : '2>/dev/null' def self.os_name case RbConfig::CONFIG['host_os'] when /darwin/ 'macos' when /mswin|mingw|cygwin/ 'windows' when /linux/ 'linux' else 'unknown' end end def self.macos? os_name == 'macos' end def self.screenshot_root Capybara::Screenshot.screenshot_root || (defined?(Rails.root) && Rails.root) || File.expand_path('.') end def self.screenshot_area parts = ['doc/screenshots'] parts << Capybara.default_driver.to_s if Capybara::Screenshot.add_driver_path parts << os_name if Capybara::Screenshot.add_os_path File.join parts end def self.screenshot_area_abs "#{screenshot_root}/#{screenshot_area}".freeze end def initialize(*) super @screenshot_counter = nil @screenshot_group = nil @screenshot_section = nil @test_screenshot_errors = nil @test_screenshots = nil end def group_parts parts = [] parts << @screenshot_section if @screenshot_section.present? parts << @screenshot_group if @screenshot_group.present? parts end def full_name(name) File.join group_parts.<<(name).map(&:to_s) end def screenshot_dir File.join [self.class.screenshot_area] + group_parts end private def current_capybara_driver_class Capybara.drivers[Capybara.current_driver].call({}).class end private def selenium? current_capybara_driver_class <= Capybara::Selenium::Driver end private def poltergeist? return false unless defined?(Capybara::Poltergeist::Driver) current_capybara_driver_class <= Capybara::Poltergeist::Driver end setup do if Capybara::Screenshot.window_size if selenium? # TODO(uwe): Enable for Chrome and non-windows when it works) if !page.driver.chrome? || ON_WINDOWS page.driver.browser.manage.window.resize_to(*Capybara::Screenshot.window_size) end elsif poltergeist? page.driver.resize(*Capybara::Screenshot.window_size) end end end teardown do if Capybara::Screenshot::Diff.enabled && @test_screenshots test_screenshot_errors = @test_screenshots .map { |caller, name, compare| assert_image_not_changed(caller, name, compare) }.compact fail(test_screenshot_errors.join("\n\n")) if test_screenshot_errors.any? end end def screenshot_section(name) @screenshot_section = name.to_s end def screenshot_group(name) @screenshot_group = name.to_s @screenshot_counter = 0 return unless Capybara::Screenshot.active? && name.present? FileUtils.rm_rf screenshot_dir end def screenshot(name, color_distance_limit: Capybara::Screenshot::Diff.color_distance_limit, area_size_limit: nil) return unless Capybara::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 = "#{self.class.screenshot_area_abs}/#{name}.png" FileUtils.mkdir_p File.dirname(file_name) comparison = Capybara::Screenshot::Diff::ImageCompare.new(file_name, dimensions: Capybara::Screenshot.window_size, color_distance_limit: color_distance_limit, area_size_limit: area_size_limit) checkout_vcs(name, comparison) take_stable_screenshot(comparison) return unless comparison.old_file_exists? (@test_screenshots ||= []) << [caller[0], name, comparison] end private def window_size_is_wrong? selenium? && Capybara::Screenshot.window_size && (!page.driver.chrome? || ON_WINDOWS) && # TODO(uwe): Allow for Chrome when it works page.driver.browser.manage.window.size != Selenium::WebDriver::Dimension.new(*Capybara::Screenshot.window_size) end private def checkout_vcs(name, comparison) svn_file_name = "#{self.class.screenshot_area_abs}/.svn/text-base/#{name}.png.svn-base" if File.exist?(svn_file_name) committed_file_name = svn_file_name FileUtils.cp committed_file_name, comparison.old_file_name else svn_info = `svn info #{comparison.new_file_name} #{SILENCE_ERRORS}` if svn_info.present? wc_root = svn_info.slice(/(?<=Working Copy Root Path: ).*$/) checksum = svn_info.slice(/(?<=Checksum: ).*$/) if checksum committed_file_name = "#{wc_root}/.svn/pristine/#{checksum[0..1]}/#{checksum}.svn-base" FileUtils.cp committed_file_name, comparison.old_file_name end else restore_git_revision(name, comparison.old_file_name) end end end private def restore_git_revision(name, target_file_name) redirect_target = "#{target_file_name} #{SILENCE_ERRORS}" `git show HEAD~0:./#{self.class.screenshot_area}/#{name}.png > #{redirect_target}` FileUtils.rm_f(target_file_name) unless $CHILD_STATUS == 0 end IMAGE_WAIT_SCRIPT = < Capybara::Screenshot.stability_time_limit break end else last_image_change_at = Time.now end assert (Time.now - screeenshot_started_at) < Capybara.default_max_wait_time, "Could not get stable screenshot within #{Capybara.default_max_wait_time}s" end previous_file_size = new_file_size comparison.reset end ensure input.click if input end private def reduce_retina_image_size(file_name) return if !self.class.macos? || !selenium? || !Capybara::Screenshot.window_size saved_image = ChunkyPNG::Image.from_file(file_name) width = Capybara::Screenshot.window_size[0] return if saved_image.width < width * 2 height = (width * saved_image.height) / saved_image.width resized_image = saved_image.resample_bilinear(width, height) resized_image.save(file_name) end def assert_image_not_changed(caller, name, comparison) return unless comparison.different? "Screenshot does not match for '#{name}' (area: #{comparison.size}px #{comparison.dimensions}" \ ", max_color_distance: #{comparison.max_color_distance.round(1)})\n" \ "#{comparison.new_file_name}\n#{comparison.annotated_old_file_name}\n" \ "#{comparison.annotated_new_file_name}\n" \ "at #{caller}" end end end # rubocop:enable Metrics/ClassLength