gatling 1.0.8 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -11,3 +11,7 @@ spec/support/ready_candidate_ref/temp/*
11
11
  reports/*
12
12
  vendor/*
13
13
  /.rbenv*
14
+ .rakeTasks
15
+ *.iml
16
+ *.ipr
17
+ *.iws
data/README.rdoc CHANGED
@@ -8,7 +8,7 @@ An integrated visual RSpec matcher which makes real visual testing easy.
8
8
 
9
9
  gem install gatling
10
10
 
11
- In spec_helper.rb :
11
+ In spec_helper.rb:
12
12
 
13
13
  #main app
14
14
  require 'gatling'
@@ -32,13 +32,16 @@ Also created are subfolders:
32
32
 
33
33
  === Sleep_between_tries - sets the sleep time (in seconds) between match tries (requires max_no_tries > 1). Defaults to 0.5
34
34
 
35
+ === browser_folders - *Currently only available with Selenium-Webdriver / Capybara* - create reference folders based on the current Selenium driver's browser. Allows for cross-browser visual testing.
36
+
35
37
  === Configuration settings are set with the following:
36
38
 
37
- Gatling.config do |setting|
38
- Gatling.reference_image_path = 'my_custom_path'
39
- Gatling.max_no_tries = 3
40
- Gatling.sleep_between_tries = 0.7
41
- end
39
+ Gatling.config do |setting|
40
+ Gatling.reference_image_path = 'my_custom_path'
41
+ Gatling.max_no_tries = 3
42
+ Gatling.sleep_between_tries = 0.7
43
+ Gatling.browser_folders = true
44
+ end
42
45
 
43
46
  GATLING::CONFIGURATION will be depreciated in future versions.
44
47
  -------------------------------------
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ require File.expand_path('spec/spec_helper.rb')
7
7
  task :default => :full_build
8
8
 
9
9
  desc "full build, run all the tests"
10
- task :full_build => [:unit, :acceptance]
10
+ task :full_build => [:unit,:acceptance]
11
11
 
12
12
  desc "Run unit tests"
13
13
  RSpec::Core::RakeTask.new(:unit) do |t|
data/lib/gatling.rb CHANGED
@@ -7,6 +7,7 @@ require 'gatling/image'
7
7
  require 'gatling/comparison'
8
8
  require 'gatling/capture_element'
9
9
 
10
+
10
11
  #TODO: Helpers for cucumber
11
12
  #TODO: Make directories as needed
12
13
 
@@ -14,13 +15,12 @@ module Gatling
14
15
 
15
16
  class << self
16
17
 
17
- attr_accessor :reference_image_path, :max_no_tries, :sleep_between_tries
18
+ attr_accessor :reference_image_path, :max_no_tries, :sleep_between_tries, :browser_folders
18
19
 
19
20
  def matches?(expected_reference_filename, actual_element)
20
21
 
21
22
  expected_reference_file = (File.join(Gatling::Configuration.path(:reference), expected_reference_filename))
22
23
 
23
-
24
24
  if Gatling::Configuration.trainer_toggle
25
25
  actual_image = Gatling::ImageFromElement.new(actual_element, expected_reference_filename)
26
26
  save_image_as_reference(actual_image)
@@ -35,26 +35,26 @@ module Gatling
35
35
  comparison = compare_until_match(actual_element, expected_reference_filename, Gatling::Configuration.max_no_tries)
36
36
  matches = comparison.matches?
37
37
  if !matches
38
- comparison.actual_image.save(:as => :candidate)
38
+ comparison.actual_image.save(:candidate)
39
39
  save_image_as_diff(comparison.diff_image)
40
40
  end
41
41
  matches
42
42
  end
43
43
  end
44
44
 
45
- def compare_until_match actual_element, expected_reference_filename, max_no_tries
46
- tries = max_no_tries
45
+ def compare_until_match actual_element, expected_reference_filename, max_no_tries = Gatling::Configuration.max_no_tries, sleep_time = Gatling::Configuration.sleep_between_tries
47
46
  try = 0
48
47
  match = false
49
48
  expected_image = Gatling::ImageFromFile.new(expected_reference_filename)
50
49
  comparison = nil
51
- while !match && try < tries
50
+ while !match && try < max_no_tries
52
51
  actual_image = Gatling::ImageFromElement.new(actual_element, expected_reference_filename)
53
- comparison = Gatling::Comparison.new(expected_image, actual_image)
52
+ comparison = Gatling::Comparison.new(actual_image, expected_image)
54
53
  match = comparison.matches?
55
54
  if !match
56
- sleep 0.5
55
+ sleep sleep_time
57
56
  try += 1
57
+ #TODO: Send to logger instead of puts
58
58
  puts "Tried to match #{try} times"
59
59
  end
60
60
  end
@@ -62,15 +62,15 @@ module Gatling
62
62
  end
63
63
 
64
64
  def save_image_as_diff(image)
65
- image.save(:as => :diff)
65
+ image.save(:diff)
66
66
  raise "element did not match #{image.file_name}. A diff image: #{image.file_name} was created in " +
67
- "#{File.join(Gatling::Configuration.path(:diff),image.file_name)}. " +
68
- "A new reference #{File.join(Gatling::Configuration.path(:candidate),image.file_name)} can be used to fix the test"
67
+ "#{image.path(:diff)} " +
68
+ "A new reference #{image.path(:candidate)} can be used to fix the test"
69
69
  end
70
70
 
71
71
  def save_image_as_candidate(image)
72
- image.save :as => :candidate
73
- raise "The design reference #{image.file_name} does not exist, #{image.path} " +
72
+ image.save(:candidate)
73
+ raise "The design reference #{image.file_name} does not exist, #{image.path(:candidate)} " +
74
74
  "is now available to be used as a reference. Copy candidate to root reference_image_path to use as reference"
75
75
  end
76
76
 
@@ -78,7 +78,7 @@ module Gatling
78
78
  if image.exists?
79
79
  puts "#{image.path} already exists. reference image was not overwritten. please delete the old file to update using trainer"
80
80
  else
81
- image.save(:as => :reference)
81
+ image.save(:reference)
82
82
  puts "Saved #{image.path} as reference"
83
83
  end
84
84
  end
@@ -1,3 +1,4 @@
1
+ require 'fileutils'
1
2
  require_relative 'image_wrangler'
2
3
 
3
4
  module Gatling
@@ -19,14 +20,17 @@ module Gatling
19
20
  end
20
21
 
21
22
  def take_screenshot
22
- temp_dir = "#{@reference_image_path}/temp"
23
+ temp_dir = File.join(@reference_image_path, 'temp')
24
+ FileUtils.mkdir_p(temp_dir) unless File.exists?(temp_dir)
23
25
  #captures the uncropped full screen
24
26
  begin
25
- Capybara.page.driver.browser.save_screenshot("#{temp_dir}/temp.png")
26
- temp_screenshot = Magick::Image.read("#{temp_dir}/temp.png").first
27
+ temp_screenshot_filename = File.join(temp_dir, "temp-#{Process.pid}.png")
28
+ Capybara.page.driver.browser.save_screenshot(temp_screenshot_filename)
29
+ temp_screenshot = Magick::Image.read(temp_screenshot_filename).first
27
30
  rescue
28
31
  raise "Could not save screenshot to #{temp_dir}. Please make sure you have permission"
29
32
  end
30
33
  end
31
34
  end
32
- end
35
+ end
36
+
@@ -6,16 +6,53 @@ module Gatling
6
6
  def initialize actual_image, expected_image
7
7
  @actual_image = actual_image
8
8
  @expected_image = expected_image
9
- @comparison = @actual_image.image.compare_channel(@expected_image.image, Magick::MeanAbsoluteErrorMetric)
9
+ @comparison = compare_image
10
10
  @match = @comparison[1] == 0.0
11
- if !@matches
12
- @diff_image =Gatling::Image.new(@comparison.first, @expected_image.file_name)
13
- end
11
+ @diff_image =Gatling::Image.new(@comparison.first, @expected_image.file_name) unless @matches
14
12
  end
15
13
 
16
14
  def matches?
17
15
  @match
18
16
  end
19
17
 
18
+ def compare_image
19
+ compare_images_with_same_size? ? compare_images_with_same_size : compare_images_with_different_size
20
+ end
21
+
22
+ def compare_images_with_same_size
23
+ images_to_compare = prep_images_for_comparison
24
+ images_to_compare.first.compare_channel(images_to_compare.last, Magick::MeanAbsoluteErrorMetric)
25
+ end
26
+
27
+ def compare_images_with_different_size
28
+ row = [@actual_image.image.rows, @expected_image.image.rows].max
29
+ column = [@actual_image.image.columns, @expected_image.image.columns].max
30
+
31
+ images_to_compare = prep_images_for_comparison do |image|
32
+ expanded_image = image.extent(column, row)
33
+ expanded_image.background_color = 'white'
34
+ expanded_image
35
+ end
36
+ images_to_compare.first.compare_channel(images_to_compare.last, Magick::MeanAbsoluteErrorMetric)
37
+ end
38
+
39
+ def compare_images_with_same_size?
40
+ @actual_image.image.rows == @expected_image.image.rows && @actual_image.image.columns == @expected_image.image.columns
41
+ end
42
+
43
+ def prep_images_for_comparison
44
+ [
45
+ @actual_image,
46
+ @expected_image,
47
+ ].collect do |gatling_image|
48
+ image = gatling_image.image.clone
49
+ image = yield image if block_given?
50
+
51
+ # Important: ensure the image 0,0 is reset to the top-left of the image before comparison
52
+ image.offset = 0
53
+ image
54
+ end
55
+ end
56
+
20
57
  end
21
58
  end
@@ -1,14 +1,16 @@
1
+ require 'logger'
2
+
1
3
  module Gatling
2
4
  module Configuration
3
5
 
4
6
  class << self
5
7
 
6
- attr_accessor :reference_image_path, :trainer_toggle, :max_no_tries, :sleep_between_tries
8
+ attr_accessor :reference_image_path, :trainer_toggle, :max_no_tries, :sleep_between_tries, :browser_folders
7
9
 
8
10
  attr_reader :paths
9
11
 
10
12
  def reference_image_path
11
- Gatling.reference_image_path || @reference_image_path ||= set_default_path
13
+ @reference_image_path ||= construct_path
12
14
  end
13
15
 
14
16
  def max_no_tries
@@ -21,9 +23,9 @@ module Gatling
21
23
 
22
24
  def path(type)
23
25
  paths = Hash[:reference => reference_image_path,
24
- :candidate => File.join(reference_image_path, 'candidate'),
25
- :diff => File.join(reference_image_path, 'diff'),
26
- :temp => File.join(reference_image_path, 'temp')]
26
+ :candidate => File.join(reference_image_path, 'candidate'),
27
+ :diff => File.join(reference_image_path, 'diff'),
28
+ :temp => File.join(reference_image_path, 'temp')]
27
29
  if paths.keys.include? type
28
30
  return paths[type]
29
31
  else
@@ -49,16 +51,51 @@ module Gatling
49
51
  end
50
52
 
51
53
 
52
- def set_default_path
54
+ def construct_path
53
55
  private
56
+ reference_image_path = user_set_reference_path || default_reference_path
57
+ reference_image_path = reference_path_with_browser_folders(reference_image_path) if browser_folders
58
+ reference_image_path
59
+ end
60
+
61
+ def user_set_reference_path
62
+ Gatling.reference_image_path
63
+ end
64
+
65
+ def default_reference_path
66
+ begin
67
+ reference_image_path = File.join(Rails.root, 'spec/reference_images')
68
+ rescue
69
+ reference_image_path = 'spec/reference_images'
70
+ puts "Currently defaulting to #{@reference_image_path}. Overide this by setting Gatling.reference_image_path=[refpath]"
71
+ end
72
+ reference_image_path
73
+ end
74
+
75
+ def reference_path_with_browser_folders path
54
76
  begin
55
- @reference_image_path ||= File.join(Rails.root, 'spec/reference_images')
77
+ reference_images_path = File.join(path, browser)
56
78
  rescue
57
- @reference_image_path = 'spec/reference_images'
58
- puts "Currently defaulting to #{@reference_image_path}. Overide this by setting Gatling::Configuration.reference_image_path=[refpath]"
79
+ reference_images_path = path
59
80
  end
60
- @reference_image_path
81
+ reference_images_path
61
82
  end
83
+
84
+ def browser_folders
85
+ Gatling.browser_folders || @browser_folders ||= false
86
+ end
87
+
88
+ def browser
89
+ begin
90
+ browser = Capybara.page.driver.browser.browser
91
+ rescue
92
+ browser = Selenium.page.driver.browser.browser
93
+ rescue
94
+ raise "Currently custom folders are only supported by Capybara. ENV variables are coming."
95
+ end
96
+ browser.to_s
97
+ end
98
+
62
99
  end
63
100
 
64
101
  end
data/lib/gatling/image.rb CHANGED
@@ -14,15 +14,18 @@ module Gatling
14
14
  end
15
15
 
16
16
  def save type
17
- path = Gatling::Configuration.path(type[:as])
18
- FileUtils::mkdir_p(path) unless File.exists?(path)
19
- @path = File.join(path, @file_name)
17
+ @path = path(type)
18
+ FileUtils::mkdir_p(File.dirname(@path)) unless File.exists?(@path)
20
19
  @image.write @path
21
20
  @path
22
21
  end
23
22
 
24
23
  def exists?
25
- File.exists?(File.join(Gatling::Configuration.path(:reference), @file_name))
24
+ File.exists?(path)
25
+ end
26
+
27
+ def path type = :reference
28
+ @path = File.join(Gatling::Configuration.path(type), @file_name)
26
29
  end
27
30
 
28
31
  end
@@ -34,7 +37,7 @@ module Gatling
34
37
 
35
38
  @image = Gatling::CaptureElement.new(element).capture
36
39
  end
37
-
40
+
38
41
  #TODO: make save a relevant subclass method
39
42
  end
40
43
 
@@ -42,13 +45,11 @@ module Gatling
42
45
 
43
46
  def initialize file_name
44
47
  super(image, file_name)
45
-
46
- @image = Magick::Image.read(File.join(Gatling::Configuration.path(:reference), @file_name)).first
48
+ @image = Magick::Image.read(path).first
47
49
  end
48
50
 
49
51
  #TODO: make save a relevant subclass method
50
-
52
+
51
53
  end
52
54
  end
53
55
 
54
-
@@ -1,6 +1,6 @@
1
1
  require 'gatling'
2
2
 
3
- RSpec::Matchers.define :look_like do |expected|
3
+ RSpec::Matchers.define:look_like do |expected|
4
4
  match do |actual|
5
5
  compare = Gatling.matches?(expected, actual)
6
6
  end
@@ -1,3 +1,3 @@
1
1
  module Gatling
2
- VERSION = "1.0.8"
2
+ VERSION = "1.0.9"
3
3
  end
@@ -8,12 +8,12 @@ describe 'Gatling' do
8
8
  end
9
9
 
10
10
  before(:each) do
11
- @ref_path = Gatling::Configuration.reference_image_path = File.join(spec_support_root, 'ref_path')
11
+ @ref_path = Gatling.reference_image_path = File.join(spec_support_root, 'ref_path')
12
12
  end
13
13
 
14
14
  after(:each) do
15
15
  remove_refs(@ref_path)
16
- Gatling::Configuration.trainer_toggle = false
16
+ config_clean_up
17
17
  end
18
18
 
19
19
  describe 'Gatling, when no reference image exists' do
@@ -42,25 +42,37 @@ describe 'Gatling' do
42
42
  Gatling.matches?("black.png", black_element).should be_true
43
43
  end
44
44
 
45
- it 'will return false, creates new diff and candidate images if the images are different' do
46
- red_element = element_for_spec('#red')
47
- expected_error = "element did not match #{"black.png"}. " +
48
- "A diff image: #{"black.png"} was created in #{@ref_path}/diff/#{"black.png"}. " +
49
- "A new reference #{@ref_path}/candidate/#{"black.png"} can be used to fix the test"
45
+ describe 'between different images of the same size' do
50
46
 
51
- expect {Gatling.matches?("black.png", red_element)}.should raise_error(RuntimeError, expected_error)
47
+ it 'will return false, creates new diff and candidate images' do
48
+ red_element = element_for_spec('#red')
49
+ expected_error = "element did not match #{"black.png"}. " +
50
+ "A diff image: #{"black.png"} was created in #{@ref_path}/diff/#{"black.png"} " +
51
+ "A new reference #{@ref_path}/candidate/#{"black.png"} can be used to fix the test"
52
+
53
+ expect {Gatling.matches?("black.png", red_element)}.should raise_error(RuntimeError, expected_error)
54
+
55
+ File.exists?(File.join(@ref_path,'diff', "black.png")).should be_true
56
+ File.exists?(File.join(@ref_path,'candidate', "black.png")).should be_true
57
+ end
52
58
 
53
- File.exists?(File.join(@ref_path,'diff', "black.png")).should be_true
54
- File.exists?(File.join(@ref_path,'candidate', "black.png")).should be_true
55
59
  end
56
60
 
57
- # it 'will try to match a specified amount of time before failing' do
58
- # red_element = element_for_spec('#red')
59
- # Gatling.should_receive(:matches?).and_return(false)
60
- # Gatling.should_receive(:try_until_match)
61
- # Gatling.matches?("black.png", red_element)
62
- # end
61
+ describe 'between images of the different size' do
62
+
63
+ it 'will return false, creates new diff and candidate images' do
64
+ red_element = element_for_spec('#differentSize')
65
+ expected_error = "element did not match #{"black.png"}. " +
66
+ "A diff image: #{"black.png"} was created in #{@ref_path}/diff/#{"black.png"} " +
67
+ "A new reference #{@ref_path}/candidate/#{"black.png"} can be used to fix the test"
63
68
 
69
+ expect {Gatling.matches?("black.png", red_element)}.should raise_error(RuntimeError, expected_error)
70
+
71
+ File.exists?(File.join(@ref_path,'diff', "black.png")).should be_true
72
+ File.exists?(File.join(@ref_path,'candidate', "black.png")).should be_true
73
+ end
74
+
75
+ end
64
76
  end
65
77
 
66
78
  describe 'Gatling trainer toggle' do
@@ -79,7 +91,7 @@ describe 'Gatling' do
79
91
  it 'will warn if a reference already exists and not overwrite it' do
80
92
  create_square_image(@ref_path, 'black')
81
93
  Gatling::Configuration.trainer_toggle = true
82
- expected_message = " already exists. reference image was not overwritten. " +
94
+ expected_message = "#{File.join(@ref_path,"black.png")} already exists. reference image was not overwritten. " +
83
95
  "please delete the old file to update using trainer"
84
96
  black_element = element_for_spec
85
97
  reference_file_ctime = File.ctime(File.join(@ref_path,"black.png"))
@@ -91,4 +103,16 @@ describe 'Gatling' do
91
103
  reference_file_ctime.eql?(File.ctime(File.join(@ref_path, "black.png"))).should be_true
92
104
  end
93
105
  end
106
+
107
+ describe 'Gatling browser folders' do
108
+
109
+ it 'should set image path according to the driver\'s browser' do
110
+ Gatling.browser_folders = true
111
+ Gatling.reference_image_path = '/some/random/path'
112
+ Gatling::Configuration.reference_image_path.should == '/some/random/path/firefox'
113
+ Gatling.browser_folders = false
114
+ end
115
+ end
116
+
117
+
94
118
  end
@@ -3,14 +3,14 @@ include Capybara::DSL
3
3
 
4
4
  describe 'rspec matcher' do
5
5
 
6
- before(:all) do
6
+ before(:each) do
7
7
  @black_box = 'black.png'
8
- @ref_path = Gatling::Configuration.reference_image_path = File.join(spec_support_root, 'ref_path')
8
+ @ref_path = Gatling.reference_image_path = File.join(spec_support_root, 'ref_path')
9
9
  create_images_for_web_page
10
10
  end
11
11
 
12
12
  after(:each) do
13
- Gatling::Configuration.trainer_toggle = false
13
+ config_clean_up
14
14
  remove_refs(@ref_path)
15
15
  end
16
16
 
@@ -18,11 +18,11 @@ describe 'rspec matcher' do
18
18
 
19
19
  it 'will pass if images matches reference' do
20
20
  create_square_image(@ref_path, 'black')
21
- black_element = element_for_spec("#black")
21
+ black_element = element_for_spec("#black")
22
22
  black_element.should look_like("black.png")
23
23
  end
24
24
 
25
- it 'will fail if images dosent matches reference' do
25
+ it "will fail if images doesn't matches reference" do
26
26
  create_square_image(@ref_path, 'black')
27
27
  red_element = element_for_spec("#red")
28
28
  expect{red_element.should look_like(@black_box)}.should raise_error
data/spec/capture_spec.rb CHANGED
@@ -1,23 +1,54 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Gatling::CaptureElement do
4
-
5
- before do
6
4
 
5
+ before :each do
7
6
  capybara_node = mock (Capybara::Node::Element)
8
- Gatling::Configuration.reference_image_path="./"
9
- Gatling::CaptureElement.new capybara_node, capybara_node
7
+ Gatling::Configuration.should_receive(:reference_image_path).and_return("./")
8
+ @capture_element = Gatling::CaptureElement.new capybara_node, capybara_node
10
9
  end
11
10
 
12
-
11
+ after :each do
12
+ config_clean_up
13
+ end
14
+
15
+ describe 'take_screenshot' do
16
+ before do
17
+ @webdriver = double('webdriver')
18
+ Capybara.stub_chain(:page, :driver, :browser).and_return(@webdriver)
19
+
20
+ @expected_temp_screenshot_file_pattern = /.*\/temp\/temp-\d+.png/
21
+ end
22
+
23
+ it 'should create temp directory when it does not exist' do
24
+ @webdriver.stub!(:save_screenshot)
25
+ Magick::Image.stub!(:read).and_return([])
26
+
27
+ File.stub!(:'exists?').and_return(false)
28
+ FileUtils.should_receive(:mkdir_p).with('./temp')
29
+
30
+ @capture_element.take_screenshot
31
+ end
32
+
33
+
34
+ it 'should work when Gatling is called concurrently from multiple processes' do
35
+ @webdriver.should_receive(:save_screenshot).with(@expected_temp_screenshot_file_pattern)
36
+ Magick::Image.should_receive(:read).with(@expected_temp_screenshot_file_pattern).and_return([])
37
+
38
+ @capture_element.take_screenshot
39
+ end
40
+ end
41
+
42
+
13
43
 
14
44
  # it 'should exclude a specified element from capture' do
15
- # element_to_capture =
45
+ # element_to_capture =
16
46
 
17
- # element_to_exclude = 'womething.child'
47
+ # element_to_exclude = 'womething.child'
18
48
  # # @cropped_and_censored_element = (element_to_capture, element_to_exclude).exclude
19
49
  # @cropped_and_censored_element.should_not equal(@element_to_capture)
20
50
  # @cropped_and_censored_element.should_not equal(@element_to_exclude)
21
51
  # end
22
52
 
23
- end
53
+ end
54
+
@@ -25,8 +25,46 @@ describe Gatling::Comparison do
25
25
  end
26
26
 
27
27
  describe 'Diff images' do
28
- it 'will give us a diff' do
29
- Gatling::Comparison.new(@apple, @orange).diff_image.class.should == Gatling::Image
28
+ describe 'for two images with the same size' do
29
+ it 'will be generated' do
30
+ Gatling::Comparison.new(@apple, @orange).diff_image.class.should == Gatling::Image
31
+ end
32
+ end
33
+
34
+ describe 'for two images with different sizes' do
35
+ before do
36
+ @apple_image = Magick::Image.new(30,300) { self.background_color = "green" }
37
+ @orange_image = Magick::Image.new(80,100) { self.background_color = "orange" }
38
+
39
+ @apple = Gatling::Image.new(@apple_image, "apple.png")
40
+ @orange = Gatling::Image.new(@orange_image, "orange.png")
41
+
42
+ @diff_image = Gatling::Comparison.new(@apple, @orange).diff_image
43
+ end
44
+
45
+ it 'will be generated' do
46
+ @diff_image.class.should == Gatling::Image
47
+ end
48
+
49
+ it 'will be extended to cover the difference in both images' do
50
+ @diff_image.image.columns.should eql 80
51
+ @diff_image.image.rows.should eql 300
52
+ end
53
+
54
+ describe 'with two different offset' do
55
+ before do
56
+ @apple_image.offset = 50
57
+ @orange_image.offset = 2
58
+
59
+ @diff_image = Gatling::Comparison.new(@apple, @orange).diff_image
60
+ end
61
+
62
+ it 'will remove image offset from the diff image' do
63
+ @diff_image.image.offset.should eql 0
64
+ end
65
+
66
+ end
67
+
30
68
  end
31
69
  end
32
70
  end
@@ -2,15 +2,14 @@ require 'spec_helper'
2
2
 
3
3
  describe Gatling::Configuration do
4
4
 
5
- after :each do
6
- Gatling::Configuration.reference_image_path = nil
7
- Gatling.reference_image_path = nil
8
- end
9
5
 
10
6
  describe "#reference_image_path" do
11
7
 
8
+ after :each do
9
+ config_clean_up
10
+ end
12
11
 
13
- describe "Without Rails" do
12
+ describe "without Rails" do
14
13
  it "should default to './spec/reference_images' when not in a rails environment" do
15
14
  Gatling::Configuration.reference_image_path.should eql("spec/reference_images")
16
15
  end
@@ -36,10 +35,55 @@ describe Gatling::Configuration do
36
35
  end
37
36
 
38
37
  it "should be overrideable in a rails environment" do
39
- Gatling::Configuration.reference_image_path = "my custom path"
38
+ subject.reference_image_path = "my custom path"
40
39
  Gatling::Configuration.reference_image_path.should eql("my custom path")
41
40
  end
42
41
 
42
+ it 'should return the directory for a type of image' do
43
+ Gatling::Configuration.reference_image_path = "a_path"
44
+ subject.path(:temp).should == 'a_path/temp'
45
+ end
46
+
47
+ it 'should thrown an error when you ask for the path of an unknown image type' do
48
+ expect { Gatling::Configuration.path(:unknown)}.should raise_error "Unkown image type 'unknown'"
49
+ end
50
+
51
+ end
52
+
53
+ describe "creating custom reference folders" do
54
+
55
+ before :each do
56
+ Gatling.reference_image_path = '/some/ref/path'
57
+ end
58
+
59
+ it "should default to custom folders off" do
60
+ subject.browser_folders.should == false
61
+ end
62
+
63
+ it "should allow setting custom folders on" do
64
+ Gatling::Configuration.browser_folders = true
65
+ subject.browser_folders.should == true
66
+ end
67
+
68
+
69
+ it "should set reference_image_path to default when browser can\'t be found" do
70
+ subject.browser_folders = true
71
+ Capybara.page.driver.browser.should_receive(:browser).at_least(:once).and_raise(StandardError.new)
72
+ subject.reference_image_path.should == '/some/ref/path'
73
+ end
74
+
75
+ it "should create custom folder for each browser according to ENV" do
76
+ pending
77
+ end
78
+
79
+ it "should set the image reference path for each browser according to selenium driver if no ENV is set" do
80
+ subject.browser_folders = true
81
+ subject.stub!(:browser).and_return('chrome')
82
+ subject.reference_image_path.should == '/some/ref/path/chrome'
83
+ subject.browser_folders = false
84
+ end
85
+
86
+
43
87
  end
44
88
  end
45
89
 
@@ -75,16 +119,6 @@ describe Gatling::Configuration do
75
119
  end
76
120
  end
77
121
 
78
- describe 'paths' do
79
- it 'should return the directory for a type of image' do
80
- Gatling::Configuration.reference_image_path = "a_path"
81
- subject.path(:temp).should == 'a_path/temp'
82
- end
83
-
84
- it 'should thrown an error when you ask for the path of an unknown image type' do
85
- expect { Gatling::Configuration.path(:unknown)}.should raise_error "Unkown image type 'unknown'"
86
- end
87
- end
88
122
 
89
123
  describe "#max_no_tries" do
90
124
 
@@ -112,17 +146,40 @@ describe Gatling::Configuration do
112
146
 
113
147
  describe "settings" do
114
148
 
115
- it "should accept a block of settings and parse them correctly" do
116
- Gatling.config do |setting|
117
- Gatling.reference_image_path = 'custom_path'
118
- Gatling.max_no_tries = 3
119
- Gatling.sleep_between_tries = 0.7
149
+ describe "should accept a block of settings and parse them correctly" do
150
+
151
+ it "for reference_image_path" do
152
+ Gatling.config do |setting|
153
+ Gatling.reference_image_path = 'custom_path'
154
+ end
155
+ subject.reference_image_path.should eql 'custom_path'
156
+ end
157
+
158
+ it "for max_no_tries" do
159
+ Gatling.config do |setting|
160
+ Gatling.max_no_tries = 3
161
+ end
162
+ subject.max_no_tries.should eql 3
120
163
  end
121
- subject.reference_image_path.should eql 'custom_path'
122
- subject.max_no_tries.should eql 3
123
- subject.sleep_between_tries.should eql 0.7
124
- end
125
164
 
165
+ it "sleep_between_tries" do
166
+ Gatling.config do |setting|
167
+ Gatling.sleep_between_tries = 0.7
168
+ end
169
+ subject.sleep_between_tries.should eql 0.7
170
+ end
171
+
172
+ it "browser_folders" do
173
+ Gatling.config do |setting|
174
+ Gatling.browser_folders = true
175
+ end
176
+ subject.browser_folders.should eql true
177
+ end
178
+
179
+ end
126
180
  end
127
181
 
182
+
183
+
184
+
128
185
  end
data/spec/gatling_spec.rb CHANGED
@@ -4,15 +4,9 @@ include Capybara::DSL
4
4
  describe Gatling do
5
5
 
6
6
  before :all do
7
- @spec_support_root = spec_support_root
8
7
  @box = 'box'
9
8
  @black_box = 'black.png'
10
9
  @red_box = 'red.png'
11
- @ref_path = Gatling::Configuration.reference_image_path = './ref_path'
12
- end
13
-
14
- after :each do
15
- Gatling::Configuration.trainer_toggle = false
16
10
  end
17
11
 
18
12
  describe '#Gatling' do
@@ -35,13 +29,13 @@ describe Gatling do
35
29
  end
36
30
 
37
31
  it "#save_image_as_diff" do
38
- @image_class_mock.should_receive(:save).with(:as => :diff).and_return(@ref_path)
32
+ @image_class_mock.should_receive(:save).with(:diff).and_return(@ref_path)
39
33
  @image_class_mock.should_receive(:file_name).at_least(:once).and_return("some_name")
40
34
  expect {subject.save_image_as_diff(@image_class_mock)}.should raise_error
41
35
  end
42
36
 
43
37
  it "#save_image_as_candidate" do
44
- @image_class_mock.should_receive(:save).with(:as => :candidate).and_return(@ref_path)
38
+ @image_class_mock.should_receive(:save).with(:candidate).and_return(@ref_path)
45
39
  @image_class_mock.should_receive(:file_name).at_least(:once).and_return("some_name")
46
40
  @image_class_mock.should_receive(:path).and_return(@path)
47
41
  expect {subject.save_image_as_candidate(@image_class_mock)}.should raise_error
@@ -58,7 +52,7 @@ describe Gatling do
58
52
 
59
53
  it "when image_exists? == false" do
60
54
  @image_class_mock.should_receive(:exists?).and_return(false)
61
- @image_class_mock.should_receive(:save).with(:as => :reference).and_return(@ref_path)
55
+ @image_class_mock.should_receive(:save).with(:reference).and_return(@ref_path)
62
56
  @image_class_mock.should_receive(:path).and_return(@path)
63
57
  subject.save_image_as_reference(@image_class_mock)
64
58
  end
@@ -72,12 +66,12 @@ describe Gatling do
72
66
  describe "#compare_until_match" do
73
67
 
74
68
  before do
75
- @apple = mock("Gatling::Image")
76
- @orange = mock("Gatling::Image")
77
- @element = mock(Gatling::CaptureElement)
69
+ @ref_image = mock("Gatling::Image")
70
+ @actual_image = mock("Gatling::Image")
71
+ @element = mock("Gatling::CaptureElement")
78
72
  @comparison = mock("Gatling::Comparison")
79
- Gatling::ImageFromFile.stub!(:new).and_return(@orange)
80
- Gatling::ImageFromElement.stub!(:new).and_return(@orange)
73
+ Gatling::ImageFromFile.stub!(:new).and_return(@ref_image)
74
+ Gatling::ImageFromElement.stub!(:new).and_return(@actual_image)
81
75
  Gatling::Comparison.stub!(:new).and_return(@comparison)
82
76
  end
83
77
 
@@ -90,5 +84,11 @@ describe "#compare_until_match" do
90
84
  @comparison.should_receive(:matches?).exactly(1).times.and_return(true)
91
85
  Gatling.compare_until_match(@element, "orange.png", 3)
92
86
  end
87
+
88
+ it 'should compare image from the element with image from the file' do
89
+ @comparison.stub!(:matches?).and_return(true)
90
+ Gatling::Comparison.should_receive(:new).with(@actual_image, @ref_image).and_return(@comparison)
91
+ Gatling.compare_until_match(@element, "orange.png", 3)
92
+ end
93
93
  end
94
94
  end
data/spec/image_spec.rb CHANGED
@@ -3,19 +3,22 @@ require 'spec_helper'
3
3
  describe Gatling::Image do
4
4
 
5
5
  before :all do
6
- ref_path = './image_tests'
7
- Gatling::Configuration.reference_image_path = ref_path
8
- @expected_path = File.join ref_path, 'temp'
9
- @expected_full_path = File.join @expected_path, 'image.png'
6
+ @ref_path = './image_tests'
7
+ @expected_temp_path = File.join @ref_path, 'temp'
8
+ @expected_temp_image_path = File.join @expected_temp_path, 'image.png'
9
+ end
10
+
11
+ after :each do
12
+ config_clean_up
10
13
  end
11
14
 
12
15
  describe 'should initialize from' do
13
16
 
14
17
  it 'image file type' do
15
- File.stub(:exists?).and_return true
18
+ File.stub(:exists?).and_return(true)
16
19
  image_mock = mock(Magick::Image)
20
+ Gatling::Configuration.should_receive(:reference_image_path).any_number_of_times.and_return(@ref_path)
17
21
  Magick::Image.should_receive(:read).with('./image_tests/image.png').and_return([image_mock])
18
-
19
22
  subject = Gatling::ImageFromFile.new("image.png")
20
23
  subject.image.should == image_mock
21
24
  subject.file_name.should == 'image.png'
@@ -44,7 +47,7 @@ describe Gatling::Image do
44
47
  describe "Images file handling" do
45
48
 
46
49
  before :each do
47
-
50
+ Gatling::Configuration.should_receive(:reference_image_path).any_number_of_times.and_return(@ref_path)
48
51
  end
49
52
 
50
53
  it 'will save an image to the correct path for the type' do
@@ -53,32 +56,35 @@ describe Gatling::Image do
53
56
  subject = Gatling::Image.new(mock_image, 'image.png')
54
57
  subject.image = mock_image
55
58
  subject.file_name = 'image.png'
56
- subject.save(:as => :temp)
59
+ subject.save(:temp)
57
60
  end
58
61
 
59
62
  it 'will create directory, then save and image if directory doesnt exist' do
63
+ image_path = 'path/to/image/in/sub/directory/image.png'
64
+ expected_full_path = File.join(Gatling::Configuration.path(:temp), image_path)
65
+ expected_image_dir = File.dirname(expected_full_path)
60
66
 
61
67
  mock_image = stub(Magick::Image)
62
- mock_image.should_receive(:write).with(@expected_full_path).and_return()
68
+ mock_image.should_receive(:write).with(expected_full_path).and_return()
63
69
 
64
- File.should_receive(:exists?).with(@expected_path).and_return(false)
65
- FileUtils.should_receive(:mkdir_p).with(@expected_path)
66
- subject = Gatling::Image.new(mock_image, 'image.png')
70
+ File.should_receive(:exists?).with(expected_full_path).and_return(false)
71
+ FileUtils.should_receive(:mkdir_p).with(expected_image_dir)
72
+ subject = Gatling::Image.new(mock_image, image_path)
67
73
 
68
- subject.save(:as => :temp)
69
- subject.path.should == @expected_full_path
74
+ subject.save(:temp)
75
+ subject.path(:temp).should == expected_full_path
70
76
  end
71
77
 
72
- it 'will save and image if directory exista' do
78
+ it 'will save an image if directory exist' do
73
79
  mock_image = stub(Magick::Image)
74
- mock_image.should_receive(:write).with(@expected_full_path).and_return()
80
+ mock_image.should_receive(:write).with(@expected_temp_image_path).and_return()
75
81
 
76
- File.should_receive(:exists?).with(@expected_path).and_return(true)
82
+ File.should_receive(:exists?).with(@expected_temp_image_path).and_return(true)
77
83
  FileUtils.should_not_receive(:mkdir_p)
78
84
  subject = Gatling::Image.new(mock_image, 'image.png')
79
85
 
80
- subject.save(:as => :temp)
81
- subject.path.should == @expected_full_path
86
+ subject.save(:temp)
87
+ subject.path(:temp).should == @expected_temp_image_path
82
88
  end
83
89
 
84
90
  it 'should check if a file exists, with the file name and type' do
@@ -88,9 +94,35 @@ describe Gatling::Image do
88
94
  subject.file_name = 'image.png'
89
95
  subject.exists?
90
96
  end
97
+
98
+ it 'image object should default to reference type' do
99
+ mock_image = mock(Magick::Image)
100
+ subject = Gatling::Image.new(mock_image, 'image.png')
101
+ subject.path.should == File.join(@ref_path,'image.png')
102
+ end
91
103
  end
92
104
 
105
+ describe "it creates a path from path types" do
106
+
107
+ before :each do
108
+ Gatling::Configuration.should_receive(:reference_image_path).any_number_of_times.and_return('/some/path')
109
+ mock_image = mock(Magick::Image)
110
+ @subject = Gatling::Image.new(mock_image, 'image.png')
111
+ end
112
+
113
+ it "from reference" do
114
+ # expected_path = '/some/path/reference'
115
+ @subject.path.should == '/some/path/image.png'
116
+ end
93
117
 
94
- end
118
+ it "from diff" do
119
+ @subject.path(:diff).should == '/some/path/diff/image.png'
120
+ end
121
+
122
+ it "from candidate" do
123
+ @subject.path(:candidate).should == '/some/path/candidate/image.png'
124
+ end
125
+
126
+ end
95
127
 
96
- describe Gatling::ImageFromElement
128
+ end
@@ -12,7 +12,7 @@ describe Gatling::ImageWrangler do
12
12
 
13
13
  it 'should get the position of the css element' do
14
14
 
15
- #Overiding the stupid public method :y of YAML module
15
+ #Overiding the stupid public method:y of YAML module
16
16
 
17
17
  location = Point.new
18
18
  location.x = 1
data/spec/spec_helper.rb CHANGED
@@ -18,6 +18,12 @@ Capybara.app_host = "file://#{File.expand_path(File.dirname(__FILE__))}/support/
18
18
  Capybara.default_driver = :selenium
19
19
  Capybara.run_server = false
20
20
 
21
+ def config_clean_up
22
+ Gatling::Configuration.trainer_toggle = false
23
+ Gatling::Configuration.reference_image_path = nil
24
+ Gatling::Configuration.browser_folders = false
25
+ end
26
+
21
27
  def remove_refs(dir)
22
28
  Dir.glob("#{dir}/**/*.png").each {|image| FileUtils.rm image}
