Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/capybara/screenshot/diff/browser_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def self.blur_from_focused_element
JS

def self.all_visible_regions_for(selector)
BrowserHelpers.session.all(selector, visible: true).map(&method(:region_for))
BrowserHelpers.session.all(selector, visible: true).map { |el| region_for(el) }
end

def self.region_for(element)
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/screenshot/diff/reporters/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Default
def initialize(difference)
@difference = difference

screenshot_format = difference.comparison.options[:screenshot_format] || comparison.new_image_path.extname.slice(1..-1)
screenshot_format = difference.comparison.options[:screenshot_format] || comparison.new_image_path.extname.delete_prefix(".").then { |ext| ext.empty? ? "png" : ext }
@annotated_image_path = comparison.new_image_path.sub_ext(".diff.#{screenshot_format}")
@annotated_base_image_path = comparison.base_image_path.sub_ext(".diff.#{screenshot_format}")
@heatmap_diff_path = comparison.new_image_path.sub_ext(".heatmap.diff.#{screenshot_format}")
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/screenshot/diff/screenshot_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def capture_screenshot(capture_options, comparison_options)
screenshoter = if capture_options[:stability_time_limit]
StableScreenshoter.new(capture_options, comparison_options)
else
Diff.screenshoter.new(capture_options, comparison_options[:driver])
Diff.screenshoter.new(capture_options, comparison_options)
end
screenshoter.take_comparison_screenshot(@snapshot)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/capybara/screenshot/diff/screenshoter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module Screenshot
class Screenshoter
attr_reader :capture_options, :driver

def initialize(capture_options, driver)
def initialize(capture_options, comparison_options = {})
@capture_options = capture_options
@driver = driver
@driver = Diff::Drivers.for(comparison_options)
end

def crop
Expand Down Expand Up @@ -101,7 +101,7 @@ def save_and_process_screenshot(screenshot_path)
# Load saved screenshot and pre-process it
process_screenshot(tmpfile.path, screenshot_path)
ensure
File.unlink(tmpfile) if tmpfile
tmpfile&.close!
end

def capture_screenshot_at(snapshot)
Expand Down
10 changes: 4 additions & 6 deletions lib/capybara/screenshot/diff/stable_screenshoter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,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_time_limit), driver)
@screenshoter = Diff.screenshoter.new(capture_options.except(:stability_time_limit), @comparison_options)
end

# Takes a comparison screenshot ensuring page stability
Expand Down Expand Up @@ -61,14 +60,13 @@ def take_stable_screenshot(snapshot)
# Cleanup all previous attempts for sure
snapshot.cleanup_attempts!

0.step do |i|
# FIXME: it should be wait, and wait should be replaced with stability_time_limit
sleep(stability_time_limit) unless i == 0 # test prev_attempt_path is nil

loop do
attempt_next_screenshot(snapshot)

return true if attempt_successful?(snapshot)
return false if timeout?(deadline_at)

sleep(stability_time_limit)
end
end

Expand Down
7 changes: 7 additions & 0 deletions lib/capybara_screenshot_diff/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def screenshot(name, skip_stack_frames: 0, **options)
# @see #screenshot
alias_method :assert_matches_screenshot, :screenshot

# Asserts the current page has no visual changes from the baseline.
# Override in your base test class to add project-specific behavior
# (e.g., waiting for Turbo, default skip areas).
def assert_no_screenshot_changes(name, skip_stack_frames: 0, **opts)
screenshot(name, skip_stack_frames: skip_stack_frames + 1, **opts)
end
Comment thread
pftg marked this conversation as resolved.

private

# Builds a screenshot assertion object that can be validated immediately or later.
Expand Down
6 changes: 3 additions & 3 deletions test/unit/screenshoter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ScreenshoterTest < ActiveSupport::TestCase
include CapybaraScreenshotDiff::DSLStub

test "#take_screenshot without wait skips image loading" do
screenshoter = Screenshoter.new({wait: nil}, ::Minitest::Mock.new)
screenshoter = Screenshoter.new({wait: nil}, {driver: :chunky_png})

mock = ::Minitest::Mock.new
mock.expect(:save_screenshot, true) { |path| path.include?("01_a.png") }
Expand All @@ -27,7 +27,7 @@ class ScreenshoterTest < ActiveSupport::TestCase
test "#take_screenshot with custom screenshot options" do
screenshoter = Screenshoter.new(
{wait: nil, capybara_screenshot_options: {full: true}},
::Minitest::Mock.new
{driver: :chunky_png}
)

mock = ::Minitest::Mock.new
Expand All @@ -43,7 +43,7 @@ class ScreenshoterTest < ActiveSupport::TestCase
end

test "#prepare_page_for_screenshot without wait does not raise any error" do
screenshoter = Screenshoter.new({wait: nil}, ::Minitest::Mock.new)
screenshoter = Screenshoter.new({wait: nil}, {driver: :chunky_png})

assert_nil screenshoter.prepare_page_for_screenshot(timeout: nil) # does not raise an error
end
Expand Down
Loading