Skip to content

Commit

Permalink
Merge pull request #339 from BBC-News/before_capture_pathfix
Browse files Browse the repository at this point in the history
Before capture pathfix
  • Loading branch information
ChrisBAshton committed Dec 2, 2015
2 parents 349ff7c + 9fed114 commit 53d2484
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/wraith/javascript/_phantom__common.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module.exports = function (config) {
// see https://github.com/n1k0/casperjs/issues/1068
setTimeout(function(){
phantom.exit();
}, 10);
}, 30);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/wraith/javascript/casper.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ casper.open(url);
casper.viewport(currentDimensions.viewportWidth, currentDimensions.viewportHeight);
casper.then(function() {
if (globalBeforeCaptureJS) {
require('./' + globalBeforeCaptureJS)(this);
require(globalBeforeCaptureJS)(this);
}
});
casper.then(function() {
if (pathBeforeCaptureJS) {
require('./' + pathBeforeCaptureJS)(this);
require(pathBeforeCaptureJS)(this);
}
});
// waits for all images to download before taking screenshots
Expand Down
3 changes: 2 additions & 1 deletion lib/wraith/save_images.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "wraith"
require "parallel"
require "shellwords"
require "wraith/utilities"

class Wraith::SaveImages
attr_reader :wraith, :history, :meta
Expand Down Expand Up @@ -135,7 +136,7 @@ def selector
end

def before_capture
options["before_capture"] || "false"
@options["before_capture"] ? convert_to_absolute(@options["before_capture"]) : "false"
end

def base_url
Expand Down
12 changes: 12 additions & 0 deletions lib/wraith/utilities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# @TODO - extract more shared functions to this file

def convert_to_absolute(filepath)
if filepath[0] == '/'
# filepath is already absolute. return unchanged
filepath
else
# filepath is relative. it must be converted to absolute
working_dir = `pwd`.chomp
"#{working_dir}/#{filepath}"
end
end
2 changes: 1 addition & 1 deletion lib/wraith/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Wraith
VERSION = "2.8.1"
VERSION = "3.0.1"
end
5 changes: 3 additions & 2 deletions lib/wraith/wraith.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "yaml"
require "wraith/utilities"

class Wraith::Wraith
attr_accessor :config
Expand Down Expand Up @@ -38,7 +39,7 @@ def engine
end

def snap_file
@config["snap_file"] || snap_file_from_engine(engine)
@config["snap_file"] ? convert_to_absolute(@config["snap_file"]) : snap_file_from_engine(engine)
end

def snap_file_from_engine(engine)
Expand All @@ -55,7 +56,7 @@ def snap_file_from_engine(engine)
end

def before_capture
@config["before_capture"] || "false"
@config["before_capture"] ? convert_to_absolute(@config["before_capture"]) : "false"
end

def widths
Expand Down
23 changes: 23 additions & 0 deletions spec/before_capture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@
let(:before_suite_js) { "spec/js/global.js" }
let(:before_capture_js) { "spec/js/path.js" }

describe "different ways of determining the before_capture file" do
it "should allow users to specify the relative path to the before_capture file" do
config = YAML.load '
browser: casperjs
before_capture: javascript/do_something.js
'
wraith = Wraith::Wraith.new(config, true)
# not sure about having code IN the test, but we want to get this right.
expect(wraith.before_capture).to eq (`pwd`.chomp! + '/javascript/do_something.js')
end

it "should allow users to specify the absolute path to the before_capture file" do
config = YAML.load '
browser: casperjs
before_capture: /Users/some_user/wraith/javascript/do_something.js
'
wraith = Wraith::Wraith.new(config, true)
expect(wraith.before_capture).to eq ('/Users/some_user/wraith/javascript/do_something.js')
end
end

# @TODO - we need tests determining the path to "path-level before_capture hooks"

describe "When hooking into beforeCapture (CasperJS)" do

it "Executes the global JS before capturing" do
Expand Down
14 changes: 12 additions & 2 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,23 @@
expect(wraith.snap_file).to include 'lib/wraith/javascript/casper.js'
end

it "should allow users to specify their own snap file" do
it "should allow users to specify the relative path to their own snap file" do
config = YAML.load '
browser: casperjs
snap_file: path/to/snap.js
'
wraith = Wraith::Wraith.new(config, true)
expect(wraith.snap_file).to eq 'path/to/snap.js'
# not sure about having code IN the test, but we want to get this right.
expect(wraith.snap_file).to eq (`pwd`.chomp! + '/path/to/snap.js')
end

it "should allow users to specify the absolute path to their own snap file" do
config = YAML.load '
browser: casperjs
snap_file: /Users/my_username/Sites/bbc/wraith/path/to/snap.js
'
wraith = Wraith::Wraith.new(config, true)
expect(wraith.snap_file).to eq ('/Users/my_username/Sites/bbc/wraith/path/to/snap.js')
end
end

Expand Down

0 comments on commit 53d2484

Please sign in to comment.