From 87bc88cd3308ecf1a1fc6bccdf2ecd75803e7469 Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Mon, 14 Dec 2020 19:19:38 +0000 Subject: [PATCH] Quick demo --- .github/workflows/visual-regression-tests.yml | 2 +- Rakefile | 4 ++ bin/visual_regression_tests | 66 +++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100755 bin/visual_regression_tests diff --git a/.github/workflows/visual-regression-tests.yml b/.github/workflows/visual-regression-tests.yml index ad4bc997b6..48802abf1d 100644 --- a/.github/workflows/visual-regression-tests.yml +++ b/.github/workflows/visual-regression-tests.yml @@ -35,6 +35,6 @@ jobs: - run: yarn install --frozen-lockfile - uses: percy/exec-action@v0.3.1 with: - command: bundle exec rspec ./spec/visual_regression_tests/all_components.rb + command: bundle exec rake visual_regression_tests env: PERCY_TOKEN: ${{ secrets.PERCY_TOKEN }} diff --git a/Rakefile b/Rakefile index 8062ad7a3e..d844d42ea5 100644 --- a/Rakefile +++ b/Rakefile @@ -33,4 +33,8 @@ task lint: %i[rubocop environment] do sh "yarn run lint" end +task :visual_regression_tests do + sh "bin/visual_regression_tests" +end + task default: [:lint, :spec, "app:jasmine:ci"] diff --git a/bin/visual_regression_tests b/bin/visual_regression_tests new file mode 100755 index 0000000000..a9c03e3eef --- /dev/null +++ b/bin/visual_regression_tests @@ -0,0 +1,66 @@ +#!/usr/bin/env ruby + +require "rubygems" +require "bundler/setup" + +ENV["RAILS_ENV"] ||= "test" + +require File.expand_path("../spec/dummy/config/environment", __dir__) + +require "capybara/rails" +require "govuk_test" +require "active_support/testing/time_helpers" +require "uri" +require "percy" + +GovukTest.configure +Capybara.configure do |config| + config.default_driver = :headless_chrome + config.default_max_wait_time = 10 +end + +class VisualRegressionTestRunner + include Capybara::DSL + include ActiveSupport::Testing::TimeHelpers + + COMPONENTS_TO_SKIP = [ + # not visible + "/component-guide/meta_tags", + "/component-guide/machine_readable_metadata", + "/component-guide/admin_analytics", + "/component-guide/google_tag_manager_script", + "/component-guide/layout_for_admin", + # times out + "/component-guide/govspeak", + ].freeze + + def self.call + new.call + end + + def call + travel_to(Time.zone.local(2020, 12, 1, 6, 0, 0)) do + visit("/component-guide") + + find("#list-all-components-in-the-gem") + .all("a") + .map { |link| URI(link[:href]).path } + .reject { |path| COMPONENTS_TO_SKIP.include?(path) } + .each { |path| test(path) } + end + end + + def test(path) + visit("#{path}/preview") + name = title.gsub(/(: Default|) preview - Component Guide/, "") + + if page.find(:css, "#wrapper") + puts "Screenshotting #{name}" + page.save_screenshot("#{name}.png") + else + puts "Failed to screenshot #{name}" + end + end +end + +VisualRegressionTestRunner.call