Skip to content

Commit 6aa6ce3

Browse files
pftgclaude
andcommitted
feat: embed images as base64 data URIs in HTML report
Replace relative file paths with inline base64 data URIs so the HTML report is fully self-contained. Images now render in GitHub's artifact viewer (archive: false), offline, and when emailed. Also: - Fix typo: jquer-tablesorter → jquery-tablesorter - Move at_exit outside unless guard (always register) - Rename artifact to snap_diff-report.html Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b6382aa commit 6aa6ce3

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

docs/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ Capybara::Screenshot::Diff.color_distance_limit = 42
224224

225225
### Allowed shift distance
226226

227-
Sometimes you want to allow small movements in the images. For example, jquer-tablesorter
227+
Sometimes you want to allow small movements in the images. For example, jquery-tablesorter
228228
renders the same table slightly differently sometimes. You can set set the shift distance
229229
threshold for the comparison using the `shift_distance_limit` option to the `screenshot`
230230
method:

lib/capybara_screenshot_diff/reporters/html.rb

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

3+
require "base64"
34
require "erb"
45
require "fileutils"
56
require "pathname"
@@ -68,24 +69,26 @@ def failure_entry_for(name, compare)
6869
difference = compare.difference
6970
{
7071
name: name,
71-
original: relative_path(compare.base_image_path),
72-
new: relative_path(compare.image_path),
73-
base_diff: relative_path(compare.reporter.annotated_base_image_path),
74-
diff: relative_path(compare.reporter.annotated_image_path),
75-
heatmap: relative_path(compare.reporter.heatmap_diff_path),
72+
original: image_data_uri(compare.base_image_path),
73+
new: image_data_uri(compare.image_path),
74+
base_diff: image_data_uri(compare.reporter.annotated_base_image_path),
75+
diff: image_data_uri(compare.reporter.annotated_image_path),
76+
heatmap: image_data_uri(compare.reporter.heatmap_diff_path),
7677
diff_level: difference.ratio && (difference.ratio * 100).round(2),
7778
area_size: difference.region_area_size,
7879
max_color_distance: difference.meta[:max_color_distance]&.round(1)
7980
}
8081
end
8182

82-
def relative_path(path)
83+
def image_data_uri(path)
8384
return unless path
8485

8586
pathname = Pathname.new(path).expand_path
8687
return unless pathname.exist?
8788

88-
pathname.relative_path_from(@report_dir.expand_path).to_s
89+
ext = pathname.extname.delete_prefix(".")
90+
mime = (ext == "webp") ? "image/webp" : "image/png"
91+
"data:#{mime};base64,#{Base64.strict_encode64(pathname.binread)}"
8992
end
9093

9194
def write_report
@@ -98,6 +101,10 @@ def write_report
98101

99102
unless CapybaraScreenshotDiff.reporters.any?(CapybaraScreenshotDiff::Reporters::HTML)
100103
CapybaraScreenshotDiff.reporters << CapybaraScreenshotDiff::Reporters::HTML.new
104+
end
105+
106+
unless defined?(@_snap_diff_at_exit_registered)
107+
@_snap_diff_at_exit_registered = true
101108

102109
at_exit do
103110
CapybaraScreenshotDiff.reporters.each do |reporter|

0 commit comments

Comments
 (0)