simulacrum 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c310ede91a08d7f1f3dc4a8117b2c819cbae6106
4
- data.tar.gz: 0baa8198be24a7d440e3c18bc1f2cc3dcf7a155c
3
+ metadata.gz: 7687eec5b004b38792cf95b1c4dc725c060f9a00
4
+ data.tar.gz: 794e8cd0f33574cc2290cfcdb407b21a99c5d27e
5
5
  SHA512:
6
- metadata.gz: 73c02e295c5a64c54b5698b9ab635b13949e539f0acbaa246be388b7179c90faf369ea90812e2e7ef0372c66efca1f12a513beba83b7c316efcd72a4d2ffab75
7
- data.tar.gz: 43d26d53bbb8562d4bdf036a45206fd09830dc892661244d848f73537e40ba9e1566201d7d01781fdf4a780ca9956755ea4c5d3611e5444aae8e7b284741ad69
6
+ metadata.gz: 7e570746996c898fdaba924b2f885637640dae8c1734b433942ba1da5fcec94b1860daaa54b2df3aec0138c34be784fae34635d54d44b13994333460415a1a62
7
+ data.tar.gz: 1e356ca93bd87a2483f7e13855ac7eb561520d7e927f06619229fa4e1da97d70b29a37964d7c79d42b2509c05ff81f5d523c5b93eaf5dc94ee6898da6fab1605
data/LICENSE ADDED
File without changes
@@ -1,7 +1,44 @@
1
1
  require 'capybara'
2
- require 'capybara/dsl'
3
2
 
4
3
  module Simulacrum
5
4
  class Browser
5
+ attr_reader :name, :options
6
+
7
+ def initialize(name, options)
8
+ @name = name
9
+ @options = options
10
+ end
11
+
12
+ def use
13
+ register_driver
14
+ Capybara.default_driver = name.to_sym
15
+ end
16
+
17
+ def configure(config)
18
+ puts config.inspect
19
+ end
20
+
21
+ def capture_delay
22
+ @options.capture_delay || Simulacrum.configuration.capture_delay
23
+ end
24
+
25
+ def remote?
26
+ @options.remote.nil? ? false : @options.remote
27
+ end
28
+
29
+ private
30
+
31
+ def driver_options
32
+ options = { desired_capabilities: @options.caps }
33
+ options[:browser] = :remote if remote?
34
+ options[:url] = Simulacrum.configuration.remote_url if remote?
35
+ options
36
+ end
37
+
38
+ def register_driver
39
+ Capybara.register_driver name.to_sym do |app|
40
+ Capybara::Selenium::Driver.new(app, driver_options)
41
+ end
42
+ end
6
43
  end
7
44
  end
@@ -0,0 +1,5 @@
1
+ module Simulacrum
2
+ class BrowserOptions < OpenStruct
3
+
4
+ end
5
+ end
@@ -1,4 +1,4 @@
1
- require_relative 'diff/rmagick'
1
+ require_relative './diff/rmagick'
2
2
 
3
3
  module Simulacrum
4
4
  # The Comparator class is responsible for comparing and handling
@@ -10,11 +10,10 @@ module Simulacrum
10
10
 
11
11
  def initialize(component)
12
12
  @component = component
13
+ @component.render
13
14
  end
14
15
 
15
16
  def test
16
- @candidate = @component.capture_candidate
17
-
18
17
  # If the component has a reference then we should diff the candidate
19
18
  # image against the reference
20
19
  if @component.reference?
@@ -34,6 +33,7 @@ module Simulacrum
34
33
 
35
34
  def pass
36
35
  @component.remove_candidate
36
+ @component.remove_diff
37
37
  true
38
38
  end
39
39
 
@@ -48,12 +48,12 @@ module Simulacrum
48
48
 
49
49
  def perform_diff()
50
50
  @diff = Simulacrum::RmagicDiff.new(@component.reference_path,
51
- @component.candidate_path)
51
+ @component.candidate_path)
52
52
  diff_delta_percent_is_acceptable
53
53
  end
54
54
 
55
55
  def diff_delta_percent_is_acceptable
