Skip to content

Commit

Permalink
Fixes multiple problems (#132)
Browse files Browse the repository at this point in the history
* feat: do not expect to lazy loading images

Closes: #127

* bug: allows to pass wait and override timeout for stable screenshoter

Closes: #131

* chore: remove redundant operation

* chore: reduce requirement of the Rails

* chore: remove redundant chunky_png requirement

* chore: updates system font
  • Loading branch information
pftg authored Sep 12, 2024
1 parent 8fe324f commit 90f71e1
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
18 changes: 14 additions & 4 deletions lib/capybara/screenshot/diff/browser_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion lib/capybara/screenshot/diff/drivers/base_driver.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require "chunky_png"
require "capybara/screenshot/diff/difference"

module Capybara
Expand Down
1 change: 0 additions & 1 deletion lib/capybara/screenshot/diff/screenshoter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/screenshot/diff/stable_screenshoter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara_screenshot_diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
Binary file modified test/fixtures/app/doc/screenshots/macos/cuprite/index-vips.webp
Binary file not shown.

0 comments on commit 90f71e1

Please sign in to comment.