23
29
  end
@@ -32,9 +38,9 @@ def spec_support_root
32
38
  end
33
39
 
34
40
  def create_images_for_web_page
35
- asset_path = File.join(spec_support_root, 'assets')
36
- create_square_image(asset_path, 'black')
37
- create_square_image(asset_path, 'red')
41
+ asset_reference_path = File.join(spec_support_root, 'assets')
42
+ create_square_image(asset_reference_path, 'black')
43
+ create_square_image(asset_reference_path, 'red')
38
44
  end
39
45
 
40
46
  def create_square_image(path, color)
@@ -18,5 +18,6 @@
18
18
  <img src='./black.png' id="black"/>
19
19
  <img src='./red.png' id="red"/>
20
20
  </div>
21
+ <img src='./purple.png' id="differentSize" width="15px" height="180px"/>
21
22
  </body>
22
23
  </html>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gatling
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-27 00:00:00.000000000 Z
12
+ date: 2012-05-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rmagick
16
- requirement: &70304962876040 !ruby/object:Gem::Requirement
16
+ requirement: &70099690488660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.13.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70304962876040
24
+ version_requirements: *70099690488660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec-core
27
- requirement: &70304962875260 !ruby/object:Gem::Requirement
27
+ requirement: &70099690488120 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.8.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70304962875260
35
+ version_requirements: *70099690488120
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70304962874240 !ruby/object:Gem::Requirement
38
+ requirement: &70099690536780 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.8.0
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70304962874240
46
+ version_requirements: *70099690536780
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: capybara
49
- requirement: &70304962873720 !ruby/object:Gem::Requirement
49
+ requirement: &70099690536300 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.1.2
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70304962873720
57
+ version_requirements: *70099690536300
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &70304962873040 !ruby/object:Gem::Requirement
60
+ requirement: &70099690535820 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 0.9.2
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70304962873040
68
+ version_requirements: *70099690535820
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec-instafail
71
- requirement: &70304962872180 !ruby/object:Gem::Requirement
71
+ requirement: &70099690535340 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 0.1.8
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70304962872180
79
+ version_requirements: *70099690535340
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: pry
82
- requirement: &70304962920060 !ruby/object:Gem::Requirement
82
+ requirement: &70099690534800 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: 0.9.8.2
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70304962920060
90
+ version_requirements: *70099690534800
91
91
  description: Add visual comparison matchers for rspec
