dotdiff 2.0.2-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +100 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/dotdiff.gemspec +36 -0
- data/lib/dotdiff.rb +51 -0
- data/lib/dotdiff/command_wrapper.rb +52 -0
- data/lib/dotdiff/comparer.rb +58 -0
- data/lib/dotdiff/element_handler.rb +39 -0
- data/lib/dotdiff/element_meta.rb +45 -0
- data/lib/dotdiff/image/cropper.rb +45 -0
- data/lib/dotdiff/rspec_matcher.rb +17 -0
- data/lib/dotdiff/snapshot.rb +89 -0
- data/lib/dotdiff/version.rb +3 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bc7d6d30ec1acd29d43fdef9b82ba35411e0277f
|
4
|
+
data.tar.gz: cf7af469474460e3d1905cc351dbce3b03c386ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 80d3131f8d56dbb9fbbd6c1b39a35cddf9146138eba8938d1889082cdf4d5f44b01c64a6685b363524fca75b96428a0fa9c9c3e5717bbcf00f6abe5a45b70a6a
|
7
|
+
data.tar.gz: 81422794c94c3e438074014a22e97183f8a07f9321f2eb5dfff532df4a40d168e78c8c5479bc9feaa9713cd1dbb47f3e74d8db3a40e4b8c42c6198852add1dd2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Jon Normington
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# Dotdiff
|
2
|
+
|
3
|
+
Dotdiff is a very basic wrapper around imåge magick compare program which works with both Capybara
|
4
|
+
and RSpec to capture and compare the images with a simple rspec matcher.
|
5
|
+
|
6
|
+
It is now also possible to snapshot a particular element on the page just by using the standard
|
7
|
+
capybara finders - which once the element is found will query the browser for its dimensions and
|
8
|
+
placement on the page and use that metadata to crop using chunky_png from a full page snapshot.
|
9
|
+
|
10
|
+
It can also hide certain elements via executing javascript for elements which can change with
|
11
|
+
different display suchas username or user specific details, but only for full page screenshots.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'dotdiff'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install dotdiff
|
28
|
+
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
### Dependencies
|
33
|
+
First ensure to install image magick binary which is available via apt-get or brew
|
34
|
+
|
35
|
+
In your spec/spec_helper
|
36
|
+
```
|
37
|
+
require 'dotdiff'
|
38
|
+
```
|
39
|
+
|
40
|
+
If you want the rspec_matcher require the following as well
|
41
|
+
```
|
42
|
+
require 'dotdiff/rspec_matcher'
|
43
|
+
```
|
44
|
+
|
45
|
+
### Configuration
|
46
|
+
In an initializer you can configure certain options example shown below within Dotdiff
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
DotDiff.configure do |config|
|
50
|
+
config.image_magick_diff_bin = `which compare`
|
51
|
+
config.pixel_threshold = 120
|
52
|
+
config.image_magick_options = '-metric AE'
|
53
|
+
config.image_store_path = File.join('/home', 'user', 'images')
|
54
|
+
config.xpath_elements_to_hide = ["id('main')"]
|
55
|
+
config.failure_image_path = File.join('/home', 'user', 'failure_comparisions')
|
56
|
+
config.max_wait_time = 2
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
### Spec usage
|
61
|
+
|
62
|
+
For a full page screenshot you can use the below;
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
expect(page).to match_image('HomePage', subdir: 'Normitec')
|
66
|
+
```
|
67
|
+
|
68
|
+
For a specific element screenshot you can use like so;
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
expect(find('#login-form')).to match_image('LoginForm', subdir: 'Normitec')
|
72
|
+
```
|
73
|
+
|
74
|
+
The only difference for the element specific is passing a specific element in the `expect` parameter.
|
75
|
+
|
76
|
+
## Configuration Options
|
77
|
+
|
78
|
+
| Config | Description | Example | Default | Required |
|
79
|
+
|------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------|--------------------------------|----------|
|
80
|
+
| image_magick_diff_bin | Location of the image magick compare binary file | `which compare` | N/A | Yes |
|
81
|
+
| image_store_path | The root path to store the base images | File.join('/home', 'user','images') | nil | Yes |
|
82
|
+
| xpath_elements_to_hide | When taking full page screenshots it will hide elements specified that might change each time and re-shows them after the screenshot. It doesn't use this option for taking screenshots of specific elements. | ["id('main')", "//div[contains(@class, 'formy'])[1]"] | [] | No |
|
83
|
+
| failure_image_path | When a comparison occurs and the perceptual_diff binary returns a failure with the message. It will dump the new image taken for comparison to this directory. If not supplied it will not move the images from the temporary location that it is generated at. | File.join('/home', 'user','failures') | nil | No |
|
84
|
+
| max_wait_time | This is similar to the Capybara#default_max_wait_time if you have a high value such as 10, as its possible that the global xpath_elements_to_hide might not always exist it will wait the full time - therefore you can drop it for the hiding and showing of the elements. In this example it would wait up 20 seconds in total 10 for hiding and 10 seconds for re-showing - that is if the element isn't even going to be present on the page. | 2 | Capybara#default_max_wait_time | No |
|
85
|
+
| pixel_threshold | This validates the output from compare is within your specified threshold | 120 | 100 | No |
|
86
|
+
| image_magick_options | This allows you to pass some custom options to image magick | '-fuzz 10% -metric RSME' | '-fuzz 5% -metric AE' | No |
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jnormington/dotdiff.
|
90
|
+
|
91
|
+
|
92
|
+
## License
|
93
|
+
|
94
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
95
|
+
|
96
|
+
## Whats still to do
|
97
|
+
|
98
|
+
Its not fully completed yet but is usable in its current state.
|
99
|
+
- Improve the message output to extract just the fail line
|
100
|
+
- Add an integration spec
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dotdiff"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/dotdiff.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dotdiff/version'
|
5
|
+
|
6
|
+
is_java = RUBY_PLATFORM == 'java'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = "dotdiff"
|
10
|
+
spec.version = DotDiff::VERSION
|
11
|
+
spec.authors = ["Jon Normington"]
|
12
|
+
spec.email = ["jnormington@users.noreply.github.com"]
|
13
|
+
spec.platform = 'java' if is_java
|
14
|
+
|
15
|
+
spec.summary = "Preceptual diff wrapper for capybara and rspec image regression specs"
|
16
|
+
spec.description = [spec.summary, "which is great for graphs and charts where checking"\
|
17
|
+
"the DOM is either impossible to not worth it."].join(' ')
|
18
|
+
spec.homepage = "https://github.com/jnormington/dotdiff"
|
19
|
+
spec.license = "MIT"
|
20
|
+
|
21
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
if is_java
|
27
|
+
spec.add_runtime_dependency "rmagick4j", '~> 0.4.0'
|
28
|
+
else
|
29
|
+
spec.add_runtime_dependency "rmagick", '~> 2.15'
|
30
|
+
end
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
+
spec.add_development_dependency "capybara", "~> 2.6"
|
36
|
+
end
|
data/lib/dotdiff.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'dotdiff/version'
|
2
|
+
|
3
|
+
require 'shellwords'
|
4
|
+
require 'tmpdir'
|
5
|
+
require 'fileutils'
|
6
|
+
|
7
|
+
require 'dotdiff/command_wrapper'
|
8
|
+
require 'dotdiff/element_handler'
|
9
|
+
|
10
|
+
require 'dotdiff/element_meta'
|
11
|
+
require 'dotdiff/image/cropper'
|
12
|
+
require 'dotdiff/snapshot'
|
13
|
+
require 'dotdiff/comparer'
|
14
|
+
|
15
|
+
module DotDiff
|
16
|
+
class << self
|
17
|
+
attr_accessor :resave_base_image, :failure_image_path,
|
18
|
+
:image_store_path, :overwrite_on_resave, :xpath_elements_to_hide,
|
19
|
+
:max_wait_time
|
20
|
+
|
21
|
+
attr_writer :image_magick_options, :pixel_threshold, :image_magick_diff_bin
|
22
|
+
|
23
|
+
def configure
|
24
|
+
yield self
|
25
|
+
end
|
26
|
+
|
27
|
+
def resave_base_image
|
28
|
+
@resave_base_image ||= false
|
29
|
+
end
|
30
|
+
|
31
|
+
def xpath_elements_to_hide
|
32
|
+
@xpath_elements_to_hide ||= []
|
33
|
+
end
|
34
|
+
|
35
|
+
def image_magick_options
|
36
|
+
@image_magick_options ||= '-fuzz 5% -metric AE'
|
37
|
+
end
|
38
|
+
|
39
|
+
def image_magick_diff_bin
|
40
|
+
@image_magick_diff_bin.to_s.strip
|
41
|
+
end
|
42
|
+
|
43
|
+
def pixel_threshold
|
44
|
+
@pixel_threshold ||= 100
|
45
|
+
end
|
46
|
+
|
47
|
+
def max_wait_time
|
48
|
+
@max_wait_time || Capybara.default_max_wait_time
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
|
3
|
+
module DotDiff
|
4
|
+
class CommandWrapper
|
5
|
+
attr_reader :message
|
6
|
+
|
7
|
+
def run(base_image, new_image, diff_image_path)
|
8
|
+
output = run_command(base_image, new_image, diff_image_path)
|
9
|
+
|
10
|
+
@ran_checks = true
|
11
|
+
|
12
|
+
begin
|
13
|
+
pixels = Float(output)
|
14
|
+
|
15
|
+
if pixels && pixels <= DotDiff.pixel_threshold
|
16
|
+
@failed = false
|
17
|
+
else
|
18
|
+
@failed = true
|
19
|
+
@message = "Images are #{pixels} pixels different"
|
20
|
+
end
|
21
|
+
rescue ArgumentError => e
|
22
|
+
@failed = true
|
23
|
+
@message = output
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def passed?
|
28
|
+
!failed?
|
29
|
+
end
|
30
|
+
|
31
|
+
def failed?
|
32
|
+
@ran_checks && @failed
|
33
|
+
end
|
34
|
+
|
35
|
+
def ran_checks
|
36
|
+
@ran_checks
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
# For the tests
|
42
|
+
def run_command(base_image, new_image, diff_image_path)
|
43
|
+
`#{command(base_image, new_image, diff_image_path)}`.strip
|
44
|
+
end
|
45
|
+
|
46
|
+
def command(base_image, new_image, diff_image_path)
|
47
|
+
"#{DotDiff.image_magick_diff_bin} #{DotDiff.image_magick_options} " \
|
48
|
+
"#{Shellwords.escape(base_image)} #{Shellwords.escape(new_image)} " \
|
49
|
+
"#{Shellwords.escape(diff_image_path)} 2>&1"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module DotDiff
|
2
|
+
class Comparer
|
3
|
+
attr_reader :element, :page, :snapshot
|
4
|
+
|
5
|
+
def initialize(element, page, snapshot)
|
6
|
+
@page = page
|
7
|
+
@element = element
|
8
|
+
@snapshot = snapshot
|
9
|
+
end
|
10
|
+
|
11
|
+
def result
|
12
|
+
if element.is_a?(Capybara::Session)
|
13
|
+
compare_page
|
14
|
+
elsif element.is_a?(Capybara::Node::Base)
|
15
|
+
compare_element
|
16
|
+
else
|
17
|
+
raise ArgumentError, "Unknown element class received: #{element.class.name}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def compare_element(element_meta = ElementMeta.new(page, element))
|
24
|
+
snapshot.capture_from_browser(false, nil)
|
25
|
+
snapshot.crop_and_resave(element_meta)
|
26
|
+
|
27
|
+
if !File.exists?(snapshot.basefile)
|
28
|
+
snapshot.resave_cropped_file
|
29
|
+
[true, snapshot.basefile]
|
30
|
+
else
|
31
|
+
compare(snapshot.cropped_file)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def compare_page
|
36
|
+
snapshot.capture_from_browser
|
37
|
+
|
38
|
+
if !File.exists?(snapshot.basefile)
|
39
|
+
snapshot.resave_fullscreen_file
|
40
|
+
[true, snapshot.basefile]
|
41
|
+
else
|
42
|
+
compare(snapshot.fullscreen_file)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def compare(compare_to_image)
|
47
|
+
result = CommandWrapper.new
|
48
|
+
result.run(snapshot.basefile, compare_to_image, snapshot.diff_file)
|
49
|
+
|
50
|
+
if result.failed? && DotDiff.failure_image_path
|
51
|
+
FileUtils.mkdir_p(snapshot.failure_path)
|
52
|
+
FileUtils.mv(compare_to_image, snapshot.new_file, force: true)
|
53
|
+
end
|
54
|
+
|
55
|
+
[result.passed?, result.message]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module DotDiff
|
2
|
+
class ElementHandler
|
3
|
+
attr_accessor :driver
|
4
|
+
|
5
|
+
def initialize(driver, elements = DotDiff.xpath_elements_to_hide)
|
6
|
+
@driver = driver
|
7
|
+
@elements = elements
|
8
|
+
end
|
9
|
+
|
10
|
+
def hide
|
11
|
+
elements.each do |xpath|
|
12
|
+
if element_exists?(xpath)
|
13
|
+
driver.execute_script("#{js_element(xpath)}.style.visibility = 'hidden'")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def show
|
19
|
+
elements.each do |xpath|
|
20
|
+
if element_exists?(xpath)
|
21
|
+
driver.execute_script("#{js_element(xpath)}.style.visibility = ''")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def js_element(xpath)
|
27
|
+
"document.evaluate(\"#{xpath}\", "\
|
28
|
+
"document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue"
|
29
|
+
end
|
30
|
+
|
31
|
+
def element_exists?(xpath)
|
32
|
+
driver.find(:xpath, xpath, wait: DotDiff.max_wait_time, visible: :all) rescue nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def elements
|
36
|
+
@elements ||= []
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module DotDiff
|
2
|
+
class ElementMeta
|
3
|
+
attr_reader :page, :element_xpath
|
4
|
+
|
5
|
+
def initialize(page, element)
|
6
|
+
@element_xpath = element.path
|
7
|
+
@page = page
|
8
|
+
end
|
9
|
+
|
10
|
+
def rectangle
|
11
|
+
@rectangle ||= Rectangle.new(@page, @element_xpath)
|
12
|
+
end
|
13
|
+
|
14
|
+
class Rectangle
|
15
|
+
attr_reader :rect
|
16
|
+
|
17
|
+
def initialize(page, xpath)
|
18
|
+
@rect = get_rect(page, xpath)
|
19
|
+
end
|
20
|
+
|
21
|
+
def method_missing(name, *args, &block)
|
22
|
+
if %w(x y width height).include?(name.to_s)
|
23
|
+
case name
|
24
|
+
when :x then rect['left']
|
25
|
+
when :y then rect['top']
|
26
|
+
else rect[name.to_s]
|
27
|
+
end
|
28
|
+
else
|
29
|
+
super
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def js_query(xpath)
|
36
|
+
"document.evaluate(\"#{xpath}\", document, null, XPathResult."\
|
37
|
+
"FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.getBoundingClientRect()"
|
38
|
+
end
|
39
|
+
|
40
|
+
def get_rect(page, xpath)
|
41
|
+
page.evaluate_script(js_query(xpath))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rmagick'
|
2
|
+
|
3
|
+
module DotDiff
|
4
|
+
module Image
|
5
|
+
module Cropper
|
6
|
+
def crop_and_resave(element)
|
7
|
+
image = load_image(fullscreen_file)
|
8
|
+
image.crop!(
|
9
|
+
element.rectangle.x,
|
10
|
+
element.rectangle.y,
|
11
|
+
width(element, image),
|
12
|
+
height(element, image)
|
13
|
+
)
|
14
|
+
|
15
|
+
image.write(cropped_file)
|
16
|
+
end
|
17
|
+
|
18
|
+
def load_image(file)
|
19
|
+
Magick::Image.read(file).first
|
20
|
+
end
|
21
|
+
|
22
|
+
def height(element, image)
|
23
|
+
element_height = element.rectangle.height + element.rectangle.y
|
24
|
+
image_height = image.rows
|
25
|
+
|
26
|
+
if element_height > image_height
|
27
|
+
image_height - element.rectangle.y
|
28
|
+
else
|
29
|
+
element.rectangle.height
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def width(element, image)
|
34
|
+
element_width = element.rectangle.width + element.rectangle.x
|
35
|
+
image_width = image.columns
|
36
|
+
|
37
|
+
if element_width > image_width
|
38
|
+
image_width - element.rectangle.x
|
39
|
+
else
|
40
|
+
element.rectangle.width
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rspec/expectations'
|
2
|
+
|
3
|
+
RSpec::Matchers.define :match_image do |filename, opts = {}|
|
4
|
+
match do |page_or_element|
|
5
|
+
options = Hash(opts).merge(filename: filename, page: page)
|
6
|
+
|
7
|
+
snapshot = DotDiff::Snapshot.new(options)
|
8
|
+
comparer = DotDiff::Comparer.new(page_or_element, page, snapshot)
|
9
|
+
|
10
|
+
result, @message = comparer.result
|
11
|
+
result == true
|
12
|
+
end
|
13
|
+
|
14
|
+
failure_message do |page_or_element|
|
15
|
+
"expected #{page_or_element.class} to match image but failed with: #{@message}"
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module DotDiff
|
2
|
+
class Snapshot
|
3
|
+
include Image::Cropper
|
4
|
+
|
5
|
+
attr_reader :base_filename, :subdir, :rootdir, :page, :use_custom_screenshot
|
6
|
+
|
7
|
+
IMAGE_EXT = 'png'.freeze
|
8
|
+
|
9
|
+
def initialize(options = {})
|
10
|
+
opts = { rootdir: DotDiff.image_store_path }.merge(Hash(options))
|
11
|
+
@base_filename = opts[:filename]
|
12
|
+
@subdir = opts[:subdir].to_s
|
13
|
+
@rootdir = opts[:rootdir].to_s
|
14
|
+
@page = opts[:page]
|
15
|
+
@fullscreen = opts[:fullscreen_file]
|
16
|
+
@use_custom_screenshot = opts[:use_custom_screenshot]
|
17
|
+
end
|
18
|
+
|
19
|
+
def fullscreen_file
|
20
|
+
@fullscreen ||= File.join(Dir.tmpdir, subdir, base_filename)
|
21
|
+
end
|
22
|
+
|
23
|
+
def cropped_file
|
24
|
+
@cropped ||= File.join(Dir.tmpdir, subdir, "#{base_filename(false)}_cropped.#{IMAGE_EXT}")
|
25
|
+
end
|
26
|
+
|
27
|
+
def basefile
|
28
|
+
File.join(rootdir, subdir.to_s, base_filename)
|
29
|
+
end
|
30
|
+
|
31
|
+
def base_filename(with_extension=true)
|
32
|
+
filename = File.basename(@base_filename)
|
33
|
+
extension = File.extname(filename)
|
34
|
+
rtn_file = @base_filename
|
35
|
+
|
36
|
+
if with_extension
|
37
|
+
rtn_file = "#{@base_filename}.#{IMAGE_EXT}" if extension.empty?
|
38
|
+
else
|
39
|
+
rtn_file = @base_filename.sub(extension, '') unless extension.empty?
|
40
|
+
end
|
41
|
+
|
42
|
+
rtn_file
|
43
|
+
end
|
44
|
+
|
45
|
+
def failure_path
|
46
|
+
File.join(DotDiff.failure_image_path.to_s, subdir)
|
47
|
+
end
|
48
|
+
|
49
|
+
def new_file
|
50
|
+
File.join(failure_path, "#{base_filename(false)}.new.#{IMAGE_EXT}" )
|
51
|
+
end
|
52
|
+
|
53
|
+
def diff_file
|
54
|
+
File.join(failure_path, "#{base_filename(false)}.diff.#{IMAGE_EXT}")
|
55
|
+
end
|
56
|
+
|
57
|
+
def capture_from_browser(hide_and_show = true, element_handler = ElementHandler.new(page))
|
58
|
+
return fullscreen_file if use_custom_screenshot
|
59
|
+
|
60
|
+
if hide_and_show
|
61
|
+
element_handler.hide
|
62
|
+
page.save_screenshot(fullscreen_file)
|
63
|
+
element_handler.show
|
64
|
+
else
|
65
|
+
page.save_screenshot(fullscreen_file)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def resave_cropped_file
|
70
|
+
resave_base_file(:cropped)
|
71
|
+
end
|
72
|
+
|
73
|
+
def resave_fullscreen_file
|
74
|
+
resave_base_file(:fullscreen)
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def resave_base_file(version)
|
80
|
+
FileUtils.mkdir_p(File.join(DotDiff.image_store_path, subdir))
|
81
|
+
|
82
|
+
if !File.exists?(basefile) || DotDiff.overwrite_on_resave
|
83
|
+
FileUtils.mv(self.send("#{version}_file"), basefile, force: true)
|
84
|
+
else
|
85
|
+
FileUtils.mv(self.send("#{version}_file"), "#{basefile}.r2", force: true)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dotdiff
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.2
|
5
|
+
platform: java
|
6
|
+
authors:
|
7
|
+
- Jon Normington
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: 0.4.0
|
19
|
+
name: rmagick4j
|
20
|
+
prerelease: false
|
21
|
+
type: :runtime
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.4.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1.11'
|
33
|
+
name: bundler
|
34
|
+
prerelease: false
|
35
|
+
type: :development
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '10.0'
|
47
|
+
name: rake
|
48
|
+
prerelease: false
|
49
|
+
type: :development
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '3.0'
|
61
|
+
name: rspec
|
62
|
+
prerelease: false
|
63
|
+
type: :development
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '2.6'
|
75
|
+
name: capybara
|
76
|
+
prerelease: false
|
77
|
+
type: :development
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.6'
|
83
|
+
description: Preceptual diff wrapper for capybara and rspec image regression specs which is great for graphs and charts where checkingthe DOM is either impossible to not worth it.
|
84
|
+
email:
|
85
|
+
- jnormington@users.noreply.github.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- bin/console
|
97
|
+
- bin/setup
|
98
|
+
- dotdiff.gemspec
|
99
|
+
- lib/dotdiff.rb
|
100
|
+
- lib/dotdiff/command_wrapper.rb
|
101
|
+
- lib/dotdiff/comparer.rb
|
102
|
+
- lib/dotdiff/element_handler.rb
|
103
|
+
- lib/dotdiff/element_meta.rb
|
104
|
+
- lib/dotdiff/image/cropper.rb
|
105
|
+
- lib/dotdiff/rspec_matcher.rb
|
106
|
+
- lib/dotdiff/snapshot.rb
|
107
|
+
- lib/dotdiff/version.rb
|
108
|
+
homepage: https://github.com/jnormington/dotdiff
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 2.6.8
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: Preceptual diff wrapper for capybara and rspec image regression specs
|
132
|
+
test_files: []
|