wraith 3.0.4 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +4 -0
  3. data/Gemfile +1 -1
  4. data/lib/wraith/cli.rb +108 -73
  5. data/lib/wraith/compare_images.rb +4 -2
  6. data/lib/wraith/crop.rb +3 -1
  7. data/lib/wraith/folder.rb +6 -4
  8. data/lib/wraith/gallery.rb +21 -22
  9. data/lib/wraith/helpers/capture_options.rb +52 -0
  10. data/lib/wraith/helpers/custom_exceptions.rb +8 -0
  11. data/lib/wraith/helpers/logger.rb +20 -0
  12. data/lib/wraith/helpers/save_metadata.rb +31 -0
  13. data/lib/wraith/{utilities.rb → helpers/utilities.rb} +5 -4
  14. data/lib/wraith/javascript/_helper.js +0 -2
  15. data/lib/wraith/javascript/casper.js +26 -16
  16. data/lib/wraith/javascript/phantom.js +148 -5
  17. data/lib/wraith/save_images.rb +28 -104
  18. data/lib/wraith/spider.rb +12 -5
  19. data/lib/wraith/thumbnails.rb +0 -2
  20. data/lib/wraith/validate.rb +98 -0
  21. data/lib/wraith/version.rb +1 -1
  22. data/lib/wraith/wraith.rb +19 -12
  23. data/spec/_helpers.rb +4 -10
  24. data/spec/before_capture_spec.rb +19 -10
  25. data/spec/config_spec.rb +2 -9
  26. data/spec/configs/test_config--casper.yaml +1 -5
  27. data/spec/construct_command_spec.rb +43 -0
  28. data/spec/gallery_spec.rb +1 -2
  29. data/spec/js/custom_snap_file.js +26 -16
  30. data/spec/js/global.js +2 -1
  31. data/spec/js/path.js +2 -1
  32. data/spec/resize_reload_spec.rb +4 -8
  33. data/spec/save_images_spec.rb +3 -2
  34. data/spec/validate_spec.rb +76 -0
  35. data/templates/configs/capture.yaml +61 -0
  36. data/templates/configs/history.yaml +81 -0
  37. data/templates/configs/spider.yaml +13 -2
  38. data/templates/javascript/cookies_and_headers--casper.js +16 -0
  39. data/templates/javascript/cookies_and_headers--phantom.js +26 -0
  40. data/templates/javascript/disable_javascript--casper.js +11 -0
  41. data/templates/javascript/disable_javascript--phantom.js +13 -0
  42. data/templates/javascript/interact--casper.js +11 -0
  43. data/templates/javascript/interact--phantom.js +17 -0
  44. data/templates/javascript/wait--casper.js +8 -0
  45. data/templates/javascript/wait--phantom.js +8 -0
  46. data/wraith.gemspec +1 -1
  47. metadata +27 -14
  48. data/lib/wraith/javascript/_phantom__common.js +0 -120
  49. data/lib/wraith/javascript/phantom--nojs.js +0 -6
  50. data/templates/configs/component.yaml +0 -60
  51. data/templates/configs/multiple_domains.yaml +0 -53
  52. data/templates/javascript/beforeCapture--casper_example.js +0 -12
  53. data/templates/javascript/beforeCapture--phantom_example.js +0 -36
data/spec/gallery_spec.rb CHANGED
@@ -34,5 +34,4 @@ describe Wraith do
34
34
  expect(dirs['home'][0][:diff][:thumb]).to eq 'thumbnails/home/test_image-diff.png'
35
35
  end
36
36
  end
37
-
38
- end
37
+ end
@@ -39,25 +39,35 @@ casper.start();
39
39
  casper.open(url);
40
40
  casper.viewport(dimensions.viewportWidth, dimensions.viewportHeight);
