Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Spider component guide, run vr against previews
Browse files Browse the repository at this point in the history
We can’t easily get a list of URLs for a component guide. Instead use
the wraith spider feature and crawl the review domain’s component
guide. Exclude paths that don’t start with /component-guide and only
compare paths that end with `/preview` to limit the number of pages and
speed up the test run.

* Include rake tasks for running locally
* Use Heroku runner in app
  • Loading branch information
fofr committed Sep 14, 2017
1 parent 4fb92c6 commit d1cf92a
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
results/visual
config/tmp*
config/spider_paths*
node_modules/
9 changes: 7 additions & 2 deletions app.rb
Expand Up @@ -13,6 +13,11 @@
end

environment = payload['deployment']['environment']
paths = GovukVisualRegression::VisualDiff::DocumentTypes.new.type_paths('statistics')
GovukVisualRegression::VisualDiff::HerokuRunner.new(paths: paths, environment: environment).run
review_domain = "https://#{environment}.herokuapp.com"
live_domain = "https://#{environment.gsub(/\-pr\-\d+$/, '')}.herokuapp.com"
surge_domain = "#{environment}.surge.sh"

GovukVisualRegression::VisualDiff::Runner.new(review_domain: review_domain).spider_component_guide
paths = GovukVisualRegression::VisualDiff::SpiderPaths.component_preview_paths
GovukVisualRegression::VisualDiff::HerokuRunner.new(paths: paths, review_domain: review_domain, live_domain: live_domain, surge_domain: surge_domain).run
end
1 change: 0 additions & 1 deletion config/wraith.yaml
Expand Up @@ -5,7 +5,6 @@ browser:
directory: results/visual
screen_widths:
- 320x5000
- 600x4000
- 1080x3000
resize_or_reload: 'resize'
mode: diffs_first
Expand Down
4 changes: 4 additions & 0 deletions lib/govuk_visual_regression.rb
Expand Up @@ -13,6 +13,10 @@ def self.wraith_config_template
config_file 'wraith.yaml'
end

def self.spider_paths
config_file 'spider_paths.yml'
end

def self.config_file(filename)
File.expand_path(root_dir + "/config/#{filename}")
end
Expand Down
25 changes: 25 additions & 0 deletions lib/govuk_visual_regression/tasks/rakefile.rake
Expand Up @@ -6,6 +6,14 @@ def load_paths
YAML.load(open(ENV.fetch("URI")))
end

def review_domain
ENV.fetch("REVIEW_DOMAIN")
end

def live_domain
ENV.fetch("LIVE_DOMAIN")
end

namespace :diff do
desc 'Set env var `URI` with location of a yaml file containing paths to diff'
task visual: ['config:pre_flight_check'] do |_t, args|
Expand All @@ -20,6 +28,16 @@ namespace :diff do
GovukVisualRegression::VisualDiff::Runner.new(paths: paths).run
end

desc 'Set env var `REVIEW_DOMAIN` to the domain hosting the newer component guide and `LIVE_DOMAIN` to compare with'
task component_guide: ['spider:component_guide'] do |_t, args|
paths = GovukVisualRegression::VisualDiff::SpiderPaths.component_preview_paths
if paths.any?
GovukVisualRegression::VisualDiff::Runner.new(paths: paths, review_domain: review_domain, live_domain: live_domain).run
else
puts "No paths found"
end
end

desc "clears the results directory"
task :clear_results do
puts "---> Clearing results directory"
Expand All @@ -28,6 +46,13 @@ namespace :diff do
end
end

namespace :spider do
desc 'Set env var `REVIEW_DOMAIN` to the domain hosting the component guide'
task component_guide: ['config:pre_flight_check'] do |_t, args|
GovukVisualRegression::VisualDiff::Runner.new(review_domain: review_domain).spider_component_guide
end
end

namespace :config do
desc "Checks that dependencies are in place"
task :pre_flight_check do
Expand Down
2 changes: 2 additions & 0 deletions lib/govuk_visual_regression/visual_diff.rb
Expand Up @@ -3,6 +3,8 @@ module VisualDiff
autoload :Runner, 'govuk_visual_regression/visual_diff/runner'
autoload :HerokuRunner, 'govuk_visual_regression/visual_diff/heroku_runner'
autoload :WraithConfig, 'govuk_visual_regression/visual_diff/wraith_config'
autoload :WraithSpiderComponentGuideConfig, 'govuk_visual_regression/visual_diff/wraith_spider_component_guide_config'
autoload :DocumentTypes, 'govuk_visual_regression/visual_diff/document_types'
autoload :SpiderPaths, 'govuk_visual_regression/visual_diff/spider_paths'
end
end
11 changes: 9 additions & 2 deletions lib/govuk_visual_regression/visual_diff/heroku_runner.rb
@@ -1,13 +1,20 @@
module GovukVisualRegression
module VisualDiff
class HerokuRunner < Runner
def initialize(paths: [], review_domain: nil, live_domain: nil, surge_domain: nil, kernel: Kernel)
@paths = paths
@kernel = kernel
@review_domain = review_domain
@live_domain = live_domain
@surge_domain = surge_domain
end

