Skip to content

Commit 90f71e1

Browse files
authored
Fixes multiple problems (#132)
* 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
1 parent 8fe324f commit 90f71e1

6 files changed

Lines changed: 16 additions & 8 deletions

File tree

lib/capybara/screenshot/diff/browser_helpers.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,23 @@ def self.bounds_for_css(*css_selectors)
3030

3131
IMAGE_WAIT_SCRIPT = <<~JS
3232
function pending_image() {
33-
var images = document.images;
33+
function isInViewport(element) {
34+
const rect = element.getBoundingClientRect();
35+
return (
36+
rect.top >= 0 &&
37+
rect.left >= 0 &&
38+
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
39+
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
40+
);
41+
}
42+
43+
const images = document.images
3444
for (var i = 0; i < images.length; i++) {
35-
if (!images[i].complete) {
36-
return images[i].src;
45+
if (!images[i].complete && ("lazy" !== images[i].loading || isInViewport(images[i]))) {
46+
return images[i].src
3747
}
3848
}
39-
return false;
49+
return false
4050
}(window)
4151
JS
4252

lib/capybara/screenshot/diff/drivers/base_driver.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "chunky_png"
43
require "capybara/screenshot/diff/difference"
54

65
module Capybara

lib/capybara/screenshot/diff/screenshoter.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def wait_images_loaded(timeout:)
8282

8383
deadline_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
8484
loop do
85-
Capybara.default_max_wait_time
8685
pending_image = BrowserHelpers.pending_image_to_load
8786
break unless pending_image
8887

lib/capybara/screenshot/diff/stable_screenshoter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def initialize(capture_options, comparison_options = {})
2626
@comparison_options = comparison_options
2727

2828
driver = Diff::Drivers.for(@comparison_options)
29-
@screenshoter = Diff.screenshoter.new(capture_options.except(*STABILITY_OPTIONS), driver)
29+
@screenshoter = Diff.screenshoter.new(capture_options.except(:stability_time_limit), driver)
3030
end
3131

3232
# Takes a comparison screenshot ensuring page stability

lib/capybara_screenshot_diff.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Screenshot
2020
mattr_accessor :blur_active_element
2121
mattr_accessor :enabled
2222
mattr_accessor :hide_caret
23-
mattr_reader(:root) { (defined?(Rails.root) && Rails.root) || Pathname(".").expand_path }
23+
mattr_reader(:root) { (defined?(Rails) && defined?(Rails.root) && Rails.root) || Pathname(".").expand_path }
2424
mattr_accessor :stability_time_limit
2525
mattr_accessor :window_size
2626
mattr_accessor(:save_path) { "doc/screenshots" }
12 Bytes
Loading

0 commit comments

Comments
 (0)