cucumber-screenshot 0.1.0-universal-darwin → 0.2.0-universal-darwin

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -18,27 +18,37 @@ To install the latest release as a gem
18
18
 
19
19
  == Use
20
20
 
21
- Add the following line to your ./features/support/env.rb file
21
+ Add the following linea to your ./features/support/env.rb file in your Rails
22
+ project.
22
23
 
23
24
  require 'cucumber_screenshot'
25
+ World(CucumberScreenshot::World)
24
26
 
25
- and then run
27
+ # Take a screenshot if a scenario fails
28
+ After do |scenario|
29
+ if scenario.failed?
30
+ screenshot
31
+ end
32
+ end
26
33
 
27
- cucumber features --format screenshot
34
+ # Take a screenshot for every step for scenario/features tagged @screenshot
35
+ AfterStep('@screenshot') do |scenario|
36
+ if screenshot_due?
37
+ screenshot
38
+ end
39
+ end
28
40
 
29
- from your Rails application's directory.
41
+ Then use the 'rake cucumber' and 'rake cucumber:wip' tasks as per usual
30
42
 
31
- This will create a ./features/screenshots directory in your Rails application
32
- with subfolders for each scenario. Each subfolder will contain a screenshot of
33
- each page generated and the html source of each of those pages.
43
+ Screenshots will be saved in ./features/screenshots in your project.
34
44
 
35
- == Capturing a single screenshot
45
+ === Capturing a single screenshot
36
46
 
37
47
  If you want to capture a single screenshot rather than every page then add the
38
48
  following step to one of your Rails application's step files
39
49
 
40
- sudo gem install webratThen "screenshot" do
41
- screenshot.should be_true
50
+ Then "screenshot" do
51
+ screenshot
42
52
  end
43
53
 
44
54
  and then add
@@ -46,7 +56,7 @@ and then add
46
56
  Then screenshot
47
57
 
48
58
  to your feature file in the place where you want to capture a screenshot of the
49
- page that your application geneerated.
59
+ page that your application generated.
50
60
 
51
61
  == TODO
52
62
 
data/Rakefile CHANGED
@@ -7,9 +7,9 @@ begin
7
7
  gemspec.platform = "universal-darwin"
8
8
  gemspec.requirements << "Mac OS X 10.5 or later"
9
9
  gemspec.requirements << "RubyCocoa"
10
- gemspec.add_dependency('cucumber', '>= 0.3.9')
11
- gemspec.add_dependency('webrat', '>= 0.5.3')
12
- gemspec.add_dependency('mocoso-snapurl', '>= 0.0.3')
10
+ gemspec.add_dependency('cucumber', '= 0.4.2')
11
+ gemspec.add_dependency('webrat', '= 0.5.3')
12
+ gemspec.add_dependency('snapurl', '>= 0.0.3')
13
13
  gemspec.email = 'joel.chippindale@gmail.com'
14
14
  gemspec.authors = ['Joel Chippindale']
15
15
  end
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :patch: 0
3
3
  :major: 0
4
- :minor: 1
4
+ :minor: 2
@@ -1,17 +1,10 @@
1
1
  require 'cucumber'
2
+ require 'cucumber/formatter/html'
2
3
  require 'webrat'
3
4
 
4
- require 'cucumber_screenshot/formatter'
5
- require 'cucumber_screenshot/webrat/session'
5
+ require 'cucumber_screenshot/world'
6
+ require 'cucumber_screenshot/extensions'
6
7
 