92
92
  email:
93
93
  - grotbart@gmail.com
@@ -123,7 +123,6 @@ files:
123
123
  - spec/image_spec.rb
124
124
  - spec/image_wrangler_spec.rb
125
125
  - spec/integration/gatling_integration_spec.rb
126
- - spec/rspec_matcher_spec.rb
127
126
  - spec/spec.opts
128
127
  - spec/spec_helper.rb
129
128
  - spec/support/assets/fruit_app.html
@@ -161,7 +160,6 @@ test_files:
161
160
  - spec/image_spec.rb
162
161
  - spec/image_wrangler_spec.rb
163
162
  - spec/integration/gatling_integration_spec.rb
164
- - spec/rspec_matcher_spec.rb
165
163
  - spec/spec.opts
166
164
  - spec/spec_helper.rb
167
165
  - spec/support/assets/fruit_app.html
@@ -1,39 +0,0 @@
1
- require 'spec_helper'
2
- include Capybara::DSL
3
-
4
- describe 'rspec matcher' do
5
-
6
- before(:all) do
7
- @black_box = 'black.png'
8
- @ref_path = Gatling::Configuration.reference_image_path = File.join(spec_support_root, 'ref_path')
9
- create_images_for_web_page
10
- end
11
-
12
- after(:each) do
13
- Gatling::Configuration.trainer_toggle = false
14
- remove_refs(@ref_path)
15
- end
16
-
17
- describe 'initializing and runnin gatling' do
18
-
19
- it 'will pass if images matches refernce' do
20
- create_square_image(@ref_path, 'black')
21
- black_element = element_for_spec("#black")
22
- black_element.should look_like(@black_box)
23
- end
24
-
25
- it 'will fail if images dosent matches refernce' do
26
- create_square_image(@ref_path, 'black')
27
- red_element = element_for_spec("#red")
28
- expect{red_element.should look_like(@black_box)}.should raise_error
29
- end
30
-
31
- it 'will return true if it makes a new reference image in trainer mode' do
32
- Gatling::Configuration.trainer_toggle = true
33
- black_element = element_for_spec("#black")
34
- black_element.should look_like(@black_box)
35
- File.exists?(File.join(@ref_path, @black_box)).should be_true
36
- end
37
-
38
- end
39
- end