56
- (@diff.delta * 1000) < @component.acceptable_delta
56
+ @diff.delta_percent < @component.acceptable_delta
57
57
  end
58
58
  end
59
59
  end
@@ -1,32 +1,41 @@
1
- require 'capybara'
1
+ require 'capybara/dsl'
2
2
 
3
3
  module Simulacrum
4
4
  class Component
5
5
  include Capybara::DSL
6
- Capybara.default_driver = :selenium
7
6
 
8
- attr_reader :name
7
+ attr_reader :name, :browser
8
+ attr_accessor :options
9
9
 
10
- def initialize(name, options)
10
+ def initialize(name, browser = nil, options = {})
11
11
  @name = name
12
12
  @options = options
13
+ @browser = browser
13
14
  end
14
15
 
15
16
  # Load up the component url and capture an image, returns a File object
16
- def capture_candidate
17
+ def render
18
+ use_browser
17
19
  ensure_example_path
18
20
  visit(@options.url)
19
- within(capture_selector) do
20
- page.save_screenshot(candidate_path)
21
- end
21
+ page.driver.save_screenshot(candidate_path, options)
22
+ kill_driver
23
+ end
24
+
25
+ def render_with(browser)
26
+ self.class.new(name, browser, @options)
22
27
  end
23
28
 
24
29
  def remove_candidate
25
30
  FileUtils.rm(candidate_path) if candidate?
26
31
  end
27
32
 
33
+ def remove_diff
34
+ FileUtils.rm(diff_path) if diff?
35
+ end
36
+
28
37
  def root_path
29
- File.join(Simulacrum.configuration.images_path, name.to_s)
38
+ File.join(Simulacrum.configuration.images_path, render_path)
30
39
  end
31
40
 
32
41
  def reference_path
@@ -49,12 +58,35 @@ module Simulacrum
49
58
  File.exists?(candidate_path)
50
59
  end
51
60
 
61
+ def diff?
62
+ File.exists?(diff_path)
63
+ end
64
+
52
65
  def acceptable_delta
53
66
  @options.acceptable_delta || Simulacrum.configuration.acceptable_delta
54
67
  end
55
68
 
56
69
  private
57
70
 
71
+ def kill_driver
72
+ if not @browser.nil? and @browser.remote?
73
+ page.driver.quit
74
+ end
75
+ end
76
+
77
+ def render_path
78
+ path = [name.to_s]
79
+ path << @browser.name unless @browser.nil?
80
+ File.join(path.map(&:to_s))
81
+ end
82
+
83
+ def use_browser
84
+ unless @browser.nil?
85
+ @browser.use
86
+ sleep @browser.capture_delay.to_i
87
+ end
88
+ end
89
+
58
90
  def ensure_example_path
59
91
  FileUtils.mkdir_p(root_path)
60
92
  end
@@ -3,7 +3,7 @@ require 'ostruct'
3
3
  module Simulacrum
4
4
  class Configuration
5
5
  attr_reader :images_path, :reference_filename, :candidate_filename,
6
- :diff_filename, :capture_selector, :acceptable_delta
6
+ :diff_filename, :capture_selector, :acceptable_delta, :capture_delay
7
7
 
8
8
  def initialize
9
9
  @config = OpenStruct.new
@@ -29,12 +29,16 @@ module Simulacrum
29
29
  @config.diff_filename || 'diff.png'
30
30
  end
31
31
 
32
- def capture_selector
33
- @config.capture_selector || 'html'
32
+ def capture_delay
33
+ @config.capture_delay || 0
34
34
  end
35
35
 
36
36
  def acceptable_delta
37
37
  @config.acceptable_delta || 0.0
38
38
  end
39
+
40
+ def remote_url
41
+ @config.remote_url
42
+ end
39
43
  end
40
44
  end
