autobrewster 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/BSD-LICENSE.txt ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2013, Made By Made Ltd
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ * Neither the name of the "Made By Made Ltd" nor the
12
+ names of its contributors may be used to endorse or promote products
13
+ derived from this software without specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL MADE BY MADE LTD BE LIABLE FOR ANY
19
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,21 @@
1
+ module AutoBrewster
2
+ class CLI
3
+ def initialize(args)
4
+ @args = args
5
+ end
6
+
7
+ def execute!
8
+ AutoBrewster.send(get_action)
9
+ end
10
+
11
+ def get_action
12
+ return :compare_screens if @args.length < 1
13
+
14
+ if !AutoBrewster.respond_to?(@args[0])
15
+ raise "Action \"#{@args[0]}\" not available on AutoBrewster"
16
+ else
17
+ return @args[0].to_sym
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ module AutoBrewster
2
+ class Middleware
3
+ attr_accessor :error
4
+
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ if env["PATH_INFO"] == "/__identify__"
11
+ [200, {}, [@app.object_id.to_s]]
12
+ else
13
+ begin
14
+ @app.call(env)
15
+ rescue StandardError => e
16
+ @error = e unless @error
17
+ raise e
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,103 @@
1
+ module AutoBrewster
2
+ class Screenshot
3
+ attr_accessor :server,
4
+ :path,
5
+ :url_paths,
6
+ :screen_widths,
7
+ :failed_fast
8
+
9
+ def initialize(server, path, url_paths, screen_widths)
10
+ @server = server
11
+ @path = path
12
+ @url_paths = url_paths
13
+ @screen_widths = screen_widths
14
+ end
15
+
16
+ def capture(output_directory)
17
+ @url_paths.each do |label, path|
18
+ @screen_widths.each do |width|
19
+ output_path = get_output_path(output_directory, label, width)
20
+ if File.exist?(output_path)
21
+ puts "Screenshot already exists for #{label} at #{width}. Skipping..."
22
+ else
23
+ puts `phantomjs #{snap_js_path} "#{get_url(path)}" "#{width}" "#{output_path}"`
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def compare_captured_screens(failfast = false)
30
+ create_diff_dir
31
+ failures = 0
32
+
33
+ @url_paths.each do |label, path|
34
+ @screen_widths.each do |width|
35
+ check_source_compare_screens_exist(label, width)
36
+
37
+ source_path = get_output_path(:source, label, width)
38
+ compare_path = get_output_path(:compare, label, width)
39
+ diff_path = get_output_path(:diff, label, width)
40
+
41
+ output = `compare -fuzz 20% -metric AE -highlight-color blue #{source_path} #{compare_path} #{diff_path} 2>&1`
42
+
43
+ failures += get_failure_count(output, label, width)
44
+
45
+ if failfast
46
+ @failed_fast = true
47
+ exit 1
48
+ end
49
+ end
50
+ end
51
+
52
+ exit 1 if failures > 0
53
+ end
54
+
55
+ def check_source_compare_screens_exist(label, width)
56
+ [get_output_path(:source, label, width), get_output_path(:compare, label, width)].each do |path|
57
+ raise "Screenshot at #{path} does not exist" unless File.exist?(path)
58
+ end
59
+ end
60
+
61
+ def clear_source_screens
62
+ clear_screenshot_directory(:source)
63
+ end
64
+
65
+ def clear_compare_screens
66
+ clear_screenshot_directory(:compare)
67
+ end
68
+
69
+ def clear_diff_screens
70
+ clear_screenshot_directory(:diff)
71
+ end
72
+
73
+ private
74
+ def get_output_path(directory, label, width)
75
+ "#{@path}/screens/#{directory}/#{label}-#{width}.jpg"
76
+ end
77
+
78
+ def get_url(path)
79
+ "#{@server.get_host_with_protocol_and_port}#{path}"
80
+ end
81
+
82
+ def create_diff_dir
83
+ FileUtils.mkdir_p("#{@path}/screens/diff")
84
+ end
85
+
86
+ def clear_screenshot_directory(directory)
87
+ FileUtils.rm_rf Dir.glob("#{@path}/screens/#{directory}/*.jpg")
88
+ end
89
+
90
+ def snap_js_path
91
+ File.expand_path('../../../snap.js', __FILE__)
92
+ end
93
+
94
+ def get_failure_count(output, label, width)
95
+ if output.strip! != "0"
96
+ puts "#{label} at #{width} wide doesn't match source screen"
97
+ return 1
98
+ end
99
+
100
+ return 0
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,70 @@
1
+ require 'rack'
2
+ require 'net/http'
3
+ require File.expand_path('../middleware', __FILE__)
4
+
5
+ module AutoBrewster
6
+ class Server
7
+ attr_accessor :port,
8
+ :server,
9
+ :rackup_path,
10
+ :hostname,
11
+ :app
12
+
13
+ def initialize(server, port, rackup_path = 'config.ru', hostname = false)
14
+ ENV['RACK_ENV'] ||= 'test'
15
+
16
+ @port = port
17
+ @server = server
18
+ @rackup_path = rackup_path
19
+ @hostname = hostname
20
+ @app = build_rack_app
21
+
22
+ @middleware = AutoBrewster::Middleware.new(@app)
23
+ end
24
+
25
+ def start
26
+ @server_thread = Thread.new do
27
+ @server.call(@middleware, @port)
28
+ end
29
+
30
+ Timeout.timeout(10) { @server_thread.join(0.1) until responsive? }
31
+ end
32
+
33
+ def stop
34
+ return if @server_thread.nil?
35
+ @server_thread.kill
36
+ Timeout.timeout(10) { @server_thread.join(0.1) until !responsive? }
37
+ end
38
+
39
+ def get_host_with_protocol_and_port
40
+ if @hostname
41
+ return @hostname
42
+ else
43
+ return "http://127.0.0.1:#{@port}"
44
+ end
45
+ end
46
+
47
+ private
48
+ def responsive?
49
+ return false if @server_thread and @server_thread.join(0)
50
+
51
+ res = Net::HTTP.start('127.0.0.1', @port) { |http| http.get('/__identify__') }
52
+
53
+ if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
54
+ return res.body == @app.object_id.to_s
55
+ end
56
+ rescue SystemCallError
57
+ return false
58
+ end
59
+
60
+ def build_rack_app
61
+ rackup_path = @rackup_path
62
+
63
+ Rack::Builder.new do
64
+ map '/' do
65
+ run Rack::Builder.parse_file(rackup_path)[0]
66
+ end
67
+ end.to_app
68
+ end
69
+ end
70
+ end
data/snap.js ADDED
@@ -0,0 +1,30 @@
1
+ var system = require('system');
2
+ var page = require('webpage').create();
3
+ var fs = require('fs');
4
+
5
+ if (system.args.length === 3) {
6
+ console.log('Usage: snap.js <some URL> <view port width> <target image name>');
7
+ phantom.exit();
8
+ }
9
+
10
+ var url = system.args[1];
11
+ var image_name = system.args[3];
12
+ var view_port_width = system.args[2];
13
+
14
+ page.viewportSize = { width: view_port_width, height: 1500};
15
+ page.settings = { loadImages: true, javascriptEnabled: true };
16
+
17
+ 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',
18
+
19
+ page.open(url, function(status) {
20
+ if (status === 'success') {
21
+ window.setTimeout(function() {
22
+ console.log('Snapping ' + url + ' at width ' + view_port_width);
23
+ page.render(image_name);
24
+ phantom.exit();
25
+ }, 3000);
26
+ } else {
27
+ console.log('Error with page ' + url);
28
+ phantom.exit();
29
+ }
30
+ });
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Chris Blackburn
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2013-08-24 00:00:00 +01:00
17
+ date: 2013-08-27 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -64,12 +64,18 @@ extensions: []
64
64
  extra_rdoc_files: []
65
65
 
66
66
  files:
67
+ - lib/auto_brewster/cli.rb
68
+ - lib/auto_brewster/middleware.rb
69
+ - lib/auto_brewster/screenshot.rb
70
+ - lib/auto_brewster/server.rb
67
71
  - lib/auto_brewster.rb
68
72
  - bin/autobrewster
73
+ - snap.js
74
+ - BSD-LICENSE.txt
69
75
  has_rdoc: true
70
76
  homepage: https://github.com/madebymade/autobrewster
71
- licenses: []
72
-
77
+ licenses:
78
+ - BSD
73
79
  post_install_message:
74
80
  rdoc_options: []
75
81