From 013466bdfdcc2b0e8dfeff3e5bf76fa6ab726749 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Thu, 5 Nov 2015 16:07:45 +0000 Subject: [PATCH 01/21] started work on #305 --- .../wraith}/javascript/_getDimensions.js | 0 .../wraith}/javascript/_phantom__common.js | 0 {templates => lib/wraith}/javascript/casper.js | 0 .../wraith}/javascript/phantom--nojs.js | 0 .../wraith}/javascript/phantom.js | 0 lib/wraith/save_images.rb | 18 +++++++++++++++++- lib/wraith/wraith.rb | 8 ++------ templates/javascript/README.md | 5 ----- 8 files changed, 19 insertions(+), 12 deletions(-) rename {templates => lib/wraith}/javascript/_getDimensions.js (100%) rename {templates => lib/wraith}/javascript/_phantom__common.js (100%) rename {templates => lib/wraith}/javascript/casper.js (100%) rename {templates => lib/wraith}/javascript/phantom--nojs.js (100%) rename {templates => lib/wraith}/javascript/phantom.js (100%) delete mode 100644 templates/javascript/README.md diff --git a/templates/javascript/_getDimensions.js b/lib/wraith/javascript/_getDimensions.js similarity index 100% rename from templates/javascript/_getDimensions.js rename to lib/wraith/javascript/_getDimensions.js diff --git a/templates/javascript/_phantom__common.js b/lib/wraith/javascript/_phantom__common.js similarity index 100% rename from templates/javascript/_phantom__common.js rename to lib/wraith/javascript/_phantom__common.js diff --git a/templates/javascript/casper.js b/lib/wraith/javascript/casper.js similarity index 100% rename from templates/javascript/casper.js rename to lib/wraith/javascript/casper.js diff --git a/templates/javascript/phantom--nojs.js b/lib/wraith/javascript/phantom--nojs.js similarity index 100% rename from templates/javascript/phantom--nojs.js rename to lib/wraith/javascript/phantom--nojs.js diff --git a/templates/javascript/phantom.js b/lib/wraith/javascript/phantom.js similarity index 100% rename from templates/javascript/phantom.js rename to lib/wraith/javascript/phantom.js diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index 68df9668..e54ff935 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -36,8 +36,24 @@ def save_images parallel_task(jobs) end + def engine_from_browser(snap_file) + case snap_file + when "phantom" + "phantomjs" + when "phantom--nojs" + "phantomjs" + when "casper" + "casperjs" + # @TODO - add a SlimerJS option + else + abort "Wraith does not know which browser engine to use for browser '#{snap_file}'" + end + end + def capture_page_image(browser, url, width, file_name, selector, global_before_capture, path_before_capture) - command = "#{browser} #{wraith.phantomjs_options} '#{wraith.snap_file}' '#{url}' '#{width}' '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}'" + + engine = engine_from_browser browser + command = "#{engine} #{wraith.phantomjs_options} 'javascript/#{browser}.js' '#{url}' '#{width}' '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}'" # @TODO - uncomment the following line when we add a verbose mode # puts command diff --git a/lib/wraith/wraith.rb b/lib/wraith/wraith.rb index 3b41e4f1..1c95bb22 100644 --- a/lib/wraith/wraith.rb +++ b/lib/wraith/wraith.rb @@ -23,8 +23,8 @@ def history_dir @config["history_dir"] end - def snap_file - @config["snap_file"] ? @config["snap_file"] : File.expand_path("lib/wraith/javascript/snap.js") + def engine + @config["browser"] end def before_capture @@ -75,10 +75,6 @@ def paths @config["paths"] end - def engine - @config["browser"] - end - def fuzz @config["fuzz"] end diff --git a/templates/javascript/README.md b/templates/javascript/README.md deleted file mode 100644 index af5a7c40..00000000 --- a/templates/javascript/README.md +++ /dev/null @@ -1,5 +0,0 @@ -These JavaScript files have been provided by Wraith for your benefit, but you are free to edit these or add new JavaScript files if you wish. - -Files beginning with an underscore should not be referenced directly in your YAML files. They are 'private' modules required by some of the other modules in this directory. - -The two most important files to be aware of are `casper.js` and `_phantom__common.js`, as these files map Wraith's command line arguments to each respective browser engine. From 97a1a7533a20ce00c10fe17343848b84d0390445 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 09:35:43 +0000 Subject: [PATCH 02/21] got tests passing --- lib/wraith/save_images.rb | 18 +------- lib/wraith/wraith.rb | 17 +++++++ spec/configs/test_config--casper.yaml | 4 +- spec/configs/test_config--phantom.yaml | 4 +- spec/js/custom_snap_file.js | 63 ++++++++++++++++++++++++++ spec/wraith_spec.rb | 25 +++++++++- 6 files changed, 107 insertions(+), 24 deletions(-) create mode 100644 spec/js/custom_snap_file.js diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index e54ff935..68df9668 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -36,24 +36,8 @@ def save_images parallel_task(jobs) end - def engine_from_browser(snap_file) - case snap_file - when "phantom" - "phantomjs" - when "phantom--nojs" - "phantomjs" - when "casper" - "casperjs" - # @TODO - add a SlimerJS option - else - abort "Wraith does not know which browser engine to use for browser '#{snap_file}'" - end - end - def capture_page_image(browser, url, width, file_name, selector, global_before_capture, path_before_capture) - - engine = engine_from_browser browser - command = "#{engine} #{wraith.phantomjs_options} 'javascript/#{browser}.js' '#{url}' '#{width}' '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}'" + command = "#{browser} #{wraith.phantomjs_options} '#{wraith.snap_file}' '#{url}' '#{width}' '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}'" # @TODO - uncomment the following line when we add a verbose mode # puts command diff --git a/lib/wraith/wraith.rb b/lib/wraith/wraith.rb index 1c95bb22..9b890c42 100644 --- a/lib/wraith/wraith.rb +++ b/lib/wraith/wraith.rb @@ -27,6 +27,23 @@ def engine @config["browser"] end + def snap_file + @config["snap_file"] || snap_file_from_engine(engine) + end + + def snap_file_from_engine(engine) + path_to_js_templates = File.dirname(__FILE__) + '/javascript' + case engine.keys.first + when "phantomjs" + path_to_js_templates + "/phantom.js" + when "casperjs" + path_to_js_templates + "/casper.js" + # @TODO - add a SlimerJS option + else + abort "Wraith does not recognise the browser engine '#{engine}'" + end + end + def before_capture @config["before_capture"] || "false" end diff --git a/spec/configs/test_config--casper.yaml b/spec/configs/test_config--casper.yaml index 2c8d355b..8f23fdb3 100644 --- a/spec/configs/test_config--casper.yaml +++ b/spec/configs/test_config--casper.yaml @@ -8,8 +8,8 @@ browser: phantomjs: "casperjs" # slimerjs: "slimerjs" -#If you want to have multiple snapping files, set the file name here -snap_file: "templates/javascript/casper.js" +#overriding the snap file +snap_file: "spec/js/custom_snap_file.js" # Type the name of the directory that shots will be stored in directory: 'shots' diff --git a/spec/configs/test_config--phantom.yaml b/spec/configs/test_config--phantom.yaml index 4ad420f1..699eb32b 100644 --- a/spec/configs/test_config--phantom.yaml +++ b/spec/configs/test_config--phantom.yaml @@ -6,10 +6,8 @@ #Headless browser option browser: phantomjs: "phantomjs" - # slimerjs: "slimerjs" -#If you want to have multiple snapping files, set the file name here -snap_file: "templates/javascript/phantom.js" +# we've deliberately left out the snap_file # Type the name of the directory that shots will be stored in directory: 'shots' diff --git a/spec/js/custom_snap_file.js b/spec/js/custom_snap_file.js new file mode 100644 index 00000000..c35d92d2 --- /dev/null +++ b/spec/js/custom_snap_file.js @@ -0,0 +1,63 @@ +// modules +var system = require('system'), + casper = require('casper').create(); + +// command line arguments +var url = casper.cli.get(0), + dimensions_tmp = /(\d*)x?((\d*))?/i.exec(casper.cli.get(1)); + dimensions = { + 'viewportWidth': parseInt(dimensions_tmp[1]), + 'viewportHeight': parseInt(dimensions_tmp[2] || 1500) + }, + image_name = casper.cli.get(2), + selector = casper.cli.get(3), + globalBeforeCaptureJS = casper.cli.get(4), + pathBeforeCaptureJS = casper.cli.get(5); + +// functions +function requireRelative(file) { + // PhantomJS will automatically `require` relatively, but CasperJS needs some extra help. Hence this function. + // 'templates/javascript/casper.js' -> 'templates/javascript' + var currentFilePath = system.args[3].split('/'); + currentFilePath.pop(); + var fs = require('fs'); + currentFilePath = fs.absolute(currentFilePath.join('/')); + return require(currentFilePath + '/' + file); +} +function snap() { + if (!selector) { + this.capture(image_name); + } + else { + this.captureSelector(image_name, selector); + } + console.log('Snapping ' + url + ' at: ' + dimensions.viewportWidth + 'x' + dimensions.viewportHeight); +} + +// Casper can now do its magic +casper.start(); +casper.open(url); +casper.viewport(dimensions.viewportWidth, dimensions.viewportHeight); +casper.then(function() { + if (globalBeforeCaptureJS) { + require('./' + globalBeforeCaptureJS)(this); + } +}); +casper.then(function() { + if (pathBeforeCaptureJS) { + require('./' + pathBeforeCaptureJS)(this); + } +}); +// waits for all images to download before taking screenshots +// (broken images are a big cause of Wraith failures!) +// Credit: http://reff.it/8m3HYP +casper.waitFor(function() { + return this.evaluate(function() { + var images = document.getElementsByTagName('img'); + return Array.prototype.every.call(images, function(i) { return i.complete; }); + }); +}, function then () { + snap.bind(this)(); +}); + +casper.run(); diff --git a/spec/wraith_spec.rb b/spec/wraith_spec.rb index 4358d8c9..f2aaf379 100644 --- a/spec/wraith_spec.rb +++ b/spec/wraith_spec.rb @@ -35,8 +35,29 @@ end context "When creating a wraith worker" do - it "should have 7 config keys" do - expect(wraith.config.keys.size).to be 7 + + it "should have a browser engine defined" do + expect(wraith.engine).to be_a Hash + end + + it "should have a directory defined" do + expect(wraith.directory).to be_a String + end + + it "should have domains defined" do + expect(wraith.domains).to be_a Hash + end + + it "should have screen widths defined" do + expect(wraith.widths).to be_a Array + end + + it "should have paths defined" do + expect(wraith.paths).to be_a Hash + end + + it "should have fuzz defined" do + expect(wraith.fuzz).to be_a String end it "should have widths" do From c3a24f33c3fcf268e7a194fd3fa379a69a777868 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 09:53:32 +0000 Subject: [PATCH 03/21] modularising the tests --- spec/{helpers.rb => _helpers.rb} | 3 ++ spec/config_spec.rb | 73 ++++++++++++++++++++++++++++++++ spec/wraith_spec.rb | 69 +----------------------------- 3 files changed, 77 insertions(+), 68 deletions(-) rename spec/{helpers.rb => _helpers.rb} (95%) create mode 100644 spec/config_spec.rb diff --git a/spec/helpers.rb b/spec/_helpers.rb similarity index 95% rename from spec/helpers.rb rename to spec/_helpers.rb index 76d9da5b..017404bd 100644 --- a/spec/helpers.rb +++ b/spec/_helpers.rb @@ -1,3 +1,6 @@ +require "rspec" +require "./lib/wraith/cli" + def create_diff_image wraith.engine.each do |_type, engine| saving.capture_page_image(engine, test_url1, 320, test_image1, selector, 'false', 'false') diff --git a/spec/config_spec.rb b/spec/config_spec.rb new file mode 100644 index 00000000..763154c6 --- /dev/null +++ b/spec/config_spec.rb @@ -0,0 +1,73 @@ +require "_helpers" + +describe "wraith config" do + + let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--phantom.yaml" } + let(:wraith) { Wraith::Wraith.new(config_name) } + + describe "Config" do + it "returns a Wraith class" do + expect(wraith).is_a? Wraith::Wraith + end + + it "when config is loaded" do + expect(wraith).to respond_to :config + end + + it "contains shot options" do + expect(wraith.config).to include "directory" => "shots" + end + end + + describe "When creating a wraith worker" do + + it "should have a browser engine defined" do + expect(wraith.engine).to be_a Hash + end + + it "should have a directory defined" do + expect(wraith.directory).to be_a String + end + + it "should have domains defined" do + expect(wraith.domains).to be_a Hash + end + + it "should have screen widths defined" do + expect(wraith.widths).to be_a Array + end + + it "should have paths defined" do + expect(wraith.paths).to be_a Hash + end + + it "should have fuzz defined" do + expect(wraith.fuzz).to be_a String + end + + it "should have widths" do + expect(wraith.widths).to eq [320, 600, 1280] + end + + it "include base domain" do + expect(wraith.base_domain).to eq "http://www.bbc.com/afrique" + end + + it "include compare domain" do + expect(wraith.comp_domain).to eq "http://www.bbc.com/russian" + end + + it "include base label" do + expect(wraith.base_domain_label).to eq "afrique" + end + + it "include compare label" do + expect(wraith.comp_domain_label).to eq "russian" + end + + it "include compare label" do + expect(wraith.paths).to eq("home" => "/", "uk_index" => "/uk") + end + end + +end diff --git a/spec/wraith_spec.rb b/spec/wraith_spec.rb index f2aaf379..d8695eb8 100644 --- a/spec/wraith_spec.rb +++ b/spec/wraith_spec.rb @@ -1,7 +1,5 @@ -require "rspec" +require "_helpers" require "image_size" -require "helpers" -require "./lib/wraith/cli" describe Wraith do let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--phantom.yaml" } @@ -20,71 +18,6 @@ Dir.mkdir("shots/test") end - describe "Config" do - it "returns a Wraith class" do - expect(wraith).is_a? Wraith::Wraith - end - - it "when config is loaded" do - expect(wraith).to respond_to :config - end - - it "contains shot options" do - expect(wraith.config).to include "directory" => "shots" - end - end - - context "When creating a wraith worker" do - - it "should have a browser engine defined" do - expect(wraith.engine).to be_a Hash - end - - it "should have a directory defined" do - expect(wraith.directory).to be_a String - end - - it "should have domains defined" do - expect(wraith.domains).to be_a Hash - end - - it "should have screen widths defined" do - expect(wraith.widths).to be_a Array - end - - it "should have paths defined" do - expect(wraith.paths).to be_a Hash - end - - it "should have fuzz defined" do - expect(wraith.fuzz).to be_a String - end - - it "should have widths" do - expect(wraith.widths).to eq [320, 600, 1280] - end - - it "include base domain" do - expect(wraith.base_domain).to eq "http://www.bbc.com/afrique" - end - - it "include compare domain" do - expect(wraith.comp_domain).to eq "http://www.bbc.com/russian" - end - - it "include base label" do - expect(wraith.base_domain_label).to eq "afrique" - end - - it "include compare label" do - expect(wraith.comp_domain_label).to eq "russian" - end - - it "include compare label" do - expect(wraith.paths).to eq("home" => "/", "uk_index" => "/uk") - end - end - describe "When capturing an image" do let(:image_size) { ImageSize.path(test_image1).size } From 1595517dfabbf51261d3ab0d25cea767dc135c81 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 10:48:41 +0000 Subject: [PATCH 04/21] expanded browser engine and snap file tests --- lib/wraith/wraith.rb | 9 +++++--- spec/config_spec.rb | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/lib/wraith/wraith.rb b/lib/wraith/wraith.rb index 9b890c42..00e79979 100644 --- a/lib/wraith/wraith.rb +++ b/lib/wraith/wraith.rb @@ -3,8 +3,10 @@ class Wraith::Wraith attr_accessor :config - def initialize(config_name) - if File.exist?(config_name) && File.extname(config_name) == ".yaml" + def initialize(config_name, yaml_passed = false) + if yaml_passed + @config = YAML.load(config_name) + elsif File.exist?(config_name) && File.extname(config_name) == ".yaml" @config = YAML.load(File.open(config_name)) else @config = YAML.load(File.open("configs/#{config_name}.yaml")) @@ -32,8 +34,9 @@ def snap_file end def snap_file_from_engine(engine) + engine = (engine.is_a? Hash) ? engine.values.first : engine path_to_js_templates = File.dirname(__FILE__) + '/javascript' - case engine.keys.first + case engine when "phantomjs" path_to_js_templates + "/phantom.js" when "casperjs" diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 763154c6..ab1b19b8 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -70,4 +70,56 @@ end end + describe "different ways of initialising browser engine" do + + it "should let us directly specify the engine" do + config = 'browser: phantomjs' + wraith = Wraith::Wraith.new(config, true) + + expect(wraith.engine).to eq 'phantomjs' + end + + it "should be backwards compatible with the old way" do + config = ' + browser: + phantomjs: "casperjs" + ' + wraith = Wraith::Wraith.new(config, true) + + expect(wraith.engine).to be_a Hash + expect(wraith.config['browser']['phantomjs']).to eq 'casperjs' + end + end + + describe "different ways of determining the snap file" do + + it "should calculate the snap file from the engine" do + config = 'browser: phantomjs' + wraith = Wraith::Wraith.new(config, true) + expect(wraith.snap_file).to include 'lib/wraith/javascript/phantom.js' + + config = 'browser: casperjs' + wraith = Wraith::Wraith.new(config, true) + expect(wraith.snap_file).to include 'lib/wraith/javascript/casper.js' + end + + it "should calculate the snap file in a backwards-compatible way" do + config = ' + browser: + phantomjs: "casperjs" + ' + wraith = Wraith::Wraith.new(config, true) + expect(wraith.snap_file).to include 'lib/wraith/javascript/casper.js' + end + + it "should allow users to specify their own snap file" do + config = ' + browser: casperjs + snap_file: path/to/snap.js + ' + wraith = Wraith::Wraith.new(config, true) + expect(wraith.snap_file).to eq 'path/to/snap.js' + end + end + end From 091b53949106483e53830fc799d23e8e96ef25d0 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 10:56:46 +0000 Subject: [PATCH 05/21] made yaml_passed parameter clearer --- lib/wraith/wraith.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/wraith/wraith.rb b/lib/wraith/wraith.rb index 00e79979..b3f49347 100644 --- a/lib/wraith/wraith.rb +++ b/lib/wraith/wraith.rb @@ -3,19 +3,22 @@ class Wraith::Wraith attr_accessor :config - def initialize(config_name, yaml_passed = false) - if yaml_passed - @config = YAML.load(config_name) - elsif File.exist?(config_name) && File.extname(config_name) == ".yaml" - @config = YAML.load(File.open(config_name)) - else - @config = YAML.load(File.open("configs/#{config_name}.yaml")) - end + def initialize(config, yaml_passed = false) + yaml_string = yaml_passed ? config : open_config_file(config) + @config = YAML.load yaml_string rescue - puts "unable to find config at #{config_name}" + puts "unable to find config at #{config}" exit 1 end + def open_config_file(config_name) + if File.exist?(config_name) && File.extname(config_name) == ".yaml" + File.open config_name + else + File.open "configs/#{config_name}.yaml" + end + end + def directory # Legacy support for those using array configs @config["directory"].is_a?(Array) ? @config["directory"].first : @config["directory"] From 43816ce0a10894c49c926c7905bb097bc02d17b9 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 11:13:48 +0000 Subject: [PATCH 06/21] simplified the way the browser engine is configured --- lib/wraith/save_images.rb | 8 ++------ lib/wraith/wraith.rb | 8 ++++++-- spec/_helpers.rb | 6 ++---- spec/config_spec.rb | 6 ++---- spec/wraith_spec.rb | 5 +---- 5 files changed, 13 insertions(+), 20 deletions(-) diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index 68df9668..c123cee8 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -142,12 +142,8 @@ def history_label history ? "_latest" : "" end - def engine_label - wraith.engine.key(engine) - end - def file_names(width, label, domain_label) - "#{wraith.directory}/#{label}/#{width}_#{engine_label}_#{domain_label}.png" + "#{wraith.directory}/#{label}/#{width}_#{engine}_#{domain_label}.png" end def base_label @@ -159,6 +155,6 @@ def compare_label end def engine - wraith.engine.each { |_label, browser| return browser } + wraith.engine end end diff --git a/lib/wraith/wraith.rb b/lib/wraith/wraith.rb index b3f49347..5413448f 100644 --- a/lib/wraith/wraith.rb +++ b/lib/wraith/wraith.rb @@ -29,7 +29,12 @@ def history_dir end def engine - @config["browser"] + engine = @config["browser"] + # Legacy support for those using the old style "browser: \n phantomjs: 'casperjs'" configs + if engine.is_a? Hash + engine = engine.values.first + end + engine end def snap_file @@ -37,7 +42,6 @@ def snap_file end def snap_file_from_engine(engine) - engine = (engine.is_a? Hash) ? engine.values.first : engine path_to_js_templates = File.dirname(__FILE__) + '/javascript' case engine when "phantomjs" diff --git a/spec/_helpers.rb b/spec/_helpers.rb index 017404bd..78be7fe9 100644 --- a/spec/_helpers.rb +++ b/spec/_helpers.rb @@ -2,10 +2,8 @@ require "./lib/wraith/cli" def create_diff_image - wraith.engine.each do |_type, engine| - saving.capture_page_image(engine, test_url1, 320, test_image1, selector, 'false', 'false') - saving.capture_page_image(engine, test_url2, 320, test_image2, selector, 'false', 'false') - end + saving.capture_page_image(wraith.engine, test_url1, 320, test_image1, selector, 'false', 'false') + saving.capture_page_image(wraith.engine, test_url2, 320, test_image2, selector, 'false', 'false') end def crop_images diff --git a/spec/config_spec.rb b/spec/config_spec.rb index ab1b19b8..389945b8 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -22,7 +22,7 @@ describe "When creating a wraith worker" do it "should have a browser engine defined" do - expect(wraith.engine).to be_a Hash + expect(wraith.engine).to be_a String end it "should have a directory defined" do @@ -85,9 +85,7 @@ phantomjs: "casperjs" ' wraith = Wraith::Wraith.new(config, true) - - expect(wraith.engine).to be_a Hash - expect(wraith.config['browser']['phantomjs']).to eq 'casperjs' + expect(wraith.engine).to eq 'casperjs' end end diff --git a/spec/wraith_spec.rb b/spec/wraith_spec.rb index d8695eb8..4bfb79fd 100644 --- a/spec/wraith_spec.rb +++ b/spec/wraith_spec.rb @@ -22,10 +22,7 @@ let(:image_size) { ImageSize.path(test_image1).size } it "saves image" do - wraith.engine.each do |_type, engine| - saving.capture_page_image(engine, test_url1, 320, test_image1, selector, 'false', 'false') - end - + saving.capture_page_image(wraith.engine, test_url1, 320, test_image1, selector, 'false', 'false') expect(image_size[0]).to eq 320 end end From 24854876a80d092148eaf1c4991f73669c6694cf Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 14:11:15 +0000 Subject: [PATCH 07/21] new example configs and fixed #325 --- lib/wraith/javascript/phantom--nojs.js | 1 + templates/configs/component.yaml | 8 +---- templates/configs/multiple_domains.yaml | 10 ++---- templates/configs/spider.yaml | 32 ++++++++----------- .../beforeCapture--casper_example.js | 13 ++++++-- .../beforeCapture--phantom_example.js | 11 +++++-- 6 files changed, 36 insertions(+), 39 deletions(-) diff --git a/lib/wraith/javascript/phantom--nojs.js b/lib/wraith/javascript/phantom--nojs.js index 95c75aac..d5b280e9 100644 --- a/lib/wraith/javascript/phantom--nojs.js +++ b/lib/wraith/javascript/phantom--nojs.js @@ -1,5 +1,6 @@ var system = require('system'); +// @TODO - make a javascript_disabled property in YAML, and pass via command line require('./_phantom__common.js')({ systemArgs: system.args, javascriptEnabled: false diff --git a/templates/configs/component.yaml b/templates/configs/component.yaml index 1cb9a11a..240fff1c 100644 --- a/templates/configs/component.yaml +++ b/templates/configs/component.yaml @@ -35,13 +35,7 @@ screen_widths: - 1280 # the engine to run Wraith with. -browser: - phantomjs: "casperjs" # variant of PhantomJS that allows screenshots by element selector - #phantomjs: "phantomjs" # PhantomJS, built on top of Webkit and JavascriptCore (like Safari) - #slimerjs: "slimerjs" # SlimerJS, built on top of Gecko and SpiderMonkey (like Firefox) - -# the file in charge of taking the screenshot -snap_file: "javascript/casper.js" +browser: "casperjs" # the directory that your base screenshots will be stored in history_dir: 'shots_base' diff --git a/templates/configs/multiple_domains.yaml b/templates/configs/multiple_domains.yaml index 2cb755f6..035d516f 100644 --- a/templates/configs/multiple_domains.yaml +++ b/templates/configs/multiple_domains.yaml @@ -30,14 +30,8 @@ screen_widths: - 1024 - 1280 -# the engine to run Wraith with. -browser: - #phantomjs: "casperjs" # variant of PhantomJS that allows screenshots by element selector - phantomjs: "phantomjs" # PhantomJS, built on top of Webkit and JavascriptCore (like Safari) - #slimerjs: "slimerjs" # SlimerJS, built on top of Gecko and SpiderMonkey (like Firefox) - -# the file in charge of taking the screenshot -snap_file: "javascript/phantom--nojs.js" +# the engine to run Wraith with. Examples: 'phantomjs', 'casperjs', 'slimerjs' +browser: "phantomjs" # the directory that your base screenshots will be stored in history_dir: 'shots_base' diff --git a/templates/configs/spider.yaml b/templates/configs/spider.yaml index 61d6ed3a..7e072cb6 100644 --- a/templates/configs/spider.yaml +++ b/templates/configs/spider.yaml @@ -5,19 +5,19 @@ domains: # Notice the absence of a `paths` property. When no paths are provided, Wraith defaults to # spidering mode to check your entire website. -​ -​# A list of URLs to skip when spidering. -# Ruby regular expressions can be​ used, if prefixed with `!ruby/regexp` as defined in the YAML Cookbook​. -​# See http://www.yaml.org/YAML_for_ruby.html#regexps​ -​spider_skips:​ - - /foo/bar.html # Matches /foo/bar.html explicitly​ -​ - !ruby/regexp /^\/baz\// # Matches any URLs that start with /baz​ - -# the filename of the spider file to use. Default: spider.txt​ + +# A list of URLs to skip when spidering. +# Ruby regular expressions can be used, if prefixed with `!ruby/regexp` as defined in the YAML Cookbook​. +# See http://www.yaml.org/YAML_for_ruby.html#regexps +spider_skips: + - /foo/bar.html # Matches /foo/bar.html explicitly + - !ruby/regexp /^\/baz\// # Matches any URLs that start with /baz + +# the filename of the spider file to use. Default: spider.txt spider_file: example_com_spider.txt -​ -​# the number of days to keep the site spider file​ -spider_days: 10​ + +# the number of days to keep the site spider file +spider_days: 10 # amount of fuzz ImageMagick will use when comparing images. A higher fuzz makes the comparison less strict. fuzz: '20%' @@ -35,13 +35,7 @@ screen_widths: - 1280 # the engine to run Wraith with. -browser: - #phantomjs: "casperjs" # variant of PhantomJS that allows screenshots by element selector - phantomjs: "phantomjs" # PhantomJS, built on top of Webkit and JavascriptCore (like Safari) - #slimerjs: "slimerjs" # SlimerJS, built on top of Gecko and SpiderMonkey (like Firefox) - -# the file in charge of taking the screenshot -snap_file: "javascript/phantom.js" +browser: "phantomjs" # the directory that your latest screenshots will be stored in directory: 'shots_nojs' diff --git a/templates/javascript/beforeCapture--casper_example.js b/templates/javascript/beforeCapture--casper_example.js index 8380d69e..9f795541 100644 --- a/templates/javascript/beforeCapture--casper_example.js +++ b/templates/javascript/beforeCapture--casper_example.js @@ -1,5 +1,12 @@ -module.exports = function (browserEngine) { - browserEngine.wait(2000, function() { - browserEngine.click('.ns-panel__hotspot--2'); +module.exports = function (casper) { + + /* you may want to test some interaction on the page */ + casper.wait(2000, function() { + casper.click('.ns-panel__hotspot--2'); }); + + /* or you may want to see how your page looks without JavaScript */ + // casper.options.pageSettings.javascriptEnabled = false; + // casper.thenOpen(casper.page.url); + } \ No newline at end of file diff --git a/templates/javascript/beforeCapture--phantom_example.js b/templates/javascript/beforeCapture--phantom_example.js index addf1f39..fd292146 100644 --- a/templates/javascript/beforeCapture--phantom_example.js +++ b/templates/javascript/beforeCapture--phantom_example.js @@ -1,9 +1,16 @@ -module.exports = function (browserEngine) { - browserEngine.evaluate(function(){ +module.exports = function (phantom) { + + /* you may want to test some interaction on the page */ + phantom.evaluate(function(){ var a = document.querySelector('.ns-panel__hotspot--2'); var e = document.createEvent('MouseEvents'); e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); a.dispatchEvent(e); waitforload = true; }); + + /* or you may want to see how your page looks without JavaScript */ + // phantom.settings.javascriptEnabled = false; + // phantom.open(phantom.page.url); + } \ No newline at end of file From fcb75a69021893b5f63e43a13a1f375eddf05aa7 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 14:37:00 +0000 Subject: [PATCH 08/21] fix #319 --- lib/wraith/gallery.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/wraith/gallery.rb b/lib/wraith/gallery.rb index 8d31784d..032abc0f 100755 --- a/lib/wraith/gallery.rb +++ b/lib/wraith/gallery.rb @@ -147,7 +147,9 @@ def generate_gallery(with_path = "") generate_html(@location, directories, slideshow_template, dest, with_path) puts "Gallery generated" - check_failed_shots + failed = check_failed_shots + prompt_user_to_open_gallery dest + exit 1 if failed end def check_failed_shots @@ -167,12 +169,17 @@ def check_failed_shots end end - exit 1 + return false else true end end + def prompt_user_to_open_gallery(dest) + puts "\nView the gallery in your browser:" + puts "\t file://" + `pwd`.chomp + '/' + dest + end + class ErbBinding < OpenStruct def get_binding binding From c58dde3dc7642a54fed8bcebbcfcdfb48a898406 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 15:24:51 +0000 Subject: [PATCH 09/21] got the optional efficiency working in casper --- lib/wraith/javascript/_getDimensions.js | 25 ++++++++++++--- lib/wraith/javascript/casper.js | 42 +++++++++++++++++++++++-- lib/wraith/save_images.rb | 5 ++- templates/configs/component.yaml | 3 ++ 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/lib/wraith/javascript/_getDimensions.js b/lib/wraith/javascript/_getDimensions.js index 8571343c..d15c3634 100644 --- a/lib/wraith/javascript/_getDimensions.js +++ b/lib/wraith/javascript/_getDimensions.js @@ -1,7 +1,22 @@ module.exports = function (dimensions) { - dimensions = /(\d*)x?((\d*))?/i.exec(dimensions); - return { - 'viewportWidth': parseInt(dimensions[1]), - 'viewportHeight': parseInt(dimensions[2] || 1500) - }; + dimensions = '' + dimensions; // cast to string + + function getWidthAndHeight(dimensions) { + dimensions = /(\d*)x?((\d*))?/i.exec(dimensions); + return { + 'viewportWidth': parseInt(dimensions[1]), + 'viewportHeight': parseInt(dimensions[2] || 1500) + }; + } + + var multipleDimensions = dimensions.split(' '); + + if (multipleDimensions.length > 1) { + return multipleDimensions.map(function (dimensions) { + return getWidthAndHeight(dimensions); + }); + } + else { + return getWidthAndHeight(dimensions); + } } \ No newline at end of file diff --git a/lib/wraith/javascript/casper.js b/lib/wraith/javascript/casper.js index 77c8e0ae..dab3ad07 100644 --- a/lib/wraith/javascript/casper.js +++ b/lib/wraith/javascript/casper.js @@ -8,9 +8,14 @@ var url = casper.cli.get(0), image_name = casper.cli.get(2), selector = casper.cli.get(3), globalBeforeCaptureJS = casper.cli.get(4), - pathBeforeCaptureJS = casper.cli.get(5); + pathBeforeCaptureJS = casper.cli.get(5), + dimensionsProcessed = 0, + currentDimensions; // functions +function takingMultipleScreenshots() { + return dimensions.length && dimensions.length > 1; +} function requireRelative(file) { // PhantomJS will automatically `require` relatively, but CasperJS needs some extra help. Hence this function. // 'templates/javascript/casper.js' -> 'templates/javascript' @@ -27,13 +32,44 @@ function snap() { else { this.captureSelector(image_name, selector); } - console.log('Snapping ' + url + ' at: ' + dimensions.viewportWidth + 'x' + dimensions.viewportHeight); + console.log('Snapping ' + url + ' at: ' + currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight); + + dimensionsProcessed++; + if (takingMultipleScreenshots() && dimensionsProcessed < dimensions.length) { + currentDimensions = dimensions[dimensionsProcessed]; + image_name = replaceImageNameWithDimensions(image_name, currentDimensions); + casper.viewport(currentDimensions.viewportWidth, currentDimensions.viewportHeight); + casper.wait(300, function then () { + snap.bind(this)(); + }); + } +} +function replaceImageNameWithDimensions(image_name, currentDimensions) { + // shots/clickable_guide__after_click/600x768_casperjs_english.png + // -> + // shots/clickable_guide__after_click/1024_casperjs_english.png + var dirs = image_name.split('/'), + filename = dirs[dirs.length - 1], + filenameParts = filename.split('_'), + newFilename; + + filenameParts[0] = currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight; + dirs.pop(); // remove 600x768_casperjs_english.png + newFilename = dirs.join('/') + '/' + filenameParts.join('_'); + return newFilename; +} + +if (takingMultipleScreenshots()) { + currentDimensions = dimensions[0]; +} +else { + currentDimensions = dimensions; } // Casper can now do its magic casper.start(); casper.open(url); -casper.viewport(dimensions.viewportWidth, dimensions.viewportHeight); +casper.viewport(currentDimensions.viewportWidth, currentDimensions.viewportHeight); casper.then(function() { if (globalBeforeCaptureJS) { require('./' + globalBeforeCaptureJS)(this); diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index c123cee8..641fa999 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -37,7 +37,10 @@ def save_images end def capture_page_image(browser, url, width, file_name, selector, global_before_capture, path_before_capture) - command = "#{browser} #{wraith.phantomjs_options} '#{wraith.snap_file}' '#{url}' '#{width}' '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}'" + + width = "320 464x900 768" # temporarily mocking the array passing behaviour in shell + + command = "#{browser} #{wraith.phantomjs_options} '#{wraith.snap_file}' '#{url}' \"'#{width}'\" '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}'" # @TODO - uncomment the following line when we add a verbose mode # puts command diff --git a/templates/configs/component.yaml b/templates/configs/component.yaml index 240fff1c..ed3da43e 100644 --- a/templates/configs/component.yaml +++ b/templates/configs/component.yaml @@ -34,6 +34,9 @@ screen_widths: - 1024 - 1280 +# resize to each screen width, or reload at each screen width. (The former is more efficient). +resize_or_reload: 'resize' + # the engine to run Wraith with. browser: "casperjs" From 914c9c815ac76c3443e3d38bef9f087e31cb994b Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 16:20:23 +0000 Subject: [PATCH 10/21] made more progress --- lib/wraith/javascript/_getDimensions.js | 2 +- lib/wraith/javascript/casper.js | 10 +++--- lib/wraith/save_images.rb | 42 +++++++++++++++++++------ lib/wraith/wraith.rb | 4 +++ 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/lib/wraith/javascript/_getDimensions.js b/lib/wraith/javascript/_getDimensions.js index d15c3634..1d171af4 100644 --- a/lib/wraith/javascript/_getDimensions.js +++ b/lib/wraith/javascript/_getDimensions.js @@ -9,7 +9,7 @@ module.exports = function (dimensions) { }; } - var multipleDimensions = dimensions.split(' '); + var multipleDimensions = dimensions.split(','); if (multipleDimensions.length > 1) { return multipleDimensions.map(function (dimensions) { diff --git a/lib/wraith/javascript/casper.js b/lib/wraith/javascript/casper.js index dab3ad07..0d52925d 100644 --- a/lib/wraith/javascript/casper.js +++ b/lib/wraith/javascript/casper.js @@ -26,13 +26,14 @@ function requireRelative(file) { return require(currentFilePath + '/' + file); } function snap() { + console.log('Snapping ' + url + ' at: ' + currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight); + if (!selector) { this.capture(image_name); } else { this.captureSelector(image_name, selector); } - console.log('Snapping ' + url + ' at: ' + currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight); dimensionsProcessed++; if (takingMultipleScreenshots() && dimensionsProcessed < dimensions.length) { @@ -45,22 +46,23 @@ function snap() { } } function replaceImageNameWithDimensions(image_name, currentDimensions) { - // shots/clickable_guide__after_click/600x768_casperjs_english.png + // shots/clickable_guide__after_click/MULTI_casperjs_english.png // -> - // shots/clickable_guide__after_click/1024_casperjs_english.png + // shots/clickable_guide__after_click/1024x359_casperjs_english.png var dirs = image_name.split('/'), filename = dirs[dirs.length - 1], filenameParts = filename.split('_'), newFilename; filenameParts[0] = currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight; - dirs.pop(); // remove 600x768_casperjs_english.png + dirs.pop(); // remove MULTI_casperjs_english.png newFilename = dirs.join('/') + '/' + filenameParts.join('_'); return newFilename; } if (takingMultipleScreenshots()) { currentDimensions = dimensions[0]; + image_name = replaceImageNameWithDimensions(image_name, currentDimensions); } else { currentDimensions = dimensions; diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index 641fa999..7c153164 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -25,25 +25,40 @@ def save_images check_paths.each do |label, options| settings = CaptureOptions.new(options, wraith) - wraith.widths.each do |width| - base_file_name = meta.file_names(width, label, meta.base_label) - compare_file_name = meta.file_names(width, label, meta.compare_label) - - jobs << [label, settings.path, width, settings.base_url, base_file_name, settings.selector, wraith.before_capture, settings.before_capture] - jobs << [label, settings.path, width, settings.compare_url, compare_file_name, settings.selector, wraith.before_capture, settings.before_capture] unless settings.compare_url.nil? + if wraith.resize + jobs = define_jobs(label, settings, wraith.widths) + else + jobs = [] + wraith.widths.each do |width| + jobs = jobs + define_jobs(label, settings, width) + end end end parallel_task(jobs) end - def capture_page_image(browser, url, width, file_name, selector, global_before_capture, path_before_capture) + def define_jobs(label, settings, width) + base_file_name = meta.file_names(width, label, meta.base_label) + compare_file_name = meta.file_names(width, label, meta.compare_label) + + if width.kind_of? Array + # prepare for the command line + width = width.map{ |i| "'#{i}'" }.join(',') + end + + jobs = [] + jobs << [label, settings.path, width, settings.base_url, base_file_name, settings.selector, wraith.before_capture, settings.before_capture] + jobs << [label, settings.path, width, settings.compare_url, compare_file_name, settings.selector, wraith.before_capture, settings.before_capture] unless settings.compare_url.nil? - width = "320 464x900 768" # temporarily mocking the array passing behaviour in shell + jobs + end + + def capture_page_image(browser, url, width, file_name, selector, global_before_capture, path_before_capture) - command = "#{browser} #{wraith.phantomjs_options} '#{wraith.snap_file}' '#{url}' \"'#{width}'\" '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}'" + command = "#{browser} #{wraith.phantomjs_options} '#{wraith.snap_file}' '#{url}' \"#{width}\" '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}'" # @TODO - uncomment the following line when we add a verbose mode - # puts command + puts command run_command command end @@ -71,6 +86,10 @@ def attempt_image_capture(width, url, filename, selector, global_before_capture, max_attempts.times do |i| capture_page_image meta.engine, url, width, filename, selector, global_before_capture, path_before_capture + if wraith.resize + return # @TODO - need to check if the image was generated, as per the non-efficient example below + end + return if File.exist? filename puts "Failed to capture image #{filename} on attempt number #{i + 1} of #{max_attempts}" @@ -146,6 +165,9 @@ def history_label end def file_names(width, label, domain_label) + if width.kind_of? Array + width = 'MULTI' + end "#{wraith.directory}/#{label}/#{width}_#{engine}_#{domain_label}.png" end diff --git a/lib/wraith/wraith.rb b/lib/wraith/wraith.rb index 5413448f..f2ffa2b0 100644 --- a/lib/wraith/wraith.rb +++ b/lib/wraith/wraith.rb @@ -62,6 +62,10 @@ def widths @config["screen_widths"] end + def resize + @config["resize_or_reload"] ? (@config["resize_or_reload"] == "resize") : true + end + def domains @config["domains"] end From f27799ec0d07d3cecb83f348372bed751ea53ced Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 16:32:02 +0000 Subject: [PATCH 11/21] bug fixes --- lib/wraith/save_images.rb | 5 ++--- lib/wraith/wraith.rb | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index 7c153164..3fcc44b4 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -26,9 +26,8 @@ def save_images settings = CaptureOptions.new(options, wraith) if wraith.resize - jobs = define_jobs(label, settings, wraith.widths) + jobs = jobs + define_jobs(label, settings, wraith.widths) else - jobs = [] wraith.widths.each do |width| jobs = jobs + define_jobs(label, settings, width) end @@ -58,7 +57,7 @@ def capture_page_image(browser, url, width, file_name, selector, global_before_c command = "#{browser} #{wraith.phantomjs_options} '#{wraith.snap_file}' '#{url}' \"#{width}\" '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}'" # @TODO - uncomment the following line when we add a verbose mode - puts command + #puts command run_command command end diff --git a/lib/wraith/wraith.rb b/lib/wraith/wraith.rb index f2ffa2b0..dd767104 100644 --- a/lib/wraith/wraith.rb +++ b/lib/wraith/wraith.rb @@ -63,7 +63,8 @@ def widths end def resize - @config["resize_or_reload"] ? (@config["resize_or_reload"] == "resize") : true + # @TODO make this default to true, once it's been tested a bit more thoroughly + @config["resize_or_reload"] ? (@config["resize_or_reload"] == "resize") : false end def domains From 8a639114cddb7979c62c858784c74c8720252d03 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 16:51:19 +0000 Subject: [PATCH 12/21] set up efficient mode on phantom --- lib/wraith/javascript/_getDimensions.js | 3 +- lib/wraith/javascript/_phantom__common.js | 104 +++++++++++++--------- 2 files changed, 66 insertions(+), 41 deletions(-) diff --git a/lib/wraith/javascript/_getDimensions.js b/lib/wraith/javascript/_getDimensions.js index 1d171af4..589ceec9 100644 --- a/lib/wraith/javascript/_getDimensions.js +++ b/lib/wraith/javascript/_getDimensions.js @@ -1,5 +1,6 @@ module.exports = function (dimensions) { - dimensions = '' + dimensions; // cast to string + // remove quotes from dimensions string + dimensions = dimensions.replace(/'/gi, ''); function getWidthAndHeight(dimensions) { dimensions = /(\d*)x?((\d*))?/i.exec(dimensions); diff --git a/lib/wraith/javascript/_phantom__common.js b/lib/wraith/javascript/_phantom__common.js index b4074ae1..c83d40f8 100644 --- a/lib/wraith/javascript/_phantom__common.js +++ b/lib/wraith/javascript/_phantom__common.js @@ -13,7 +13,9 @@ module.exports = function (config) { image_name = systemArgs[3], selector = systemArgs[4], globalBeforeCaptureJS = systemArgs[5], - pathBeforeCaptureJS = systemArgs[6]; + pathBeforeCaptureJS = systemArgs[6], + dimensionsProcessed = 0, + currentDimensions; globalBeforeCaptureJS = globalBeforeCaptureJS === 'false' ? false : globalBeforeCaptureJS; pathBeforeCaptureJS = pathBeforeCaptureJS === 'false' ? false : pathBeforeCaptureJS; @@ -22,7 +24,15 @@ module.exports = function (config) { var last_request_timeout; var final_timeout; - page.viewportSize = { width: dimensions.viewportWidth, height: dimensions.viewportHeight}; + if (takingMultipleScreenshots()) { + currentDimensions = dimensions[0]; + image_name = replaceImageNameWithDimensions(image_name, currentDimensions); + } + else { + currentDimensions = dimensions; + } + + page.viewportSize = { width: currentDimensions.viewportWidth, height: currentDimensions.viewportHeight}; page.settings = { loadImages: true, javascriptEnabled: javascriptEnabled }; // If you want to use additional phantomjs commands, place them here @@ -66,6 +76,56 @@ module.exports = function (config) { } }); + function takingMultipleScreenshots() { + return dimensions.length && dimensions.length > 1; + } + + function replaceImageNameWithDimensions(image_name, currentDimensions) { + // shots/clickable_guide__after_click/MULTI_casperjs_english.png + // -> + // shots/clickable_guide__after_click/1024x359_casperjs_english.png + var dirs = image_name.split('/'), + filename = dirs[dirs.length - 1], + filenameParts = filename.split('_'), + newFilename; + + filenameParts[0] = currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight; + dirs.pop(); // remove MULTI_casperjs_english.png + newFilename = dirs.join('/') + '/' + filenameParts.join('_'); + return newFilename; + } + + function runSetupJavaScriptThenCaptureImage() { + if (globalBeforeCaptureJS) { + require(globalBeforeCaptureJS)(page); + } + if (pathBeforeCaptureJS) { + require(pathBeforeCaptureJS)(page); + } + captureImage(); + } + + function captureImage() { + + console.log('Snapping ' + url + ' at: ' + currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight); + page.clipRect = { + top: 0, + left: 0, + height: currentDimensions.viewportHeight, + width: currentDimensions.viewportWidth + }; + page.render(image_name); + + dimensionsProcessed++; + if (takingMultipleScreenshots() && dimensionsProcessed < dimensions.length) { + currentDimensions = dimensions[dimensionsProcessed]; + image_name = replaceImageNameWithDimensions(image_name, currentDimensions); + setTimeout(captureImage, 1000); + } + else { + phantom.exit(); + } + } function debounced_render() { clearTimeout(last_request_timeout); @@ -75,48 +135,12 @@ module.exports = function (config) { // rendering, just in case the page kicks off another request if (current_requests < 1) { clearTimeout(final_timeout); - last_request_timeout = setTimeout(function() { - - if (globalBeforeCaptureJS) { - require(globalBeforeCaptureJS)(page); - } - if (pathBeforeCaptureJS) { - require(pathBeforeCaptureJS)(page); - } - - console.log('Snapping ' + url + ' at: ' + dimensions.viewportWidth + 'x' + dimensions.viewportHeight); - page.clipRect = { - top: 0, - left: 0, - height: dimensions.viewportHeight, - width: dimensions.viewportWidth - }; - page.render(image_name); - phantom.exit(); - }, 1000); + last_request_timeout = setTimeout(runSetupJavaScriptThenCaptureImage, 1000); } // Sometimes, straggling requests never make it back, in which // case, timeout after 5 seconds and render the page anyway - final_timeout = setTimeout(function() { - - if (globalBeforeCaptureJS) { - require(globalBeforeCaptureJS)(page); - } - if (pathBeforeCaptureJS) { - require(pathBeforeCaptureJS)(page); - } - - console.log('Snapping ' + url + ' at: ' + dimensions.viewportWidth + 'x' + dimensions.viewportHeight); - page.clipRect = { - top: 0, - left: 0, - height: dimensions.viewportHeight, - width: dimensions.viewportWidth - }; - page.render(image_name); - phantom.exit(); - }, 5000); + final_timeout = setTimeout(runSetupJavaScriptThenCaptureImage, 5000); } } \ No newline at end of file From b06fb9aa570fc7b90e1deed091df3b0a31d3cf90 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 16:59:59 +0000 Subject: [PATCH 13/21] reduced duplication between casperjs and phantomjs snap files --- lib/wraith/javascript/_getDimensions.js | 23 -------- lib/wraith/javascript/_helper.js | 44 ++++++++++++++++ lib/wraith/javascript/_phantom__common.js | 52 +++---------------- lib/wraith/javascript/casper.js | 30 +++-------- .../beforeCapture--phantom_example.js | 20 +++++++ 5 files changed, 78 insertions(+), 91 deletions(-) delete mode 100644 lib/wraith/javascript/_getDimensions.js create mode 100644 lib/wraith/javascript/_helper.js diff --git a/lib/wraith/javascript/_getDimensions.js b/lib/wraith/javascript/_getDimensions.js deleted file mode 100644 index 589ceec9..00000000 --- a/lib/wraith/javascript/_getDimensions.js +++ /dev/null @@ -1,23 +0,0 @@ -module.exports = function (dimensions) { - // remove quotes from dimensions string - dimensions = dimensions.replace(/'/gi, ''); - - function getWidthAndHeight(dimensions) { - dimensions = /(\d*)x?((\d*))?/i.exec(dimensions); - return { - 'viewportWidth': parseInt(dimensions[1]), - 'viewportHeight': parseInt(dimensions[2] || 1500) - }; - } - - var multipleDimensions = dimensions.split(','); - - if (multipleDimensions.length > 1) { - return multipleDimensions.map(function (dimensions) { - return getWidthAndHeight(dimensions); - }); - } - else { - return getWidthAndHeight(dimensions); - } -} \ No newline at end of file diff --git a/lib/wraith/javascript/_helper.js b/lib/wraith/javascript/_helper.js new file mode 100644 index 00000000..3187a59f --- /dev/null +++ b/lib/wraith/javascript/_helper.js @@ -0,0 +1,44 @@ +module.exports = function (dimensions) { + // remove quotes from dimensions string + dimensions = dimensions.replace(/'/gi, ''); + + function getWidthAndHeight(dimensions) { + dimensions = /(\d*)x?((\d*))?/i.exec(dimensions); + return { + 'viewportWidth': parseInt(dimensions[1]), + 'viewportHeight': parseInt(dimensions[2] || 1500) + }; + } + + var multipleDimensions = dimensions.split(','); + + if (multipleDimensions.length > 1) { + dimensions = multipleDimensions.map(function (dimensions) { + return getWidthAndHeight(dimensions); + }); + } + else { + dimensions = getWidthAndHeight(dimensions); + } + + return { + dimensions: dimensions, + takingMultipleScreenshots: function () { + return dimensions.length && dimensions.length > 1; + }, + replaceImageNameWithDimensions: function (image_name, currentDimensions) { + // shots/clickable_guide__after_click/MULTI_casperjs_english.png + // -> + // shots/clickable_guide__after_click/1024x359_casperjs_english.png + var dirs = image_name.split('/'), + filename = dirs[dirs.length - 1], + filenameParts = filename.split('_'), + newFilename; + + filenameParts[0] = currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight; + dirs.pop(); // remove MULTI_casperjs_english.png + newFilename = dirs.join('/') + '/' + filenameParts.join('_'); + return newFilename; + } + } +} \ No newline at end of file diff --git a/lib/wraith/javascript/_phantom__common.js b/lib/wraith/javascript/_phantom__common.js index c83d40f8..0672484e 100644 --- a/lib/wraith/javascript/_phantom__common.js +++ b/lib/wraith/javascript/_phantom__common.js @@ -1,7 +1,8 @@ module.exports = function (config) { // modules - var page = require('webpage').create(); + var page = require('webpage').create(), + helper = require('./_helper.js')(systemArgs[2]); // config var systemArgs = config.systemArgs, @@ -9,7 +10,7 @@ module.exports = function (config) { // command line arguments var url = systemArgs[1], - dimensions = require('./_getDimensions.js')(systemArgs[2]), + dimensions = helper.dimensions, image_name = systemArgs[3], selector = systemArgs[4], globalBeforeCaptureJS = systemArgs[5], @@ -24,9 +25,9 @@ module.exports = function (config) { var last_request_timeout; var final_timeout; - if (takingMultipleScreenshots()) { + if (helper.takingMultipleScreenshots()) { currentDimensions = dimensions[0]; - image_name = replaceImageNameWithDimensions(image_name, currentDimensions); + image_name = helper.replaceImageNameWithDimensions(image_name, currentDimensions); } else { currentDimensions = dimensions; @@ -38,26 +39,6 @@ module.exports = function (config) { // If you want to use additional phantomjs commands, place them here page.settings.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.17'; - // You can place custom headers here, example below. - // page.customHeaders = { - - // 'X-Candy-OVERRIDE': 'https://api.live.bbc.co.uk/' - - // }; - - // If you want to set a cookie, just add your details below in the following way. - - // phantom.addCookie({ - // 'name': 'ckns_policy', - // 'value': '111', - // 'domain': '.bbc.co.uk' - // }); - // phantom.addCookie({ - // 'name': 'locserv', - // 'value': '1#l1#i=6691484:n=Oxford+Circus:h=e@w1#i=8:p=London@d1#1=l:2=e:3=e:4=2@n1#r=40', - // 'domain': '.bbc.co.uk' - // }); - page.onResourceRequested = function(req) { current_requests += 1; }; @@ -76,25 +57,6 @@ module.exports = function (config) { } }); - function takingMultipleScreenshots() { - return dimensions.length && dimensions.length > 1; - } - - function replaceImageNameWithDimensions(image_name, currentDimensions) { - // shots/clickable_guide__after_click/MULTI_casperjs_english.png - // -> - // shots/clickable_guide__after_click/1024x359_casperjs_english.png - var dirs = image_name.split('/'), - filename = dirs[dirs.length - 1], - filenameParts = filename.split('_'), - newFilename; - - filenameParts[0] = currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight; - dirs.pop(); // remove MULTI_casperjs_english.png - newFilename = dirs.join('/') + '/' + filenameParts.join('_'); - return newFilename; - } - function runSetupJavaScriptThenCaptureImage() { if (globalBeforeCaptureJS) { require(globalBeforeCaptureJS)(page); @@ -117,9 +79,9 @@ module.exports = function (config) { page.render(image_name); dimensionsProcessed++; - if (takingMultipleScreenshots() && dimensionsProcessed < dimensions.length) { + if (helper.takingMultipleScreenshots() && dimensionsProcessed < dimensions.length) { currentDimensions = dimensions[dimensionsProcessed]; - image_name = replaceImageNameWithDimensions(image_name, currentDimensions); + image_name = helper.replaceImageNameWithDimensions(image_name, currentDimensions); setTimeout(captureImage, 1000); } else { diff --git a/lib/wraith/javascript/casper.js b/lib/wraith/javascript/casper.js index 0d52925d..bd55d4d6 100644 --- a/lib/wraith/javascript/casper.js +++ b/lib/wraith/javascript/casper.js @@ -1,10 +1,11 @@ // modules var system = require('system'), - casper = require('casper').create(); + casper = require('casper').create(), + helper = requireRelative('_helper.js')(casper.cli.get(1)); // command line arguments var url = casper.cli.get(0), - dimensions = requireRelative('_getDimensions.js')(casper.cli.get(1)), + dimensions = helper.dimensions, image_name = casper.cli.get(2), selector = casper.cli.get(3), globalBeforeCaptureJS = casper.cli.get(4), @@ -13,9 +14,6 @@ var url = casper.cli.get(0), currentDimensions; // functions -function takingMultipleScreenshots() { - return dimensions.length && dimensions.length > 1; -} function requireRelative(file) { // PhantomJS will automatically `require` relatively, but CasperJS needs some extra help. Hence this function. // 'templates/javascript/casper.js' -> 'templates/javascript' @@ -36,33 +34,19 @@ function snap() { } dimensionsProcessed++; - if (takingMultipleScreenshots() && dimensionsProcessed < dimensions.length) { + if (helper.takingMultipleScreenshots() && dimensionsProcessed < dimensions.length) { currentDimensions = dimensions[dimensionsProcessed]; - image_name = replaceImageNameWithDimensions(image_name, currentDimensions); + image_name = helper.replaceImageNameWithDimensions(image_name, currentDimensions); casper.viewport(currentDimensions.viewportWidth, currentDimensions.viewportHeight); casper.wait(300, function then () { snap.bind(this)(); }); } } -function replaceImageNameWithDimensions(image_name, currentDimensions) { - // shots/clickable_guide__after_click/MULTI_casperjs_english.png - // -> - // shots/clickable_guide__after_click/1024x359_casperjs_english.png - var dirs = image_name.split('/'), - filename = dirs[dirs.length - 1], - filenameParts = filename.split('_'), - newFilename; - - filenameParts[0] = currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight; - dirs.pop(); // remove MULTI_casperjs_english.png - newFilename = dirs.join('/') + '/' + filenameParts.join('_'); - return newFilename; -} -if (takingMultipleScreenshots()) { +if (helper.takingMultipleScreenshots()) { currentDimensions = dimensions[0]; - image_name = replaceImageNameWithDimensions(image_name, currentDimensions); + image_name = helper.replaceImageNameWithDimensions(image_name, currentDimensions); } else { currentDimensions = dimensions; diff --git a/templates/javascript/beforeCapture--phantom_example.js b/templates/javascript/beforeCapture--phantom_example.js index fd292146..cc1dfa4e 100644 --- a/templates/javascript/beforeCapture--phantom_example.js +++ b/templates/javascript/beforeCapture--phantom_example.js @@ -13,4 +13,24 @@ module.exports = function (phantom) { // phantom.settings.javascriptEnabled = false; // phantom.open(phantom.page.url); + // You can place custom headers here, example below. + // page.customHeaders = { + + // 'X-Candy-OVERRIDE': 'https://api.live.bbc.co.uk/' + + // }; + + // If you want to set a cookie, just add your details below in the following way. + + // phantom.addCookie({ + // 'name': 'ckns_policy', + // 'value': '111', + // 'domain': '.bbc.co.uk' + // }); + // phantom.addCookie({ + // 'name': 'locserv', + // 'value': '1#l1#i=6691484:n=Oxford+Circus:h=e@w1#i=8:p=London@d1#1=l:2=e:3=e:4=2@n1#r=40', + // 'domain': '.bbc.co.uk' + // }); + } \ No newline at end of file From 931d357bc0175d2b7e85b8f760d7d7963ccec782 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 17:10:11 +0000 Subject: [PATCH 14/21] removed comment, created https://github.com/BBC-News/wraith/issues/336 instead --- lib/wraith/javascript/phantom--nojs.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/wraith/javascript/phantom--nojs.js b/lib/wraith/javascript/phantom--nojs.js index d5b280e9..95c75aac 100644 --- a/lib/wraith/javascript/phantom--nojs.js +++ b/lib/wraith/javascript/phantom--nojs.js @@ -1,6 +1,5 @@ var system = require('system'); -// @TODO - make a javascript_disabled property in YAML, and pass via command line require('./_phantom__common.js')({ systemArgs: system.args, javascriptEnabled: false From 70170a08edfb507d33ea48e07eab4f40d39e01fa Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 17:18:54 +0000 Subject: [PATCH 15/21] fixed the tests --- lib/wraith/javascript/_phantom__common.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/wraith/javascript/_phantom__common.js b/lib/wraith/javascript/_phantom__common.js index 0672484e..5ddd715f 100644 --- a/lib/wraith/javascript/_phantom__common.js +++ b/lib/wraith/javascript/_phantom__common.js @@ -1,13 +1,13 @@ module.exports = function (config) { - // modules - var page = require('webpage').create(), - helper = require('./_helper.js')(systemArgs[2]); - // config var systemArgs = config.systemArgs, javascriptEnabled = config.javascriptEnabled; + // modules + var page = require('webpage').create(), + helper = require('./_helper.js')(systemArgs[2]); + // command line arguments var url = systemArgs[1], dimensions = helper.dimensions, From c86c63cdeeadd854cc563c0c02f256bcbeecdd02 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 17:24:49 +0000 Subject: [PATCH 16/21] added some rudimentary tests --- lib/wraith/save_images.rb | 2 +- spec/config_spec.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index 3fcc44b4..bc6ff88f 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -41,7 +41,7 @@ def define_jobs(label, settings, width) compare_file_name = meta.file_names(width, label, meta.compare_label) if width.kind_of? Array - # prepare for the command line + # prepare for the command line. [30,40,50] => "'30','40','50'" width = width.map{ |i| "'#{i}'" }.join(',') end diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 389945b8..c8793b37 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -120,4 +120,20 @@ end end + describe "different modes of efficiency (resize or reload)" do + + it "should trigger efficient mode if resize was specified" do + config = 'resize_or_reload: "resize"' + wraith = Wraith::Wraith.new(config, true) + expect(wraith.resize) + end + + it "should fall back to slow mode if reload was specified" do + config = 'resize_or_reload: "reload"' + wraith = Wraith::Wraith.new(config, true) + expect(wraith.resize).to eq false + end + + end + end From 0dabbfd76e23b708c48313d13e9f4aae54712b2c Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 17:31:31 +0000 Subject: [PATCH 17/21] explicitly pass dimensions object to helper --- lib/wraith/javascript/_helper.js | 2 +- lib/wraith/javascript/_phantom__common.js | 4 ++-- lib/wraith/javascript/casper.js | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/wraith/javascript/_helper.js b/lib/wraith/javascript/_helper.js index 3187a59f..8359c2d1 100644 --- a/lib/wraith/javascript/_helper.js +++ b/lib/wraith/javascript/_helper.js @@ -23,7 +23,7 @@ module.exports = function (dimensions) { return { dimensions: dimensions, - takingMultipleScreenshots: function () { + takingMultipleScreenshots: function (dimensions) { return dimensions.length && dimensions.length > 1; }, replaceImageNameWithDimensions: function (image_name, currentDimensions) { diff --git a/lib/wraith/javascript/_phantom__common.js b/lib/wraith/javascript/_phantom__common.js index 5ddd715f..47edd8eb 100644 --- a/lib/wraith/javascript/_phantom__common.js +++ b/lib/wraith/javascript/_phantom__common.js @@ -25,7 +25,7 @@ module.exports = function (config) { var last_request_timeout; var final_timeout; - if (helper.takingMultipleScreenshots()) { + if (helper.takingMultipleScreenshots(dimensions)) { currentDimensions = dimensions[0]; image_name = helper.replaceImageNameWithDimensions(image_name, currentDimensions); } @@ -79,7 +79,7 @@ module.exports = function (config) { page.render(image_name); dimensionsProcessed++; - if (helper.takingMultipleScreenshots() && dimensionsProcessed < dimensions.length) { + if (helper.takingMultipleScreenshots(dimensions) && dimensionsProcessed < dimensions.length) { currentDimensions = dimensions[dimensionsProcessed]; image_name = helper.replaceImageNameWithDimensions(image_name, currentDimensions); setTimeout(captureImage, 1000); diff --git a/lib/wraith/javascript/casper.js b/lib/wraith/javascript/casper.js index bd55d4d6..cdafb7ca 100644 --- a/lib/wraith/javascript/casper.js +++ b/lib/wraith/javascript/casper.js @@ -34,7 +34,7 @@ function snap() { } dimensionsProcessed++; - if (helper.takingMultipleScreenshots() && dimensionsProcessed < dimensions.length) { + if (helper.takingMultipleScreenshots(dimensions) && dimensionsProcessed < dimensions.length) { currentDimensions = dimensions[dimensionsProcessed]; image_name = helper.replaceImageNameWithDimensions(image_name, currentDimensions); casper.viewport(currentDimensions.viewportWidth, currentDimensions.viewportHeight); @@ -44,7 +44,7 @@ function snap() { } } -if (helper.takingMultipleScreenshots()) { +if (helper.takingMultipleScreenshots(dimensions)) { currentDimensions = dimensions[0]; image_name = helper.replaceImageNameWithDimensions(image_name, currentDimensions); } From 0b3a55ec35311bcb76c1d2d80cfc85ec012c4faf Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Mon, 30 Nov 2015 17:45:11 +0000 Subject: [PATCH 18/21] keeping scopes absolutely separate --- lib/wraith/javascript/_helper.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/wraith/javascript/_helper.js b/lib/wraith/javascript/_helper.js index 8359c2d1..377bb0d4 100644 --- a/lib/wraith/javascript/_helper.js +++ b/lib/wraith/javascript/_helper.js @@ -1,6 +1,6 @@ -module.exports = function (dimensions) { +module.exports = function (commandLineDimensions) { // remove quotes from dimensions string - dimensions = dimensions.replace(/'/gi, ''); + commandLineDimensions = commandLineDimensions.replace(/'/gi, ''); function getWidthAndHeight(dimensions) { dimensions = /(\d*)x?((\d*))?/i.exec(dimensions); @@ -10,19 +10,20 @@ module.exports = function (dimensions) { }; } - var multipleDimensions = dimensions.split(','); + var multipleDimensions = commandLineDimensions.split(','), + dimensionsToPass; if (multipleDimensions.length > 1) { - dimensions = multipleDimensions.map(function (dimensions) { - return getWidthAndHeight(dimensions); + dimensionsToPass = multipleDimensions.map(function (cliDimensions) { + return getWidthAndHeight(cliDimensions); }); } else { - dimensions = getWidthAndHeight(dimensions); + dimensionsToPass = getWidthAndHeight(commandLineDimensions); } return { - dimensions: dimensions, + dimensions: dimensionsToPass, takingMultipleScreenshots: function (dimensions) { return dimensions.length && dimensions.length > 1; }, From 5650dd1d4198335f90530bc2c9b6253337f2e968 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Tue, 1 Dec 2015 11:11:33 +0000 Subject: [PATCH 19/21] attempting fix found in https://github.com/n1k0/casperjs/issues/1068 --- lib/wraith/javascript/_phantom__common.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/wraith/javascript/_phantom__common.js b/lib/wraith/javascript/_phantom__common.js index 47edd8eb..6dc6299b 100644 --- a/lib/wraith/javascript/_phantom__common.js +++ b/lib/wraith/javascript/_phantom__common.js @@ -85,7 +85,11 @@ module.exports = function (config) { setTimeout(captureImage, 1000); } else { - phantom.exit(); + // prevent CI from failing from 'Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL' errors + // see https://github.com/n1k0/casperjs/issues/1068 + setTimeout(function(){ + phantom.exit(); + }, 10); } } From 6cc609db2901e3f06897c74f617486c99ac83520 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Tue, 1 Dec 2015 15:03:04 +0000 Subject: [PATCH 20/21] expanded efficient/inefficient mode tests --- lib/wraith/save_images.rb | 34 +++++++++++++++--------- lib/wraith/wraith.rb | 8 +++--- spec/config_spec.rb | 16 +++++------ spec/save_images_spec.rb | 56 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 25 deletions(-) create mode 100644 spec/save_images_spec.rb diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index bc6ff88f..687c18ff 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -5,8 +5,8 @@ class Wraith::SaveImages attr_reader :wraith, :history, :meta - def initialize(config, history = false) - @wraith = Wraith::Wraith.new(config) + def initialize(config, history = false, yaml_passed = false) + @wraith = Wraith::Wraith.new(config, yaml_passed) @history = history @meta = SaveMetadata.new(@wraith, history) end @@ -21,35 +21,43 @@ def check_paths end def save_images + jobs = define_jobs + parallel_task(jobs) + end + + def define_jobs jobs = [] check_paths.each do |label, options| settings = CaptureOptions.new(options, wraith) if wraith.resize - jobs = jobs + define_jobs(label, settings, wraith.widths) + jobs = jobs + define_individual_job(label, settings, wraith.widths) else wraith.widths.each do |width| - jobs = jobs + define_jobs(label, settings, width) + jobs = jobs + define_individual_job(label, settings, width) end end end - parallel_task(jobs) + jobs end - def define_jobs(label, settings, width) + def define_individual_job(label, settings, width) base_file_name = meta.file_names(width, label, meta.base_label) compare_file_name = meta.file_names(width, label, meta.compare_label) + jobs = [] + jobs << [label, settings.path, prepare_widths_for_cli(width), settings.base_url, base_file_name, settings.selector, wraith.before_capture, settings.before_capture] + jobs << [label, settings.path, prepare_widths_for_cli(width), settings.compare_url, compare_file_name, settings.selector, wraith.before_capture, settings.before_capture] unless settings.compare_url.nil? + + jobs + end + + def prepare_widths_for_cli(width) if width.kind_of? Array # prepare for the command line. [30,40,50] => "'30','40','50'" width = width.map{ |i| "'#{i}'" }.join(',') end - - jobs = [] - jobs << [label, settings.path, width, settings.base_url, base_file_name, settings.selector, wraith.before_capture, settings.before_capture] - jobs << [label, settings.path, width, settings.compare_url, compare_file_name, settings.selector, wraith.before_capture, settings.before_capture] unless settings.compare_url.nil? - - jobs + width end def capture_page_image(browser, url, width, file_name, selector, global_before_capture, path_before_capture) @@ -86,7 +94,7 @@ def attempt_image_capture(width, url, filename, selector, global_before_capture, capture_page_image meta.engine, url, width, filename, selector, global_before_capture, path_before_capture if wraith.resize - return # @TODO - need to check if the image was generated, as per the non-efficient example below + return # @TODO - need to check if the image was generated, as per the reload example below end return if File.exist? filename diff --git a/lib/wraith/wraith.rb b/lib/wraith/wraith.rb index dd767104..d85bc67b 100644 --- a/lib/wraith/wraith.rb +++ b/lib/wraith/wraith.rb @@ -4,8 +4,7 @@ class Wraith::Wraith attr_accessor :config def initialize(config, yaml_passed = false) - yaml_string = yaml_passed ? config : open_config_file(config) - @config = YAML.load yaml_string + @config = yaml_passed ? config : open_config_file(config) rescue puts "unable to find config at #{config}" exit 1 @@ -13,10 +12,11 @@ def initialize(config, yaml_passed = false) def open_config_file(config_name) if File.exist?(config_name) && File.extname(config_name) == ".yaml" - File.open config_name + config = File.open config_name else - File.open "configs/#{config_name}.yaml" + config = File.open "configs/#{config_name}.yaml" end + YAML.load config end def directory diff --git a/spec/config_spec.rb b/spec/config_spec.rb index c8793b37..449ce457 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -73,14 +73,14 @@ describe "different ways of initialising browser engine" do it "should let us directly specify the engine" do - config = 'browser: phantomjs' + config = YAML.load 'browser: phantomjs' wraith = Wraith::Wraith.new(config, true) expect(wraith.engine).to eq 'phantomjs' end it "should be backwards compatible with the old way" do - config = ' + config = YAML.load ' browser: phantomjs: "casperjs" ' @@ -92,17 +92,17 @@ describe "different ways of determining the snap file" do it "should calculate the snap file from the engine" do - config = 'browser: phantomjs' + config = YAML.load 'browser: phantomjs' wraith = Wraith::Wraith.new(config, true) expect(wraith.snap_file).to include 'lib/wraith/javascript/phantom.js' - config = 'browser: casperjs' + config = YAML.load 'browser: casperjs' wraith = Wraith::Wraith.new(config, true) expect(wraith.snap_file).to include 'lib/wraith/javascript/casper.js' end it "should calculate the snap file in a backwards-compatible way" do - config = ' + config = YAML.load ' browser: phantomjs: "casperjs" ' @@ -111,7 +111,7 @@ end it "should allow users to specify their own snap file" do - config = ' + config = YAML.load ' browser: casperjs snap_file: path/to/snap.js ' @@ -123,13 +123,13 @@ describe "different modes of efficiency (resize or reload)" do it "should trigger efficient mode if resize was specified" do - config = 'resize_or_reload: "resize"' + config = YAML.load 'resize_or_reload: "resize"' wraith = Wraith::Wraith.new(config, true) expect(wraith.resize) end it "should fall back to slow mode if reload was specified" do - config = 'resize_or_reload: "reload"' + config = YAML.load 'resize_or_reload: "reload"' wraith = Wraith::Wraith.new(config, true) expect(wraith.resize).to eq false end diff --git a/spec/save_images_spec.rb b/spec/save_images_spec.rb new file mode 100644 index 00000000..779157d4 --- /dev/null +++ b/spec/save_images_spec.rb @@ -0,0 +1,56 @@ +require "_helpers" +require "image_size" + +describe "wraith config" do + + let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--phantom.yaml" } + let(:saving) { Wraith::SaveImages.new(config_name) } + + describe "saving images" do + + it "should pass the width plainly to the CLI when running in inefficient mode" do + prepared_width = saving.prepare_widths_for_cli 432 + expect(prepared_width).to eq 432 + end + + it "should pass an array of widths to CLI when running in efficient mode" do + prepared_width = saving.prepare_widths_for_cli [432, 21, 100] + expect(prepared_width).to eq "'432','21','100'" + end + + it "should create fewer jobs when in efficient mode" do + base_config = ' + domains: + test: http://www.bbc.com + paths: + test: /mypage + screen_widths: + - 320 + - 464 + - 624 + ' + efficient_config = YAML.load(base_config + " + resize_or_reload: resize + ") + efficient_saving = Wraith::SaveImages.new(efficient_config, false, true) + inefficient_config = YAML.load(base_config + " + resize_or_reload: reload + ") + inefficient_saving = Wraith::SaveImages.new(inefficient_config, false, true) + + efficient_jobs = efficient_saving.define_jobs + inefficient_jobs = inefficient_saving.define_jobs + + expect(efficient_jobs.length).to be 1 + expect(inefficient_jobs.length).to be 3 # 1 for each screen width + + # [["test", "/mypage", "'320','464','624'", "http://www.bbc.com/mypage", "/test/MULTI__test.png", " ", "false", "false"]] + expect(efficient_jobs[0][2]).to eq "'320','464','624'" + + # [["test", "/mypage", 320, "http://www.bbc.com/mypage", "/test/320__test.png", " ", "false", "false"], ["test", "/mypage", 464, "http://www.bbc.com/mypage", "/test/464__test.png", " ", "false", "false"], ["test", "/mypage", 624, "http://www.bbc.com/mypage", "/test/624__test.png", " ", "false", "false"]] + expect(inefficient_jobs[0][2]).to eq 320 + end + + end + +end From 604af554edd663e9eff33305daeddc465dfb4f11 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Tue, 1 Dec 2015 15:24:02 +0000 Subject: [PATCH 21/21] refactored tests --- spec/before_capture_spec.rb | 81 +++++++++++++ spec/configs/test_config--phantom.yaml | 2 + spec/gallery_spec.rb | 38 ++++++ spec/resize_reload_spec.rb | 55 +++++++++ spec/save_images_spec.rb | 82 +++++++------ spec/wraith_spec.rb | 160 ------------------------- 6 files changed, 216 insertions(+), 202 deletions(-) create mode 100644 spec/before_capture_spec.rb create mode 100644 spec/gallery_spec.rb create mode 100644 spec/resize_reload_spec.rb delete mode 100644 spec/wraith_spec.rb diff --git a/spec/before_capture_spec.rb b/spec/before_capture_spec.rb new file mode 100644 index 00000000..1aca6856 --- /dev/null +++ b/spec/before_capture_spec.rb @@ -0,0 +1,81 @@ +require "_helpers" + +describe Wraith do + let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--casper.yaml" } + let(:test_url1) { "http://www.bbc.com/afrique" } + let(:diff_image) { "shots/test/test_diff.png" } + let(:data_txt) { "shots/test/test.txt" } + let(:saving) { Wraith::SaveImages.new(config_name) } + let(:wraith) { Wraith::Wraith.new(config_name) } + let(:selector) { "body" } + let(:before_suite_js) { "spec/js/global.js" } + let(:before_capture_js) { "spec/js/path.js" } + + describe "When hooking into beforeCapture (CasperJS)" do + + it "Executes the global JS before capturing" do + run_js_then_capture( + global_js: before_suite_js, + path_js: 'false', + output_should_look_like: 'spec/base/global.png', + engine: 'casperjs' + ) + end + + it "Executes the path-level JS before capturing" do + run_js_then_capture( + global_js: 'false', + path_js: before_capture_js, + output_should_look_like: 'spec/base/path.png', + engine: 'casperjs' + ) + end + + it "Executes the global JS before the path-level JS" do + run_js_then_capture( + global_js: before_suite_js, + path_js: before_capture_js, + output_should_look_like: 'spec/base/path.png', + engine: 'casperjs' + ) + end + end + + # @TODO - uncomment and figure out why broken + # describe "When hooking into beforeCapture (PhantomJS)" do + # let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--phantom.yaml" } + # let(:saving) { Wraith::SaveImages.new(config_name) } + # let(:wraith) { Wraith::Wraith.new(config_name) } + # let(:selector) { "body" } + # let(:before_suite_js) { "../../spec/js/global.js" } + # let(:before_capture_js) { "../../spec/js/path.js" } + + # it "Executes the global JS before capturing" do + # run_js_then_capture( + # global_js: before_suite_js, + # path_js: 'false', + # output_should_look_like: 'spec/base/global.png', + # engine: 'phantomjs' + # ) + # end + + # it "Executes the path-level JS before capturing" do + # run_js_then_capture( + # global_js: 'false', + # path_js: before_capture_js, + # output_should_look_like: 'spec/base/path.png', + # engine: 'phantomjs' + # ) + # end + + # it "Executes the global JS before the path-level JS" do + # run_js_then_capture( + # global_js: before_suite_js, + # path_js: before_capture_js, + # output_should_look_like: 'spec/base/path.png', + # engine: 'phantomjs' + # ) + # end + # end + +end diff --git a/spec/configs/test_config--phantom.yaml b/spec/configs/test_config--phantom.yaml index 699eb32b..f92d0090 100644 --- a/spec/configs/test_config--phantom.yaml +++ b/spec/configs/test_config--phantom.yaml @@ -9,6 +9,8 @@ browser: # we've deliberately left out the snap_file +resize_or_reload: resize + # Type the name of the directory that shots will be stored in directory: 'shots' diff --git a/spec/gallery_spec.rb b/spec/gallery_spec.rb new file mode 100644 index 00000000..114ae1b5 --- /dev/null +++ b/spec/gallery_spec.rb @@ -0,0 +1,38 @@ +require "_helpers" + +describe Wraith do + let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--phantom.yaml" } + let(:gallery) { Wraith::GalleryGenerator.new(config_name, false) } + + describe "When generating gallery" do + + it "should not break when there is a `-` in the filename" do + dirs = gallery.parse_directories 'spec/thumbnails' + + images = [ + { + :filename => 'home/test_image-afrique.png', + :thumb => 'thumbnails/home/test_image-afrique.png' + }, + { + :filename => 'home/test_image-russian.png', + :thumb => 'thumbnails/home/test_image-russian.png' + } + ] + + dirs['home'][0][:variants].each_with_index do |image, i| + expect(image[:filename]).to eq images[i][:filename] + expect(image[:thumb]).to eq images[i][:thumb] + end + + diff = { + :filename => 'home/test_image-diff.png', + :thumb => 'thumbnails/home/test_image-diff.png' + } + + expect(dirs['home'][0][:diff][:filename]).to eq 'home/test_image-diff.png' + expect(dirs['home'][0][:diff][:thumb]).to eq 'thumbnails/home/test_image-diff.png' + end + end + +end diff --git a/spec/resize_reload_spec.rb b/spec/resize_reload_spec.rb new file mode 100644 index 00000000..104fc5f7 --- /dev/null +++ b/spec/resize_reload_spec.rb @@ -0,0 +1,55 @@ +require "_helpers" + +describe "wraith config" do + + let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--phantom.yaml" } + let(:saving) { Wraith::SaveImages.new(config_name) } + + describe "saving images" do + + it "should pass the width plainly to the CLI when running in inefficient mode" do + prepared_width = saving.prepare_widths_for_cli 432 + expect(prepared_width).to eq 432 + end + + it "should pass an array of widths to CLI when running in efficient mode" do + prepared_width = saving.prepare_widths_for_cli [432, 21, 100] + expect(prepared_width).to eq "'432','21','100'" + end + + it "should create fewer jobs when in efficient mode" do + base_config = ' + domains: + test: http://www.bbc.com + paths: + test: /mypage + screen_widths: + - 320 + - 464 + - 624 + ' + efficient_config = YAML.load(base_config + " + resize_or_reload: resize + ") + efficient_saving = Wraith::SaveImages.new(efficient_config, false, true) + inefficient_config = YAML.load(base_config + " + resize_or_reload: reload + ") + inefficient_saving = Wraith::SaveImages.new(inefficient_config, false, true) + + efficient_jobs = efficient_saving.define_jobs + inefficient_jobs = inefficient_saving.define_jobs + + expect(efficient_jobs.length).to be 1 + expect(inefficient_jobs.length).to be 3 # 1 for each screen width + + # [["test", "/mypage", "'320','464','624'", "http://www.bbc.com/mypage", "/test/MULTI__test.png", " ", "false", "false"]] + expect(efficient_jobs[0][2]).to eq "'320','464','624'" + + # [["test", "/mypage", 320, "http://www.bbc.com/mypage", "/test/320__test.png", " ", "false", "false"], ["test", "/mypage", 464, "http://www.bbc.com/mypage", "/test/464__test.png", " ", "false", "false"], ["test", "/mypage", 624, "http://www.bbc.com/mypage", "/test/624__test.png", " ", "false", "false"]] + expect(inefficient_jobs[0][2]).to eq 320 + end + + end + +end diff --git a/spec/save_images_spec.rb b/spec/save_images_spec.rb index 779157d4..953aea1f 100644 --- a/spec/save_images_spec.rb +++ b/spec/save_images_spec.rb @@ -1,56 +1,54 @@ require "_helpers" require "image_size" -describe "wraith config" do - +describe Wraith do let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--phantom.yaml" } + let(:test_url1) { "http://www.bbc.com/afrique" } + let(:test_url2) { "http://www.bbc.com/russian" } + let(:test_image1) { "shots/test/test1.png" } + let(:test_image2) { "shots/test/test(2).png" } + let(:diff_image) { "shots/test/test_diff.png" } + let(:data_txt) { "shots/test/test.txt" } + let(:selector) { "" } let(:saving) { Wraith::SaveImages.new(config_name) } + let(:wraith) { Wraith::Wraith.new(config_name) } - describe "saving images" do + before(:each) do + Wraith::FolderManager.new(config_name).clear_shots_folder + Dir.mkdir("shots/test") + end - it "should pass the width plainly to the CLI when running in inefficient mode" do - prepared_width = saving.prepare_widths_for_cli 432 - expect(prepared_width).to eq 432 - end + describe "When capturing an image" do + let(:image_size) { ImageSize.path(test_image1).size } - it "should pass an array of widths to CLI when running in efficient mode" do - prepared_width = saving.prepare_widths_for_cli [432, 21, 100] - expect(prepared_width).to eq "'432','21','100'" + it "saves image" do + saving.capture_page_image(wraith.engine, test_url1, 320, test_image1, selector, 'false', 'false') + expect(image_size[0]).to eq 320 end + end - it "should create fewer jobs when in efficient mode" do - base_config = ' - domains: - test: http://www.bbc.com - paths: - test: /mypage - screen_widths: - - 320 - - 464 - - 624 - ' - efficient_config = YAML.load(base_config + " - resize_or_reload: resize - ") - efficient_saving = Wraith::SaveImages.new(efficient_config, false, true) - inefficient_config = YAML.load(base_config + " - resize_or_reload: reload - ") - inefficient_saving = Wraith::SaveImages.new(inefficient_config, false, true) - - efficient_jobs = efficient_saving.define_jobs - inefficient_jobs = inefficient_saving.define_jobs - - expect(efficient_jobs.length).to be 1 - expect(inefficient_jobs.length).to be 3 # 1 for each screen width - - # [["test", "/mypage", "'320','464','624'", "http://www.bbc.com/mypage", "/test/MULTI__test.png", " ", "false", "false"]] - expect(efficient_jobs[0][2]).to eq "'320','464','624'" - - # [["test", "/mypage", 320, "http://www.bbc.com/mypage", "/test/320__test.png", " ", "false", "false"], ["test", "/mypage", 464, "http://www.bbc.com/mypage", "/test/464__test.png", " ", "false", "false"], ["test", "/mypage", 624, "http://www.bbc.com/mypage", "/test/624__test.png", " ", "false", "false"]] - expect(inefficient_jobs[0][2]).to eq 320 - end + describe "When comparing images" do + it "should compare" do + create_diff_image + crop_images + compare_images + diff = ImageSize.path(diff_image).size + + expect(diff[0]).to eq 320 + end end + describe "When generating thumbnails" do + it "produce thumbnails" do + create_diff_image + crop_images + Wraith::CompareImages.new(config_name).compare_task(test_image1, test_image2, diff_image, data_txt) + Wraith::Thumbnails.new(config_name).generate_thumbnails + + expect(File).to exist("shots/thumbnails/test/test1.png") + expect(File).to exist("shots/thumbnails/test/test(2).png") + expect(File).to exist("shots/thumbnails/test/test_diff.png") + end + end end diff --git a/spec/wraith_spec.rb b/spec/wraith_spec.rb deleted file mode 100644 index 4bfb79fd..00000000 --- a/spec/wraith_spec.rb +++ /dev/null @@ -1,160 +0,0 @@ -require "_helpers" -require "image_size" - -describe Wraith do - let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--phantom.yaml" } - let(:test_url1) { "http://www.bbc.com/afrique" } - let(:test_url2) { "http://www.bbc.com/russian" } - let(:test_image1) { "shots/test/test1.png" } - let(:test_image2) { "shots/test/test(2).png" } - let(:diff_image) { "shots/test/test_diff.png" } - let(:data_txt) { "shots/test/test.txt" } - let(:selector) { "" } - let(:saving) { Wraith::SaveImages.new(config_name) } - let(:wraith) { Wraith::Wraith.new(config_name) } - - before(:each) do - Wraith::FolderManager.new(config_name).clear_shots_folder - Dir.mkdir("shots/test") - end - - describe "When capturing an image" do - let(:image_size) { ImageSize.path(test_image1).size } - - it "saves image" do - saving.capture_page_image(wraith.engine, test_url1, 320, test_image1, selector, 'false', 'false') - expect(image_size[0]).to eq 320 - end - end - - describe "When comparing images" do - it "should compare" do - create_diff_image - crop_images - compare_images - - diff = ImageSize.path(diff_image).size - - expect(diff[0]).to eq 320 - end - end - - describe "When generating thumbnails" do - it "produce thumbnails" do - create_diff_image - crop_images - Wraith::CompareImages.new(config_name).compare_task(test_image1, test_image2, diff_image, data_txt) - Wraith::Thumbnails.new(config_name).generate_thumbnails - - expect(File).to exist("shots/thumbnails/test/test1.png") - expect(File).to exist("shots/thumbnails/test/test(2).png") - expect(File).to exist("shots/thumbnails/test/test_diff.png") - end - end - - describe "When generating gallery" do - let(:gallery) { Wraith::GalleryGenerator.new(config_name, false) } - - it "should not break when there is a `-` in the filename" do - dirs = gallery.parse_directories 'spec/thumbnails' - - images = [ - { - :filename => 'home/test_image-afrique.png', - :thumb => 'thumbnails/home/test_image-afrique.png' - }, - { - :filename => 'home/test_image-russian.png', - :thumb => 'thumbnails/home/test_image-russian.png' - } - ] - - dirs['home'][0][:variants].each_with_index do |image, i| - expect(image[:filename]).to eq images[i][:filename] - expect(image[:thumb]).to eq images[i][:thumb] - end - - diff = { - :filename => 'home/test_image-diff.png', - :thumb => 'thumbnails/home/test_image-diff.png' - } - - expect(dirs['home'][0][:diff][:filename]).to eq 'home/test_image-diff.png' - expect(dirs['home'][0][:diff][:thumb]).to eq 'thumbnails/home/test_image-diff.png' - end - end - - describe "When hooking into beforeCapture (CasperJS)" do - let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--casper.yaml" } - let(:saving) { Wraith::SaveImages.new(config_name) } - let(:wraith) { Wraith::Wraith.new(config_name) } - let(:selector) { "body" } - let(:before_suite_js) { "spec/js/global.js" } - let(:before_capture_js) { "spec/js/path.js" } - - it "Executes the global JS before capturing" do - run_js_then_capture( - global_js: before_suite_js, - path_js: 'false', - output_should_look_like: 'spec/base/global.png', - engine: 'casperjs' - ) - end - - it "Executes the path-level JS before capturing" do - run_js_then_capture( - global_js: 'false', - path_js: before_capture_js, - output_should_look_like: 'spec/base/path.png', - engine: 'casperjs' - ) - end - - it "Executes the global JS before the path-level JS" do - run_js_then_capture( - global_js: before_suite_js, - path_js: before_capture_js, - output_should_look_like: 'spec/base/path.png', - engine: 'casperjs' - ) - end - end - - # @TODO - uncomment and figure out why broken - # describe "When hooking into beforeCapture (PhantomJS)" do - # let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--phantom.yaml" } - # let(:saving) { Wraith::SaveImages.new(config_name) } - # let(:wraith) { Wraith::Wraith.new(config_name) } - # let(:selector) { "body" } - # let(:before_suite_js) { "../../spec/js/global.js" } - # let(:before_capture_js) { "../../spec/js/path.js" } - - # it "Executes the global JS before capturing" do - # run_js_then_capture( - # global_js: before_suite_js, - # path_js: 'false', - # output_should_look_like: 'spec/base/global.png', - # engine: 'phantomjs' - # ) - # end - - # it "Executes the path-level JS before capturing" do - # run_js_then_capture( - # global_js: 'false', - # path_js: before_capture_js, - # output_should_look_like: 'spec/base/path.png', - # engine: 'phantomjs' - # ) - # end - - # it "Executes the global JS before the path-level JS" do - # run_js_then_capture( - # global_js: before_suite_js, - # path_js: before_capture_js, - # output_should_look_like: 'spec/base/path.png', - # engine: 'phantomjs' - # ) - # end - # end - -end