@@ -1,16 +1,16 @@
1
- require 'chunky_png'
2
- require_relative '../diff'
3
-
4
- module Simulacrum
5
- class Pdiff < Simulacrum::Diff
6
- include ChunkyPNG::Color
7
-
8
- def compare
9
- a_image = ChunkyPNG::Image.from_file(@a_path)
10
- b_image = ChunkyPNG::Image.from_file(@b_path)
11
- end
12
- end
13
- end
1
+ # require 'chunky_png'
2
+ # require_relative '../diff'
3
+
4
+ # module Simulacrum
5
+ # class Pdiff < Simulacrum::Diff
6
+ # include ChunkyPNG::Color
7
+
8
+ # def compare
9
+ # a_image = ChunkyPNG::Image.from_file(@a_path)
10
+ # b_image = ChunkyPNG::Image.from_file(@b_path)
11
+ # end
12
+ # end
13
+ # end
14
14
 
15
15
  # require 'chunky_png'
16
16
  # include ChunkyPNG::Color
@@ -3,16 +3,27 @@ require_relative '../diff'
3
3
 
4
4
  module Simulacrum
5
5
  class RmagicDiff < Simulacrum::Diff
6
+ def delta_percent
7
+ @delta * 100
8
+ end
9
+
6
10
  private
7
11
 
8
12
  def compare
9
- a_image = Magick::Image.read(@a_path)
10
- b_image = Magick::Image.read(@b_path)
11
- @image, @delta = compare_images(a_image, b_image)
13
+ a = Magick::Image.read(@a_path)
14
+ b = Magick::Image.read(@b_path)
15
+ @image, @delta = compare_images(a, b)
12
16
  end
13
17
 
14
- def compare_images(a_image, b_image)
15
- a_image[0].compare_channel(b_image[0], Magick::MeanSquaredErrorMetric)
18
+ def compare_images(a, b)
19
+ # Calculate the Square Root Mean Squared Error for the comparison of the
20
+ # two images.
21
+ #
22
+ # Gets the color difference for each pixel, and square it, average all the
23
+ # squared differences, then return the square root.
24
+ #
25
+ # http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=17284
26
+ a[0].compare_channel(b[0], Magick::MeanSquaredErrorMetric)
16
27
  end
17
28
  end
18
29
  end
@@ -12,9 +12,5 @@ module Simulacrum
12
12
  def save(path)
13
13
  @image.write(path)
14
14
  end
15
-
16
- def percent_change
17
- (@delta * 1000).round(2)
18
- end
19
15
  end
20
16
  end
@@ -1,17 +1,30 @@
1
- require_relative 'comparator'
1
+ require_relative './comparator'
2
2
 
3
3
  module Simulacrum
4
4
  module Matchers
5
5
  extend RSpec::Matchers::DSL
6
6
 
7
7
  matcher :look_the_same do
8
+ # browser = options[:in_browser]
9
+ # browser.use unless browser.nil?
10
+
8
11
  match do |component|
12
+ component = component
9
13
  comparator = Simulacrum::Comparator.new(component)
14
+
10
15
  case comparator.test
11
16
  when true
12
17
  true
13
18
  when false
14
- fail <<-eos
19
+ fail Simulacrum::Matchers.fail_message(component, comparator)
20
+ when nil
21
+ pending Simulacrum::Matchers.pending_message(component)
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.fail_message(component, comparator)
27
+ <<-eos
15
28
  The pixel change percentage exceeded the maximum threshold of #{component.acceptable_delta}%.
16
29
 
17
30
  There was a #{comparator.diff.percent_change}% pixel difference found between \
@@ -22,9 +35,11 @@ Candidate: #{component.candidate_path}
22
35
  Diff: #{component.diff_path}
23
36
 
24
37
  Please review the diff and resolve manually.
25
- eos
26
- when nil
27
- pending <<-eos
38
+ eos
39
+ end
40
+
41
+ def self.pending_message(component)
42
+ <<-eos
28
43
  No reference image found! New candidate created:
29
44
 
30
45
  #{component.candidate_path}
@@ -34,9 +49,7 @@ No reference image found! New candidate created:
34
49
  - mark it as a reference image by renaming it to 'reference.png'
35
50
  - commit 'reference.png' file to your SCM of choice
36
51
  - rerun this spec making sure it passes using the new reference image