7
- # Add formatter to Cucumber's built in list so you can use
8
- #
9
- # --format screenshot
10
- #
11
- # as well as
12
- #
13
- # --format Cucumber::Format::Screenshot
14
- Cucumber::Cli::Configuration::BUILTIN_FORMATS['screenshot'] = 'CucumberScreenshot::Formatter'
15
-
16
- # Make screenshot method available to steps
17
- Webrat::Methods.delegate_to_session :screenshot
8
+ module CucumberScreenshot
9
+ VERSION = '0.2.0'
10
+ end
@@ -0,0 +1,54 @@
1
+ module CucumberScreenshot
2
+ module Extensions
3
+
4
+ module StepMother
5
+ def self.included(base)
6
+ unless base.method_defined?(:embed_image)
7
+ base.send(:define_method, :embed_image) do |image_path|
8
+ @visitor.embed_image(image_path)
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ module TreeWalker
15
+ def self.included(base)
16
+ unless base.method_defined?(:embed_image)
17
+ base.send(:define_method, :embed_image) do |image_path|
18
+ broadcast(image_path)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ module Console
25
+ def self.included(base)
26
+ unless base.method_defined?(:embed_image)
27
+ base.send(:define_method, :embed_image) do |image_path|
28
+ announce("- Image saved to #{image_path}")
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ module Html
35
+ def self.included(base)
36
+ unless base.method_defined?(:embed_image)
37
+ # TODO: needs style
38
+ # TODO: Place to right of or below step result (when captured with AfterStep)
39
+ base.send(:define_method, :embed_image) do |image_path|
40
+ builder.a('Screenshot', :class => 'announcement', :href => '#', :onclick => "img=document.getElementById('#{image_path}'); img.style.display = (img.style.display == 'none' ? 'block' : 'none');")
41
+ builder.img(:id => image_path, :src => image_path, :style => 'display: none')
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ Cucumber::StepMother.send(:include, CucumberScreenshot::Extensions::StepMother)
50
+
51
+ Cucumber::Ast::TreeWalker.send(:include, CucumberScreenshot::Extensions::TreeWalker)
52
+
53
+ Cucumber::Formatter::Pretty.send(:include, CucumberScreenshot::Extensions::Console)
54
+ Cucumber::Formatter::Html.send(:include, CucumberScreenshot::Extensions::Html)
@@ -0,0 +1,64 @@
1
+ module CucumberScreenshot
2
+ module World
3
+
4
+ attr_accessor :response_body_for_last_screenshot, :screenshot_index, :screenshot_directory, :current_feature_segment, :current_scenario_segment
5
+
6
+ def base_screenshot_directory_name
7
+ # TODO: Make this configurable
8
+ # TODO: Make this work for other frameworks e.g. sinatra
9
+ "#{RAILS_ROOT}/features/screenshots"
10
+ end
11
+
12
+ def screenshot(directory_name = base_screenshot_directory_name, file_name = "screenshot-#{(Time.now.to_f * 100).to_i}")
13
+ FileUtils.mkdir_p("#{directory_name}/html")
14
+
15
+ html_file_name = "#{directory_name}/html/#{file_name}.html"
16
+ File.open(html_file_name, "w") do |f|
17
+ f.write rewrite_javascript_and_css_and_image_references(current_response_body)
18
+ end
19
+
20
+ command = "snapurl file://#{html_file_name} --no-thumbnail --no-clip --filename #{file_name} --output-dir #{directory_name}"
21
+ `#{command}`
22
+ if $? == 0
23
+ embed_image "#{directory_name}/#{file_name}.png"
24
+ self.response_body_for_last_screenshot = current_response_body
25
+ true
26
+ else
27
+ report_error_running_screenshot_command(command)
28
+ false
29
+ end
30
+ end
31
+
32
+ def screenshot_due?
33
+ current_response_body && current_response_body != response_body_for_last_screenshot && !webrat_session.redirect?
34
+ end
35
+
36
+ def embed_image(image_path)
37
+ @__cucumber_step_mother.embed_image(image_path)
38
+ end
39
+
40
+ protected
41
+ def current_response_body
42
+ webrat_session.send(:response) && webrat_session.response_body
43
+ end
44
+
45
+ def rewrite_javascript_and_css_and_image_references(response_html) # :nodoc:
46
+ doc_root = webrat_session.adapter.doc_root
47
+ return response_html unless doc_root
48
+ response_html.gsub(/"\/(javascripts|stylesheets|images)\//, '"' + doc_root + '/\1/')
49
+ end
50
+
51
+ def report_error_running_screenshot_command(command)
52
+ STDERR.puts "
53
+ Unable to make screenshot, to find out what went wrong try the following from the command line
54
+
55
+ #{command}
56
+
57
+ Please remember need to have installed the gem snapurl to take screenshots
58
+
59
+ gem install snapurl
60
+
61
+ "
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe CucumberScreenshot::Extensions do
4
+
5
+ describe 'formatters' do
6
+ before(:each) do
7
+ @step_mother = mock('step_mother')
8
+ @io = mock('io')
9
+ end
10
+
11
+ describe 'Pretty#embed_image' do
12
+ it 'should call announce' do
13
+ formatter = Cucumber::Formatter::Pretty.new(@step_mother, @io, {})
14
+ formatter.should_receive(:announce).with('- Image saved to /tmp/foo.png')
15
+ formatter.embed_image('/tmp/foo.png')
16
+ end
17
+ end
18
+
19
+ describe 'Html#embed_image' do
20
+ it 'should build link and image' do
21
+ formatter = Cucumber::Formatter::Html.new(@step_mother, @io, {})
22
+ builder = mock('builder')
23
+ formatter.stub!(:builder => builder)
24
+ builder.should_receive(:a)
25
+ builder.should_receive(:img)
26
+ formatter.embed_image('/tmp/foo.png')
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,56 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ class TestWorld
4
+ include CucumberScreenshot::World
5
+ end
6
+
7
+ describe CucumberScreenshot::World do
8
+ before(:each) do
9
+ @session = TestWorld.new
10
+ end
11
+
12
+ describe '#base_screenshot_directory_name' do
13
+ it "add features/screenshots to rails root" do
14
+ ::RAILS_ROOT = 'tmp'
15
+ TestWorld.new.base_screenshot_directory_name.should == 'tmp/features/screenshots'
16
+ end
17
+ end
18
+
19
+ describe '#screenshot' do
20
+ before(:each) do
21
+ FileUtils.stub!(:mkdir_p => true)
22
+ File.stub!(:open => true)
23
+ @session.stub!(:base_screenshot_directory_name => 'tmp/features/screenshots', :response_body => 'foo')
24
+ @session.stub!(:'`' => 'foo')
25
+ # While $? is not being set by the backtick stub
26
+ @session.stub!(:report_error_running_screenshot_command => true)
27
+ end
28
+
29
+ it 'should call File.makedirs' do
30
+ FileUtils.should_receive(:mkdir_p).with('1/2/html').and_return(true)
31
+ @session.screenshot('1/2', 'snapshot-001')
32
+ end
33
+ end
34
+
35
+ describe '#embed_image' do
36
+
37
+ end
38
+
39
+ describe 'protected' do
40
+ describe '#rewrite_javascript_and_css_and_image_references' do
41
+ before(:each) do
42
+ @session.stub!(:webrat_session => stub('webrat_session', :adapter => stub('rails_adapter', :doc_root => '/tmp/public')))
43
+ end
44
+
45
+ it 'should replace relative /javascripts/ references with file references' do
46
+ @session.send(:rewrite_javascript_and_css_and_image_references, '<script src="/javascripts/application.js?1255077419" type="text/javascript"></script>').
47
+ should == '<script src="/tmp/public/javascripts/application.js?1255077419" type="text/javascript"></script>'
48
+ end
49
+
50
+ it 'should replace relative /javascripts/ references with file references' do
51
+ @session.send(:rewrite_javascript_and_css_and_image_references, '<link href="/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />').
52
+ should == '<link href="/tmp/public/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />'
53
+ end
54
+ end
55
+ end
56
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,2 @@
1
- SPEC_DIR = File.dirname(__FILE__)
2
-
3
1
  require 'rubygems'
4
2
  require 'cucumber_screenshot'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cucumber-screenshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: universal-darwin
6
6
  authors:
7
7
  - Joel Chippindale
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-19 00:00:00 +01:00
12
+ date: 2009-10-24 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -18,9 +18,9 @@ dependencies:
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
- - - ">="
21
+ - - "="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.3.9
23
+ version: 0.4.2
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: webrat
@@ -28,12 +28,12 @@ dependencies:
28
28
  version_requirement:
29
29
  version_requirements: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.5.3
34
34
  version:
35
35
  - !ruby/object:Gem::Dependency
36
- name: mocoso-snapurl
36
+ name: snapurl
37
37
  type: :runtime
38
38
  version_requirement:
39
39
  version_requirements: !ruby/object:Gem::Requirement
@@ -57,10 +57,10 @@ files:
57
57
  - Rakefile
58
58
  - VERSION.yml
59
59
  - lib/cucumber_screenshot.rb
60
- - lib/cucumber_screenshot/formatter.rb
61
- - lib/cucumber_screenshot/webrat/session.rb
62
- - spec/cucumber_screenshot/formatter_spec.rb
63
- - spec/cucumber_screenshot/webrat/session_spec.rb
60
+ - lib/cucumber_screenshot/extensions.rb
61
+ - lib/cucumber_screenshot/world.rb
62
+ - spec/cucumber_screenshot/extensions_spec.rb
63
+ - spec/cucumber_screenshot/world_spec.rb
64
64
  - spec/spec.opts
65
65
  - spec/spec_helper.rb
66
66
  has_rdoc: true
@@ -93,6 +93,6 @@ signing_key:
93
93
  specification_version: 3
94
94
  summary: Cucumber formatter that outputs PNG screenshots of your app
95
95
  test_files:
96
- - spec/cucumber_screenshot/formatter_spec.rb
97
- - spec/cucumber_screenshot/webrat/session_spec.rb
96
+ - spec/cucumber_screenshot/extensions_spec.rb
97
+ - spec/cucumber_screenshot/world_spec.rb
98
98
  - spec/spec_helper.rb
@@ -1,63 +0,0 @@
1
- module CucumberScreenshot
2
- class Formatter < Cucumber::Ast::Visitor
3
-
4
- attr_accessor :response_body_for_last_screenshot, :screenshot_index, :screenshot_directory, :current_feature_segment, :current_scenario_segment
5
-
6
- # Currently ignores io and options arguments
7
- def initialize(step_mother, io, options)
8
- super(step_mother)
9
- @io = io
10
- @options = options
11
- end
12
-
13
- def visit_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background)
14
- if screenshot_due?
15
- self.screenshot_index = screenshot_index.next
16
- exit unless session.screenshot(screenshot_directory_name, "screenshot-#{format('%03d', screenshot_index)}")
17
- self.response_body_for_last_screenshot = current_response_body
18
- end
19
- super
20
- end
21
-
22
- def visit_scenario_name(keyword, name, file_colon_line, source_indent)
23
- puts "Scenario #{name}"
24
- self.response_body_for_last_screenshot = nil
25
- self.screenshot_index = 0
26
- self.current_scenario_segment = segment_for_scenario_named(name)
27
- end
28
-
29
- def visit_feature(feature)
30
- self.current_feature_segment = segment_for_feature(feature)
31
- super
32
- end
33
-
34
- def visit_feature_name(name)
35
- puts(name)
36
- end
37
-
38
- protected
39
- def screenshot_directory_name
40
- File.join(session.base_screenshot_directory_name, current_feature_segment, current_scenario_segment)
41
- end
42
-
43
- def segment_for_feature(feature)
44
- feature.file.gsub(/^features\//, '').gsub(/\.feature$/, '')
45
- end
46
-
47
- def segment_for_scenario_named(name)
48
- name.downcase.gsub(' ', '_').gsub(/(\(|\))/, '')
49
- end
50
-
51
- def session
52
- step_mother.current_world.webrat_session
53
- end
54
-
55
- def current_response_body
56
- session.send(:response) && session.response_body
57
- end
58
-
59
- def screenshot_due?
60
- current_response_body && current_response_body != response_body_for_last_screenshot && !session.redirect?
61
- end
62
- end
63
- end
@@ -1,53 +0,0 @@
1
- module CucumberScreenshot
2
- module Webrat
3
- module Session #:nodoc:
4
-
5
- def base_screenshot_directory_name
6
- # TODO: Make this configurable
7
- # TODO: Make this work for other frameworks e.g. sinatra
8
- "#{RAILS_ROOT}/features/screenshots"
9
- end
10
-
11
- def screenshot(directory_name = base_screenshot_directory_name, file_name = "screenshot-#{Time.now.to_i}")
12
- File.makedirs("#{directory_name}/html")
13
-
14
- html_file_name = "#{directory_name}/html/#{file_name}.html"
15
- File.open(html_file_name, "w") do |f|
16
- f.write rewrite_javascript_and_css_and_image_references(response_body)
17
- end
18
-
19
- command = "snapurl file://#{html_file_name} --no-thumbnail --no-clip --filename #{file_name} --output-dir #{directory_name}"
20
- `#{command}`
21
- if $? == 0
22
- puts "- Screenshot saved to #{directory_name}/#{file_name}.png\n"
23
- true
24
- else
25
- report_error_running_screenshot_command(command)
26
- false
27
- end
28
- end
29
-
30
- protected
31
- def rewrite_javascript_and_css_and_image_references(response_html) # :nodoc:
32
- return response_html unless doc_root
33
- rewrite_css_and_image_references(response_html).gsub(/"\/(javascript)/, doc_root + '/\1')
34
- end
35
-
36
- def report_error_running_screenshot_command(command)
37
- STDERR.puts "
38
- Unable to make screenshot, to find out what went wrong try the following from the command line
39
-
40
- #{command}
41
-
42
- Please remember need to have installed the gem mocoso-snapshoturl to take screenshots
43
-
44
- gem install mocoso-snapshoturl
45
-
46
- "
47
- end
48
- end
49
- end
50
- end
51
-
52
- Webrat::Session.send(:include, CucumberScreenshot::Webrat::Session)
53
-
@@ -1,14 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
2
-
3
- module CucumberScreenshot
4
- describe Formatter do
5
- describe '#visit_scenario_named' do
6
- before(:each) do
7
- @formatter = Formatter.new(stub('step_mother'), nil, {})
8
- end
9
- it "set current_scenario_segment from scenario name" do
10
- lambda { @formatter.visit_scenario_name(nil, 'Login (when already has an account)', 1, 2) }.should change(@formatter, :current_scenario_segment).to('login_when_already_has_an_account')
11
- end
12
- end
13
- end
14
- end
@@ -1,30 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper'
2
-
3
- module CucumberScreenshot
4
- module Webrat
5
- describe Session do
6
- describe '#base_screenshot_directory_name' do
7
- it "add features/screenshots to rails root" do
8
- ::RAILS_ROOT = 'tmp'
9
- ::Webrat::Session.new.base_screenshot_directory_name.should == 'tmp/features/screenshots'
10
- end
11
- end
12
-
13
- describe '#screenshot' do
14
- before(:each) do
15
- @html_file = stub('html_file', :write => true)
16
- File.stub!(:makedirs => true, :open => @html_file)
17
- @session = ::Webrat::Session.new
18
- @session.stub!(:base_screenshot_directory_name => 'tmp/features/screenshots', :response_body => 'foo')
19
- @session.stub!(:` => 'foo')
20
- @session.stub!(:report_error_running_screenshot_command => true) # While $? is not being set by the backtick stub
21
- end
22
-
23
- it 'should call File.makedirs' do
24
- File.should_receive(:makedirs).with('1/2/html').and_return(true)
25
- @session.screenshot('1/2', 'snapshot-001')
26
- end
27
- end
28
- end
29
- end
30
- end