diff --git a/lib/capybara/screenshot/diff/browser_helpers.rb b/lib/capybara/screenshot/diff/browser_helpers.rb index 88714a3e..7350800f 100644 --- a/lib/capybara/screenshot/diff/browser_helpers.rb +++ b/lib/capybara/screenshot/diff/browser_helpers.rb @@ -30,13 +30,23 @@ def self.bounds_for_css(*css_selectors) IMAGE_WAIT_SCRIPT = <<~JS function pending_image() { - var images = document.images; + function isInViewport(element) { + const rect = element.getBoundingClientRect(); + return ( + rect.top >= 0 && + rect.left >= 0 && + rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && + rect.right <= (window.innerWidth || document.documentElement.clientWidth) + ); + } + + const images = document.images for (var i = 0; i < images.length; i++) { - if (!images[i].complete) { - return images[i].src; + if (!images[i].complete && ("lazy" !== images[i].loading || isInViewport(images[i]))) { + return images[i].src } } - return false; + return false }(window) JS diff --git a/lib/capybara/screenshot/diff/drivers/base_driver.rb b/lib/capybara/screenshot/diff/drivers/base_driver.rb index 0cddbbf1..8082966b 100644 --- a/lib/capybara/screenshot/diff/drivers/base_driver.rb +++ b/lib/capybara/screenshot/diff/drivers/base_driver.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require "chunky_png" require "capybara/screenshot/diff/difference" module Capybara diff --git a/lib/capybara/screenshot/diff/screenshoter.rb b/lib/capybara/screenshot/diff/screenshoter.rb index 7718746c..a05e31c7 100644 --- a/lib/capybara/screenshot/diff/screenshoter.rb +++ b/lib/capybara/screenshot/diff/screenshoter.rb @@ -82,7 +82,6 @@ def wait_images_loaded(timeout:) deadline_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout loop do - Capybara.default_max_wait_time pending_image = BrowserHelpers.pending_image_to_load break unless pending_image diff --git a/lib/capybara/screenshot/diff/stable_screenshoter.rb b/lib/capybara/screenshot/diff/stable_screenshoter.rb index 992e9e8b..331a7137 100644 --- a/lib/capybara/screenshot/diff/stable_screenshoter.rb +++ b/lib/capybara/screenshot/diff/stable_screenshoter.rb @@ -26,7 +26,7 @@ def initialize(capture_options, comparison_options = {}) @comparison_options = comparison_options driver = Diff::Drivers.for(@comparison_options) - @screenshoter = Diff.screenshoter.new(capture_options.except(*STABILITY_OPTIONS), driver) + @screenshoter = Diff.screenshoter.new(capture_options.except(:stability_time_limit), driver) end # Takes a comparison screenshot ensuring page stability diff --git a/lib/capybara_screenshot_diff.rb b/lib/capybara_screenshot_diff.rb index 3e3bd604..ff84eb4f 100644 --- a/lib/capybara_screenshot_diff.rb +++ b/lib/capybara_screenshot_diff.rb @@ -20,7 +20,7 @@ module Screenshot mattr_accessor :blur_active_element mattr_accessor :enabled mattr_accessor :hide_caret - mattr_reader(:root) { (defined?(Rails.root) && Rails.root) || Pathname(".").expand_path } + 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" } diff --git a/test/fixtures/app/doc/screenshots/macos/cuprite/index-vips.webp b/test/fixtures/app/doc/screenshots/macos/cuprite/index-vips.webp index 2c77ba1f..d5da2f3f 100644 Binary files a/test/fixtures/app/doc/screenshots/macos/cuprite/index-vips.webp and b/test/fixtures/app/doc/screenshots/macos/cuprite/index-vips.webp differ