Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added specs for Saver

  • Loading branch information...
commit 809112072f03253445cc5c4b3932d8f8ab97e54f 1 parent 19c6797
@adzap adzap authored
View
2  .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
View
4 Gemfile
@@ -3,4 +3,6 @@ source "http://rubygems.org"
# Specify your gem's dependencies in capybara-screenshot.gemspec
gemspec
-gem 'rake'
+gem 'rake'
+gem 'fakefs'
+gem 'active_support'
View
115 spec/capybara-screenshot/saver_spec.rb
@@ -0,0 +1,115 @@
+require 'spec_helper'
+
+describe Capybara::Screenshot::Saver do
+ before do
+ Capybara::Screenshot.stub(:capybara_root).and_return(capybara_root)
+ end
+
+ let(:capybara_mock) {
+ mock(Capybara).as_null_object.tap do |m|
+ m.stub(:current_driver).and_return(:default)
+ m.stub(:current_path).and_return('/')
+ m.stub_chain(:page, :driver).and_return(driver_mock)
+ end
+ }
+ let(:capybara_root) { '/tmp' }
+ let(:body_mock) { mock(String) }
+ let(:driver_mock) { mock('Capybara driver').as_null_object }
+ let(:saver) {
+ Capybara::Screenshot::Saver.new(capybara_mock, body_mock)
+ }
+ let(:screenshot_path_regexp) {
+ /^#{Regexp.escape(capybara_root)}\/screenshot-\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}\.png$/
+ }
+
+ it 'should save screenshot file in capybara root output directory' do
+ driver_mock.should_receive(:render).with(/^\/tmp\//)
+
+ saver.save
+ end
+
+ it 'should save html file with "screenshot-Y-M-D-H-M-S.html" format' do
+ capybara_mock.should_receive(:save_page).with(body_mock, /^screenshot-\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}\.html$/)
+
+ saver.save
+ end
+
+ it 'should save screenshot file with "screenshot-Y-M-D-H-M-S.png" format' do
+ driver_mock.should_receive(:render).with(/\/screenshot-\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}\.png$/)
+
+ saver.save
+ end
+
+ it 'should not save html if false passed as html argument' do
+ saver = Capybara::Screenshot::Saver.new(capybara_mock, body_mock, false)
+ capybara_mock.should_not_receive(:save_page)
+
+ saver.save
+ end
+
+ it 'should save if current_path is empty' do
+ capybara_mock.stub(:current_path).and_return(nil)
+ capybara_mock.should_not_receive(:save_page)
+ driver_mock.should_not_receive(:render)
+
+ saver.save
+ end
+
+ describe "with selenium driver" do
+ before do
+ capybara_mock.stub(:current_driver).and_return(:selenium)
+ end
+
+ it 'should save via browser' do
+ browser_mock = mock('browser')
+ driver_mock.should_receive(:browser).and_return(browser_mock)
+ browser_mock.should_receive(:save_screenshot).with(screenshot_path_regexp)
+
+ saver.save
+ end
+ end
+
+ describe "with poltergeist driver" do
+ before do
+ capybara_mock.stub(:current_driver).and_return(:poltergeist)
+ end
+
+ it 'should save driver render with :full => true' do
+ driver_mock.should_receive(:render).with(screenshot_path_regexp, {:full => true})
+
+ saver.save
+ end
+ end
+
+ describe "with webkit driver" do
+ before do
+ capybara_mock.stub(:current_driver).and_return(:webkit)
+ end
+
+ it 'should save driver render' do
+ driver_mock.should_receive(:render).with(screenshot_path_regexp)
+
+ saver.save
+ end
+ end
+
+ describe "with unknown driver" do
+ before do
+ capybara_mock.stub(:current_driver).and_return(:unknown)
+ saver.stub(:warn).and_return(nil)
+ end
+
+ it 'should save driver render' do
+ driver_mock.should_receive(:render).with(screenshot_path_regexp)
+
+ saver.save
+ end
+
+ it 'should output warning about unknown results' do
+ # Not pure mock testing
+ saver.should_receive(:warn).with(/driver 'unknown'.*unknown results/).and_return(nil)
+
+ saver.save
+ end
+ end
+end
View
16 spec/spec_helper.rb
@@ -0,0 +1,16 @@
+# This file was generated by the `rspec --init` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# Require this file using `require "spec_helper.rb"` to ensure that it is only
+# loaded once.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+
+$: << '../lib'
+require 'active_support/core_ext/module'
+require 'capybara-screenshot'
+
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+end
Please sign in to comment.
Something went wrong with that request. Please try again.