Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
Pull Capybara in, this will let us pass around one less parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJefe committed Feb 5, 2016
1 parent e568264 commit 06ec30a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
12 changes: 7 additions & 5 deletions lib/compatriot.rb
Expand Up @@ -7,7 +7,8 @@

module Compatriot
class << self
attr_accessor :app, :screenshot_directory, :ui_difference_threshold
attr_accessor :app, :screenshot_directory,
:ui_difference_threshold, :framework

def configure
yield self
Expand All @@ -17,7 +18,7 @@ def run(paths)
Compatriot::Runner.start(app, paths)
end

def take_screenshot(page, test, description)
def take_screenshot(test, description)
filename = filename_for_test(test, description)
control_image_path = filepath_for_screenshot('control', filename)

Expand All @@ -26,11 +27,11 @@ def take_screenshot(page, test, description)
else
screenshot_type = 'control'
end
page.save_screenshot filepath_for_screenshot(screenshot_type, filename)
framework.current_session.save_screenshot filepath_for_screenshot(screenshot_type, filename)
end

def percentage_changed(page, test, description = '')
variable_img_path = take_screenshot(page, test, description)
def percentage_changed(test, description = '')
variable_img_path = take_screenshot(test, description)
control_img_path = filepath_for_screenshot('control', filename_for_test(test, description))
diff = Compatriot::ColorDiffer.diff(variable_img_path, control_img_path, self.screenshot_directory + '/')
variable_image = ChunkyPNG::Image.from_file(variable_img_path)
Expand All @@ -53,5 +54,6 @@ def filepath_for_screenshot(type, filename)
Compatriot.configure do |config|
config.screenshot_directory = './compatriot/screenshots'
config.ui_difference_threshold = 0.0
config.framework = Capybara #only supporting capybara until someone wants to support something else
end
end
4 changes: 2 additions & 2 deletions lib/compatriot/assertions.rb
@@ -1,7 +1,7 @@
module Compatriot
module Assertions
def assert_no_ui_changes(page, title = '')
diff = Compatriot.percentage_changed(page, self, title)
def assert_no_ui_changes(title = '')
diff = Compatriot.percentage_changed(self, title)
assert diff <= Compatriot.ui_difference_threshold, "The difference in the page (#{diff}%) is greater then the threshold #{Compatriot.ui_difference_threshold}"
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -25,6 +25,12 @@ class Minitest::Spec
include Compatriot::Assertions
end

module FakeCapybara
def self.current_session
Page.new
end
end

class Page
def save_screenshot filepath
root_dir = File.join(File.dirname(__FILE__), './')
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/assertions_spec.rb
Expand Up @@ -22,6 +22,6 @@ def setup_control_image
end

it 'can assert on assert_no_ui_changes' do
assert_no_ui_changes(page, 'this test')
assert_no_ui_changes('this test')
end
end
15 changes: 8 additions & 7 deletions spec/unit/compatriot_spec.rb
Expand Up @@ -20,13 +20,14 @@ def setup_control_image
FileUtils.remove_dir(SCREENSHOTS_DIR) if File.directory?(SCREENSHOTS_DIR)
Compatriot.configure do |config|
config.screenshot_directory = SCREENSHOTS_DIR
config.framework = FakeCapybara
end
end

describe 'no control image is found' do
it 'it will create one' do
Compatriot.take_screenshot(page, stubbed_test, 'and has a description')
Compatriot.take_screenshot(page, stubbed_test, 'another')
Compatriot.take_screenshot(stubbed_test, 'and has a description')
Compatriot.take_screenshot(stubbed_test, 'another')

assert File.exist?(CONTROL_IMG), 'control image not found'
assert File.exist?(CONTROL_IMG2), 'control image not found'
Expand All @@ -39,23 +40,23 @@ def setup_control_image
end

it 'stores a variable image' do
Compatriot.take_screenshot(page, stubbed_test, 'and has a description')
Compatriot.take_screenshot(stubbed_test, 'and has a description')
assert File.exist?(VARIABLE_IMG)
end

it 'stores the image difference' do
Compatriot.percentage_changed(page, stubbed_test, 'and has a description')
Compatriot.percentage_changed( stubbed_test, 'and has a description')
assert File.exist?(DIFF_IMG)
end

it 'returns the percentage difference' do
result = Compatriot.percentage_changed(page, stubbed_test, 'and has a description')
result = Compatriot.percentage_changed( stubbed_test, 'and has a description')
assert_equal(0.81, result.round(2))
end

it 'returns 0 % if there is no difference' do
Compatriot::ColorDiffer.stubs(:diff).returns([])
result = Compatriot.percentage_changed(page, stubbed_test, 'and has a description')
result = Compatriot.percentage_changed( stubbed_test, 'and has a description')
assert_equal(0.0, result.round(2))
end
end
Expand All @@ -71,7 +72,7 @@ def setup_control_image
end

it 'can set the screenshot directory' do
Compatriot.take_screenshot(page, stubbed_test, 'and has a description')
Compatriot.take_screenshot( stubbed_test, 'and has a description')
assert File.exist?(@control_file), 'control image not found'
end
end
Expand Down

0 comments on commit 06ec30a

Please sign in to comment.