Class: LookbookVisualTester::ReportGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/lookbook_visual_tester/report_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReportGenerator

Returns a new instance of ReportGenerator.



5
6
7
8
9
10
11
# File 'lib/lookbook_visual_tester/report_generator.rb', line 5

def initialize
  @baseline_dir, @current_dir, @diff_dir, @base_path = LookbookVisualTester.config.then do |config|
    [config.baseline_dir, config.current_dir, config.diff_dir, config.base_path]
  end

  @report_path = base_path.join('report.html')
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



3
4
5
# File 'lib/lookbook_visual_tester/report_generator.rb', line 3

def base_path
  @base_path
end

#baseline_dirObject (readonly)

Returns the value of attribute baseline_dir.



3
4
5
# File 'lib/lookbook_visual_tester/report_generator.rb', line 3

def baseline_dir
  @baseline_dir
end

#current_dirObject (readonly)

Returns the value of attribute current_dir.



3
4
5
# File 'lib/lookbook_visual_tester/report_generator.rb', line 3

def current_dir
  @current_dir
end

#diff_dirObject (readonly)

Returns the value of attribute diff_dir.



3
4
5
# File 'lib/lookbook_visual_tester/report_generator.rb', line 3

def diff_dir
  @diff_dir
end

#report_pathObject (readonly)

Returns the value of attribute report_path.



3
4
5
# File 'lib/lookbook_visual_tester/report_generator.rb', line 3

def report_path
  @report_path
end

Instance Method Details

#generateObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lookbook_visual_tester/report_generator.rb', line 13

def generate
  File.open(report_path, 'w') do |file|
    file.puts '<!DOCTYPE html>'
    file.puts "<html lang='en'>"
    file.puts "<head><meta charset='UTF-8'><title>Visual Regression Report</title></head>"
    file.puts '<body>'
    file.puts '<h1>Visual Regression Report</h1>'
    file.puts '<ul>'

    diff_files = Dir.glob(diff_dir.join('*_diff.png'))
    diff_files.each do |diff_file|
      filename = File.basename(diff_file)
      # Extract preview and scenario names
      preview_scenario = filename.sub('_diff.png', '')
      preview, scenario = preview_scenario.split('_', 2)

      baseline_image = baseline_dir.join("#{preview}_#{scenario}.png")
      current_image = current_dir.join("#{preview}_#{scenario}.png")

      file.puts '<li>'
      file.puts "<h2>#{preview.titleize} - #{scenario.titleize}</h2>"
      file.puts "<div style='display: flex; gap: 10px;'>"

      baseline_image_path = Pathname.new(baseline_image)
      current_image_path = Pathname.new(current_image)
      diff_file_path = Pathname.new(diff_file)

      file.puts "<div><h3>Baseline</h3><img src='#{baseline_image_path.relative_path_from(base_path)}' alt='Baseline'></div>"
      file.puts "<div><h3>Current</h3><img src='#{current_image_path.relative_path_from(base_path)}' alt='Current'></div>"
      file.puts "<div><h3>Diff</h3><img src='#{diff_file_path.relative_path_from(base_path)}' alt='Diff'></div>"
      file.puts '</div>'
      file.puts '</li>'
    end

    file.puts '<li>No differences found!</li>' if diff_files.empty?

    file.puts '</ul>'
    file.puts '</body>'
    file.puts '</html>'
  end

  @report_path
end