41
41
  casper.then(function() {
42
- if (globalBeforeCaptureJS) {
43
- require('./' + globalBeforeCaptureJS)(this);
42
+ var self = this;
43
+ if (globalBeforeCaptureJS && pathBeforeCaptureJS) {
44
+ require(globalBeforeCaptureJS)(self, function thenExecuteOtherBeforeCaptureFile() {
45
+ require(pathBeforeCaptureJS)(self, captureImage);
46
+ });
44
47
  }
45
- });
46
- casper.then(function() {
47
- if (pathBeforeCaptureJS) {
48
- require('./' + pathBeforeCaptureJS)(this);
48
+ else if (globalBeforeCaptureJS) {
49
+ require(globalBeforeCaptureJS)(self, captureImage);
50
+ }
51
+ else if (pathBeforeCaptureJS) {
52
+ require(pathBeforeCaptureJS)(self, captureImage);
53
+ }
54
+ else {
55
+ captureImage();
49
56
  }
50
57
  });
51
- // waits for all images to download before taking screenshots
52
- // (broken images are a big cause of Wraith failures!)
53
- // Credit: http://reff.it/8m3HYP
54
- casper.waitFor(function() {
55
- return this.evaluate(function() {
56
- var images = document.getElementsByTagName('img');
57
- return Array.prototype.every.call(images, function(i) { return i.complete; });
58
+
59
+ function captureImage() {
60
+ // waits for all images to download before taking screenshots
61
+ // (broken images are a big cause of Wraith failures!)
62
+ // Credit: http://reff.it/8m3HYP
63
+ casper.waitFor(function() {
64
+ return this.evaluate(function() {
65
+ var images = document.getElementsByTagName('img');
66
+ return Array.prototype.every.call(images, function(i) { return i.complete; });
67
+ });
68
+ }, function then () {
69
+ snap.bind(this)();
58
70
  });
59
- }, function then () {
60
- snap.bind(this)();
61
- });
71
+ }
62
72
 
63
73
  casper.run();
data/spec/js/global.js CHANGED
@@ -1,6 +1,7 @@
1
- module.exports = function (browserEngine) {
1
+ module.exports = function (browserEngine, ready) {
2
2
  browserEngine.evaluate(function () {
3
3
  document.body.innerHTML = ' ';
4
4
  document.body.style['background-color'] = 'red';
5
5
  });
6
+ ready();
6
7
  }
data/spec/js/path.js CHANGED
@@ -1,6 +1,7 @@
1
- module.exports = function (browserEngine) {
1
+ module.exports = function (browserEngine, ready) {
2
2
  browserEngine.evaluate(function () {
3
3
  document.body.innerHTML = ' ';
4
4
  document.body.style['background-color'] = 'green';
5
5
  });
6
+ ready();
6
7
  }
@@ -1,12 +1,10 @@
1
1
  require "_helpers"
2
2
 
3
3
  describe "wraith config" do
4
-
5
4
  let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--phantom.yaml" }
6
5
  let(:saving) { Wraith::SaveImages.new(config_name) }
7
6
 
8
7
  describe "saving images" do
9
-
10
8
  it "should pass the width plainly to the CLI when running in inefficient mode" do
11
9
  prepared_width = saving.prepare_widths_for_cli 432
12
10
  expect(prepared_width).to eq 432
@@ -14,7 +12,7 @@ describe "wraith config" do
14
12
 
15
13
  it "should pass an array of widths to CLI when running in efficient mode" do
16
14
  prepared_width = saving.prepare_widths_for_cli [432, 21, 100]
17
- expect(prepared_width).to eq "'432','21','100'"
15
+ expect(prepared_width).to eq "432,21,100"
18
16
  end
19
17
 
20
18
  it "should create fewer jobs when in efficient mode" do
@@ -43,13 +41,11 @@ describe "wraith config" do
43
41
  expect(efficient_jobs.length).to be 1
44
42
  expect(inefficient_jobs.length).to be 3 # 1 for each screen width
45
43
 
46
- # [["test", "/mypage", "'320','464','624'", "http://www.bbc.com/mypage", "/test/MULTI__test.png", " ", "false", "false"]]
47
- expect(efficient_jobs[0][2]).to eq "'320','464','624'"
44
+ # [["test", "/mypage", "320,464,624", "http://www.bbc.com/mypage", "/test/MULTI__test.png", " ", "false", "false"]]
45
+ expect(efficient_jobs[0][2]).to eq "320,464,624"
48
46
 
49
47
  # [["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"]]
50
48
  expect(inefficient_jobs[0][2]).to eq 320
51
49
  end
52
-
53
50
  end
54
-
55
- end
51
+ end
@@ -22,7 +22,8 @@ describe Wraith do
22
22
  let(:image_size) { ImageSize.path(test_image1).size }
23
23
 
24
24
  it "saves image" do
25
- saving.capture_page_image(wraith.engine, test_url1, 320, test_image1, selector, 'false', 'false')
25
+ capture_image = saving.construct_command(320, test_url1, test_image1, selector, false, false)
26
+ `#{capture_image}`
26
27
  expect(image_size[0]).to eq 320
27
28
  end
28
29
  end
@@ -51,4 +52,4 @@ describe Wraith do
51
52
  expect(File).to exist("shots/thumbnails/test/test_diff.png")
52
53
  end
53
54
  end
54
- end
55
+ end
@@ -0,0 +1,76 @@
1
+ require "_helpers"
2
+
3
+ describe "Wraith config validator" do
4
+ let(:config) do
5
+ YAML.load('
6
+ domains:
7
+ test: http://www.bbc.com
8
+
9
+ browser: "casperjs"
10
+ ')
11
+ end
12
+
13
+ describe "universal, basic validation for all modes" do
14
+ it "should validate a basic config" do
15
+ validate = Wraith::Validate.new(config, true).validate
16
+ end
17
+
18
+ it "should complain if the `domains` property is missing" do
19
+ config['domains'] = nil
20
+ expect { Wraith::Validate.new(config, true).validate }.to raise_error MissingRequiredPropertyError
21
+ end
22
+
23
+ it "should complain if the `browser` property is missing" do
24
+ config['browser'] = nil
25
+ expect { Wraith::Validate.new(config, true).validate }.to raise_error MissingRequiredPropertyError
26
+ end
27
+ end
28
+
29
+ describe "validation specific to capture mode" do
30
+ it "should complain if fewer than two domains are specified" do
31
+ expect { Wraith::Validate.new(config, true).validate('capture') }.to raise_error InvalidDomainsError
32
+ end
33
+
34
+ it "should complain if more than two domains are specified" do
35
+ config['domains'] = YAML.load('
36
+ test: http://something.bbc.com
37
+ stage: http://something-else.bbc.com
38
+ live: http://www.bbc.com
39
+ ')
40
+ expect { Wraith::Validate.new(config, true).validate('capture') }.to raise_error InvalidDomainsError
41
+ end
42
+
43
+ it "should be happy if exactly two domains are specified" do
44
+ config['domains'] = YAML.load('
45
+ test: http://something.bbc.com
46
+ live: http://www.bbc.com
47
+ ')
48
+ validate = Wraith::Validate.new(config, true).validate('capture')
49
+ end
50
+ end
51
+
52
+ describe "validations specific to history mode" do
53
+ let(:history_conf) do
54
+ config.merge(YAML.load('
55
+ history_dir: "history_shots"
56
+ '))
57
+ end
58
+
59
+ it "should complain if more than one domain is specified" do
60
+ history_conf['domains'] = YAML.load('
61
+ test: http://something.bbc.com
62
+ live: http://www.bbc.com
63
+ ')
64
+ expect { Wraith::Validate.new(history_conf, true).validate('history') }.to raise_error InvalidDomainsError
65
+ end
66
+
67
+ it "should complain if no history_dir is specified" do
68
+ history_conf['history_dir'] = nil
69
+ expect { Wraith::Validate.new(history_conf, true).validate('history') }.to raise_error MissingRequiredPropertyError
70
+ end
71
+
72
+ it "should be happy if a history_dir and one domain is specified" do
73
+ validate = Wraith::Validate.new(history_conf, true).validate('history')
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,61 @@
1
+ ##############################################################
2
+ ##############################################################
3
+ # This is an example configuration provided by Wraith.
4
+ # Feel free to amend for your own requirements.
5
+ # ---
6
+ # This particular config is intended to demonstrate how
7
+ # to use Wraith in 'capture' mode, which is best suited to
8
+ # comparing a test and live version of the same website.
9
+ #
10
+ # `wraith capture capture.yaml`
11
+ #
12
+ ##############################################################
13
+ ##############################################################
14
+
15
+ # (required) The engine to run Wraith with. Examples: 'phantomjs', 'casperjs', 'slimerjs'
16
+ browser: "phantomjs"
17
+
18
+ # (required) The domains to take screenshots of.
19
+ domains:
20
+ current: "http://www.example.com"
21
+ new: "http://develop.example.com"
22
+
23
+ # (required) The paths to capture. All paths should exist for both of the domains specified above.
24
+ paths:
25
+ home: /
26
+ about: /about
27
+ contact: /get-in-touch
28
+
29
+ # (required) Screen widths (and optional height) to resize the browser to before taking the screenshot.
30
+ screen_widths:
31
+ - 320
32
+ - 600x768
33
+ - 768
34
+ - 1024
35
+ - 1280
36
+
37
+ # (optional) JavaScript file to execute before taking screenshot of every path. Default: nil
38
+ before_capture: 'javascript/disable_javascript--phantom.js'
39
+
40
+ # (required) The directory that your screenshots will be stored in
41
+ directory: 'shots'
42
+
43
+ # (required) Amount of fuzz ImageMagick will use when comparing images. A higher fuzz makes the comparison less strict.
44
+ fuzz: '20%'
45
+
46
+ # (optional) The maximum acceptable level of difference (in %) between two images before Wraith reports a failure. Default: 0
47
+ threshold: 5
48
+
49
+ # (optional) Specify the template (and generated thumbnail sizes) for the gallery output.
50
+ gallery:
51
+ template: 'slideshow_template' # Examples: 'basic_template' (default), 'slideshow_template'
52
+ thumb_width: 200
53
+ thumb_height: 200
54
+
55
+ # (optional) Choose which results are displayed in the gallery, and in what order. Default: alphanumeric
56
+ # Options:
57
+ # alphanumeric - all paths (with or without a difference) are shown, sorted by path
58
+ # diffs_first - all paths (with or without a difference) are shown, sorted by difference size (largest first)
59
+ # diffs_only - only paths with a difference are shown, sorted by difference size (largest first)
60
+ # Note: different screen widths are always grouped together.
61
+ mode: diffs_first
@@ -0,0 +1,81 @@
1
+ ##############################################################
2
+ ##############################################################
3
+ # This is an example configuration provided by Wraith.
4
+ # Feel free to amend for your own requirements.
5
+ # ---
6
+ # This particular config is intended to demonstrate how
7
+ # to use Wraith in 'history' mode, which is best suited to
8
+ # making sure your site's appearance remains consistent over
9
+ # time.
10
+ #
11
+ # `wraith history history.yaml` # generate base screenshots
12
+ # `wraith latest history.yaml` # take new shots and compare
13
+ #
14
+ ##############################################################
15
+ ##############################################################
16
+
17
+ # (required) The engine to run Wraith with. Examples: 'phantomjs', 'casperjs', 'slimerjs'
18
+ browser: "casperjs"
19
+
20
+ # (required) The domain to take screenshots of.
21
+ domains:
22
+ english: "http://www.bbc.co.uk"
23
+
24
+ # (required) The paths to capture. This particular config is using casperjs, so we can take screenshots of selectors rather than the entire page.
25
+ paths:
26
+ clickable_guide:
27
+ path: /news/entertainment-arts-27221191
28
+ selector: '.idt__news' # (optional) selector to take a screenshot of
29
+ clickable_guide__after_click:
30
+ path: /news/entertainment-arts-27221191
31
+ selector: '.idt__news'
32
+ before_capture: 'javascript/beforeCapture--casper_example.js' # (optional) JavaScript file to execute before taking the screenshot of this path.
33
+
34
+ # (optional) JavaScript file to execute before taking screenshot of every path. Default: nil
35
+ before_capture: 'javascript/wait--casper.js'
36
+
37
+ # (required) Screen widths (and optional height) to resize the browser to before taking the screenshot.
38
+ screen_widths:
39
+ - 320
40
+ - 600x768
41
+ - 768
42
+ - 1024
43
+ - 1280
44
+
45
+ # (optional) Resize to each screen width (efficient), or reload at each screen width (costly). Default: 'reload'
46
+ resize_or_reload: 'resize'
47
+
48
+ # (required for history mode, otherwise optional) The directory that your base screenshots will be stored in.
49
+ history_dir: 'shots_base'
50
+
51
+ # (required) The directory that your latest screenshots will be stored in
52
+ directory: 'shots'
53
+
54
+ # (required) Amount of fuzz ImageMagick will use when comparing images. A higher fuzz makes the comparison less strict.
55
+ fuzz: '20%'
56
+
57
+ # (optional) The maximum acceptable level of difference (in %) between two images before Wraith reports a failure. Default: 0
58
+ threshold: 5
59
+
60
+ # (optional) Specify the template (and generated thumbnail sizes) for the gallery output.
61
+ gallery:
62
+ template: 'slideshow_template' # Examples: 'basic_template' (default), 'slideshow_template'
63
+ thumb_width: 200
64
+ thumb_height: 200
65
+
66
+ # (optional) Choose which results are displayed in the gallery, and in what order. Default: alphanumeric
67
+ # Options:
68
+ # alphanumeric - all paths (with or without a difference) are shown, sorted by path
69
+ # diffs_first - all paths (with or without a difference) are shown, sorted by difference size (largest first)
70
+ # diffs_only - only paths with a difference are shown, sorted by difference size (largest first)
71
+ # Note: different screen widths are always grouped together.
72
+ mode: diffs_first
73
+
74
+ # (optional) Choose to run Wraith in verbose mode, for easier debugging. Default: false
75
+ verbose: true
76
+
77
+ # (optional) Color to highlight the image diff. Default: 'blue'
78
+ highlight_color: red
79
+
80
+ # (optional) Parameters to pass to Phantom/Casper command line. Default: '--ignore-ssl-errors=true --ssl-protocol=tlsv1'
81
+ phantomjs_options: ''
@@ -1,6 +1,17 @@
1
+ ##############################################################
2
+ ##############################################################
3
+ # This is an example configuration provided by Wraith.
4
+ # Feel free to amend for your own requirements.
5
+ # ---
6
+ # This particular config is intended to demonstrate how
7
+ # to use Wraith in 'spider' mode.
8
+ ##############################################################
9
+ ##############################################################
10
+
11
+
1
12
  # Add as many domains as necessary. Key will act as a label
2
13
  domains:
3
- my_site: "http://www.example.com"
14
+ my_site: "http://www.example.com"
4
15
  my_other_site: "http://www.test.example.com"
5
16
 
6
17
  # Notice the absence of a `paths` property. When no paths are provided, Wraith defaults to
@@ -38,7 +49,7 @@ screen_widths:
38
49
  browser: "phantomjs"
39
50
 
40
51
  # the directory that your latest screenshots will be stored in
41
- directory: 'shots_nojs'
52
+ directory: 'shots'
42
53
 
43
54
  # choose how results are displayed in the gallery (default is `alphanumeric` if omitted)
44
55
  # Different screen widths are always grouped together.
@@ -0,0 +1,16 @@
1
+ // ######################################################
2
+ // This is an example module provided by Wraith.
3
+ // Feel free to amend for your own requirements.
4
+ // ######################################################
5
+ module.exports = function (casper, ready) {
6
+ // reload page with headers set
7
+ casper.open(casper.page.url, {
8
+ method: 'get',
9
+ headers: {
10
+ 'SOME-HEADER': 'fish'
11
+ }
12
+ });
13
+ casper.then(function () {
14
+ setTimeout(ready, 1000);
15
+ });
16
+ }
@@ -0,0 +1,26 @@
1
+ // ######################################################
2
+ // This is an example module provided by Wraith.
3
+ // Feel free to amend for your own requirements.
4
+ // ######################################################
5
+ module.exports = function (phantom, ready) {
6
+
7
+ page.customHeaders = {
8
+ 'SOME-HEADER': 'fish'
9
+ };
10
+
11
+ phantom.addCookie({
12
+ 'name': 'ckns_policy',
13
+ 'value': '111',
14
+ 'domain': '.bbc.co.uk'
15
+ });
16
+
17
+ phantom.addCookie({
18
+ 'name': 'locserv',
19
+ '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',
20
+ 'domain': '.bbc.co.uk'
21
+ });
22
+
23
+ phantom.open(phantom.url, function () {
24
+ setTimeout(ready, 5000);
25
+ });
26
+ }
@@ -0,0 +1,11 @@
1
+ // ######################################################
2
+ // This is an example module provided by Wraith.
3
+ // Feel free to amend for your own requirements.
4
+ // ######################################################
5
+ module.exports = function (casper, ready) {
6
+ // disable JavaScript
7
+ casper.options.pageSettings.javascriptEnabled = false;
8
+
9
+ // reload the page without JS enabled
10
+ casper.thenOpen(casper.page.url);
11
+ }
@@ -0,0 +1,13 @@
1
+ // ######################################################
2
+ // This is an example module provided by Wraith.
3
+ // Feel free to amend for your own requirements.
4
+ // ######################################################
5
+ module.exports = function (phantom, ready) {
6
+ // disable JavaScript
7
+ phantom.settings.javascriptEnabled = false;
8
+
9
+ // reload the page without JS enabled
10
+ phantom.open(phantom.url, function () {
11
+ setTimeout(ready, 5000);
12
+ });
13
+ }