def install_surge
@kernel.system "yarn global add surge"
end

def upload_to_surge
surge_domain = @environment ? @environment : "govuk-vr"
@kernel.system "surge --project results/visual/ --domain #{surge_domain}.surge.sh"
@kernel.system "surge --project results/visual/ --domain #{@surge_domain}"
end

def run
Expand Down
18 changes: 14 additions & 4 deletions lib/govuk_visual_regression/visual_diff/runner.rb
@@ -1,15 +1,25 @@
module GovukVisualRegression
module VisualDiff
class Runner
def initialize(paths:, environment: nil, kernel: Kernel)
def initialize(paths: [], review_domain: nil, live_domain: nil, kernel: Kernel)
@paths = paths
@kernel = kernel
@environment = environment
@review_domain = review_domain
@live_domain = live_domain
end

def spider_component_guide
wraith_config = WraithSpiderComponentGuideConfig.new(review_domain: @review_domain)
wraith_config.write

cmd = "wraith spider #{wraith_config.location}"
puts "---> Running component guide spider on #{@review_domain}"
puts "running: #{cmd}"
@kernel.system cmd
end

def run
review_domain = @environment ? "https://#{@environment}.herokuapp.com" : nil
wraith_config = WraithConfig.new(paths: @paths, review_domain: review_domain)
wraith_config = WraithConfig.new(paths: @paths, review_domain: @review_domain, live_domain: @live_domain)
wraith_config.write

cmd = "wraith capture #{wraith_config.location}"
Expand Down
16 changes: 16 additions & 0 deletions lib/govuk_visual_regression/visual_diff/spider_paths.rb
@@ -0,0 +1,16 @@
require 'yaml'

module GovukVisualRegression
module VisualDiff
module SpiderPaths
def self.paths
YAML.load_file(GovukVisualRegression.spider_paths)['paths']
end

# Only test component previews, not the guide itself
def self.component_preview_paths
paths.values.select { |v| v.match(/preview$/) }
end
end
end
end
9 changes: 6 additions & 3 deletions lib/govuk_visual_regression/visual_diff/wraith_config.rb
@@ -1,5 +1,4 @@
require 'yaml'
require 'securerandom'

module GovukVisualRegression
module VisualDiff
Expand All @@ -9,11 +8,11 @@ class WraithConfig
attr_reader :review_domain
attr_reader :live_domain

def initialize(paths:, review_domain: nil, live_domain: nil)
def initialize(paths: [], review_domain: nil, live_domain: nil)
@paths = paths
@live_domain = live_domain
@review_domain = review_domain
@location = GovukVisualRegression.config_file("tmp_wraith_config.yaml")
@location = GovukVisualRegression.config_file(temporary_config_file_name)
end

def config
Expand Down Expand Up @@ -51,6 +50,10 @@ def path_would_break_wraith?(path)
def path_config_name(path)
path.gsub('/', '_')
end

def temporary_config_file_name
"tmp_wraith_config.yaml"
end
end
end
end
@@ -0,0 +1,23 @@
module GovukVisualRegression
module VisualDiff
class WraithSpiderComponentGuideConfig < WraithConfig
def config
config = YAML.load_file(GovukVisualRegression.wraith_config_template)
config["domains"].delete("live")
config["domains"]["review"] = "#{review_domain}/component-guide" if review_domain

# http://bbc-news.github.io/wraith/#Spiderfunctionality
config["imports"] = "spider_paths.yml"

# Skip pages that don't begin with /component-guide
config["spider_skips"] = [/^\/(?!component\-guide)/]
config.delete("paths")
config
end

def temporary_config_file_name
"tmp_wraith_spider_component_guide_config.yaml"
end
end
end
end
2 changes: 1 addition & 1 deletion spec/govuk_visual_regression/visual_diff/runner_spec.rb
Expand Up @@ -12,7 +12,7 @@
end

it "executes wraith with the appropriate config" do
expect(config_handler_klass).to receive(:new).with(paths: ["/government/stats/foo", "/government/stats/bar"], review_domain: nil)
expect(config_handler_klass).to receive(:new).with(paths: ["/government/stats/foo", "/government/stats/bar"], review_domain: nil, live_domain: nil)
expect(config_handler).to receive(:write)
expect(kernel).to receive(:system).with("wraith capture #{config_handler.location}")
expect(config_handler).to receive(:delete)
Expand Down

0 comments on commit d1cf92a

Please sign in to comment.