Skip to content

Commit

Permalink
Merge pull request #336 from genya0407/feature/better-visual-regression
Browse files Browse the repository at this point in the history
Fix visual regression spec
  • Loading branch information
genya0407 authored Dec 9, 2023
2 parents 725744b + 31d4249 commit 4fd3a3b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 47 deletions.
10 changes: 5 additions & 5 deletions app/lib/visual_regression_test/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ def click_rotated(x:, y:) # rubocop:disable Naming/MethodParameterName
end

def wait_server_up(path)
loop do
sleep 1
go_to(path)
break if @browser.network.status
end
go_to(path)
@browser.network.status
rescue Ferrum::StatusError
sleep 1
retry
end

def go_to(path)
Expand Down
71 changes: 29 additions & 42 deletions lib/tasks/visual_regression_test.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,36 @@ def _system(cmd, exception: true)
system(cmd, exception:)
end

def fork_run_block(&)
pid = fork(&)

_, status = Process.wait2(pid)
raise "Process exited with status code: #{status.exitstatus}. at #{caller.first}" unless status.success?
def take_screenshot(output_dir:, freeze_time_at: Time.zone.now.to_i)
FileUtils.mkdir_p(output_dir)
scenario = VisualRegressionTest::Scenario.new(output_dir:, freeze_time_at:)
scenario.execute
end

namespace :visual_regression_test do
task take_screenshot: :environment do
output_dir = ENV['OUTPUT_DIR'] || 'tmp/visual_regression_test'
freeze_time_at = ENV['FREEZE_TIME_AT'].to_i || Time.zone.now.to_i
FileUtils.mkdir_p(output_dir)
scenario = VisualRegressionTest::Scenario.new(output_dir:, freeze_time_at:)
scenario.execute
end
def compare_screenshot(before_dir:, after_dir:, compare_dir:)
FileUtils.mkdir_p(compare_dir)

task compare: :environment do
before_dir = ENV.fetch('BEFORE_DIR')
after_dir = ENV.fetch('AFTER_DIR')
compare_dir = ENV.fetch('OUTPUT_DIR')
FileUtils.mkdir_p(compare_dir)
Dir.glob(File.join(after_dir, '*')).each do |entry|
filename = File.basename(entry)
before_file = File.join(before_dir, filename)
after_file = File.join(after_dir, filename)
next if File.read(before_file) == File.read(after_file)

Dir.glob(File.join(after_dir, '*')).each do |entry|
filename = File.basename(entry)
before_file = File.join(before_dir, filename)
after_file = File.join(after_dir, filename)
next if File.read(before_file) == File.read(after_file)
compare_file = File.join(compare_dir, filename)
_system("compare #{after_file} #{before_file} #{compare_file}", exception: false)
end
end

compare_file = File.join(compare_dir, filename)
_system("compare #{after_file} #{before_file} #{compare_file}", exception: false)
end
def combine(before_dir:, after_dir:, compare_dir:, combined_dir:)
FileUtils.mkdir_p(combined_dir)

Dir.glob(File.join(compare_dir, '*')).each do |path|
entry = File.basename path
_system("convert +append #{File.join(before_dir, entry)} #{File.join(after_dir, entry)} #{File.join(compare_dir, entry)} #{File.join(combined_dir, entry)}", exception: false)
end
end

namespace :visual_regression_test do
task run: :environment do
raise 'git diff exists' unless `git status --porcelain`.strip.lines.empty?

Expand All @@ -48,32 +45,22 @@ namespace :visual_regression_test do
before_dir = File.join('tmp', 'visual_regression_test_auto', base_ref.to_s)
after_dir = File.join('tmp', 'visual_regression_test_auto', current_ref.to_s)
compare_dir = File.join('tmp', 'visual_regression_test_auto', 'compare')
combined_dir = File.join('tmp', 'visual_regression_test_auto', 'combined')

_system('rm -rf tmp/visual_regression_test_auto')

fork_run_block do
ENV['OUTPUT_DIR'] = after_dir
ENV['FREEZE_TIME_AT'] = freeze_time_at.to_s
Rake::Task['visual_regression_test:take_screenshot'].invoke
end
take_screenshot(output_dir: after_dir, freeze_time_at:)

fork_run_block do
begin
_system("git checkout #{base_branch}")
_system('bundle install')

ENV['OUTPUT_DIR'] = before_dir
ENV['FREEZE_TIME_AT'] = freeze_time_at.to_s
Rake::Task['visual_regression_test:take_screenshot'].invoke
take_screenshot(output_dir: before_dir, freeze_time_at:)
ensure
_system("git checkout #{current_branch}")
end

fork_run_block do
ENV['BEFORE_DIR'] = before_dir
ENV['AFTER_DIR'] = after_dir
ENV['OUTPUT_DIR'] = compare_dir
compare_screenshot(before_dir:, after_dir:, compare_dir:)

Rake::Task['visual_regression_test:compare'].invoke
end
combine(before_dir:, after_dir:, compare_dir:, combined_dir:)
end
end

0 comments on commit 4fd3a3b

Please sign in to comment.