37
- eos
38
- end
39
- end
52
+ eos
40
53
  end
41
54
  end
42
55
  end
@@ -1,25 +1,37 @@
1
1
  require 'ostruct'
2
- require_relative 'browser'
3
- require_relative 'component'
2
+ require_relative './browser'
3
+ require_relative './component'
4
4
 
5
5
  module Simulacrum
6
- # Rspec utility methods for defining components, browser environments and
6
+ # Rspec utility methods for defining components, browser environments
7
7
  module Methods
8
8
  def component(name, &block)
9
9
  options = OpenStruct.new
10
10
  yield options
11
- component = Simulacrum::Component.new(name, options)
11
+ component = Simulacrum::Component.new(name, nil, options)
12
12
  Simulacrum.components[name] = component
13
-
14
- subject do
15
- component
16
- end
13
+ subject { component }
14
+ let(:component) { component }
17
15
  end
18
16
 
19
- def configure_browser(name, &block)
17
+ def browser(name, &block)
20
18
  options = OpenStruct.new
19
+ options.caps = {}
21
20
  yield options
22
- @browser = Simulacrum::Browser.new(name, options)
21
+ Simulacrum.browsers[name] = Simulacrum::Browser.new(name, options)
22
+ let(name.to_sym) { browser }
23
+ end
24
+
25
+ def use_browser(name, extra_config = {})
26
+ previous_browser = Simulacrum.current_browser
27
+ current_browser = Simulacrum.browsers[name]
28
+ current_browser.configure(extra_config)
29
+ Simulacrum.current_browser = current_browser
30
+ subject { component.render_with(current_browser) }
31
+ after(:each) {
32
+ Simulacrum.current_browser = previous_browser
33
+ component.render_with(previous_browser)
34
+ }
23
35
  end
24
36
  end
25
37
  end
@@ -1,5 +1,5 @@
1
1
  # Package information
2
2
  module Simulacrum
3
3
  PACKAGE = 'simulacrum'
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
  end
data/lib/simulacrum.rb CHANGED
@@ -1,17 +1,36 @@
1
1
  require 'ostruct'
2
- require_relative 'Simulacrum/methods'
3
- require_relative 'Simulacrum/matchers'
4
- require_relative 'Simulacrum/configuration'
2
+ require 'capybara'
3
+ require_relative './simulacrum/methods'
4
+ require_relative './simulacrum/matchers'
5
+ require_relative './simulacrum/configuration'
5
6
 
6
7
  # Gem module
7
8
  module Simulacrum
9
+ @current_browser = nil
10
+ @browsers = {}
8
11
  @components = {}
9
12
  @configuration = Simulacrum::Configuration.new
10
13
 
14
+ Capybara.configure do |config|
15
+ config.default_driver = :selenium
16
+ end
17
+
11
18
  def self.components
12
19
  @components
13
20
  end
14
21
 
22
+ def self.browsers
23
+ @browsers
24
+ end
25
+
26
+ def self.current_browser
27
+ @current_browser
28
+ end
29
+
30
+ def self.current_browser=(browser)
31
+ @current_browser = browser
32
+ end
33
+
15
34
  def self.configuration
16
35
  @configuration
17
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simulacrum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Morris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-26 00:00:00.000000000 Z
11
+ date: 2014-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capybara
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: selenium-webdriver
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: coveralls
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -173,6 +187,7 @@ extensions: []
173
187
  extra_rdoc_files: []
174
188
  files:
175
189
  - lib/simulacrum/browser.rb
190
+ - lib/simulacrum/browser_options.rb
176
191
  - lib/simulacrum/comparator.rb
177
192
  - lib/simulacrum/component.rb
178
193
  - lib/simulacrum/configuration.rb
@@ -184,6 +199,7 @@ files:
184
199
  - lib/simulacrum/version.rb
185
200
  - lib/simulacrum.rb
186
201
  - README.md
202
+ - LICENSE
187
203
  - spec/fixtures/a.png
188
204
  - spec/fixtures/a2.png
189
205
  - spec/spec_helper.rb