capybara-screenshot 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +51 -14
- data/lib/capybara-screenshot.rb +11 -18
- data/lib/capybara-screenshot/rspec.rb +25 -7
- data/lib/capybara-screenshot/version.rb +1 -1
- data/spec/capybara-screenshot/capybara_spec.rb +3 -1
- data/spec/capybara-screenshot/saver_spec.rb +23 -11
- metadata +4 -4
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
capybara-screenshot gem
|
2
2
|
=======================
|
3
3
|
|
4
|
-
Using this gem, whenever a [Capybara](https://github.com/jnicklas/capybara) test in [Cucumber](http://cukes.info/), [Rspec](https://www.relishapp.com/rspec) or Minitest fails, the HTML for the failed page and a screenshot (when using [capybara-webkit](https://github.com/thoughtbot/capybara-webkit)
|
4
|
+
Using this gem, whenever a [Capybara](https://github.com/jnicklas/capybara) test in [Cucumber](http://cukes.info/), [Rspec](https://www.relishapp.com/rspec) or Minitest fails, the HTML for the failed page and a screenshot (when using [capybara-webkit](https://github.com/thoughtbot/capybara-webkit), [Selenium](http://seleniumhq.org/) or [poltergeist](https://github.com/jonleighton/poltergeist)) is saved into $APPLICATION_ROOT/tmp/capybara.
|
5
5
|
|
6
6
|
This is a huge help when trying to diagnose a problem in your failing steps as you can view the source code and potentially how the page looked at the time of the failure.
|
7
7
|
|
@@ -10,34 +10,60 @@ Usage
|
|
10
10
|
|
11
11
|
gem install capybara-screenshot
|
12
12
|
|
13
|
-
or update your Gemfile to include:
|
13
|
+
or update your Gemfile to include `capybara-screenshot` at the bottom (order respected as of Bundler 0.10):
|
14
14
|
|
15
|
-
group :test
|
16
|
-
gem 'capybara-screenshot'
|
17
|
-
end
|
18
|
-
|
19
|
-
That's it!
|
15
|
+
gem 'capybara-screenshot', :group => :test
|
20
16
|
|
21
17
|
If you require more control, you can generate the screenshot on demand rather than on failure. This is useful
|
22
|
-
if the failure occurs at a point where the screen shot is not as useful for debugging a rendering problem.
|
18
|
+
if the failure occurs at a point where the screen shot is not as useful for debugging a rendering problem. This
|
19
|
+
can be more useful if you disable the auto-generate on failure feature with the following config
|
20
|
+
|
21
|
+
Capybara::Screenshot.autosave_on_failure = false
|
23
22
|
|
24
|
-
|
23
|
+
Anywhere the Capybara DSL methods (visit, click etc.) are available so too will are the screenshot methods.
|
25
24
|
|
26
25
|
screen_shot_and_save_page
|
27
26
|
|
28
|
-
Or for screenshot only,
|
27
|
+
Or for screenshot only, which will automatically open the image.
|
29
28
|
|
30
29
|
screen_shot_and_open_image
|
31
30
|
|
32
|
-
|
33
|
-
|
31
|
+
These are just calls on the main library methods.
|
32
|
+
|
34
33
|
Capybara::Screenshot.screen_shot_and_save_page
|
35
34
|
|
36
35
|
Capybara::Screenshot.screen_shot_and_open_image
|
37
36
|
|
38
|
-
This can be more useful if you disable the auto-generate on failure feature with the following config
|
39
37
|
|
40
|
-
|
38
|
+
Driver configuration
|
39
|
+
--------------------
|
40
|
+
|
41
|
+
The gem supports the default rendering method for Capybara to generate the screenshot, which is:
|
42
|
+
|
43
|
+
page.driver.render(path)
|
44
|
+
|
45
|
+
There are also some specific driver configurations for Selenium, Webkit, and Poltergeist. See [https://github.com/mattheworiordan/capybara-screenshot/blob/master/lib/capybara-screenshot.rb](the definitions here). The Rack::Test driver, Rails' default, does not allow
|
46
|
+
rendering, so it has a driver definition as a noop.
|
47
|
+
|
48
|
+
If a driver is not found the default rendering will be used. If this doesn't work with your driver, then you can
|
49
|
+
add another driver configuration like so
|
50
|
+
|
51
|
+
# The driver name should match the Capybara driver config name.
|
52
|
+
Capybara::Screenshot.register_driver(:exotic_browser_driver) do |driver, path|
|
53
|
+
driver.super_dooper_render(path)
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
Custom screenshot filename
|
58
|
+
--------------------------
|
59
|
+
|
60
|
+
If you want to control the screenshot filename for a specific test libarary, to inject the test name into it for example,
|
61
|
+
you can override how the basename is generated for the file like so
|
62
|
+
|
63
|
+
Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
|
64
|
+
"screenshot-#{example.description.gsub(' ', '-')}"
|
65
|
+
end
|
66
|
+
end
|
41
67
|
|
42
68
|
|
43
69
|
Example application
|
@@ -47,6 +73,17 @@ A simple Rails 3.1 example application has been set up at [https://github.com/ma
|
|
47
73
|
Git clone the app, and then run Cucumber `rake cucumber`, RSpec `rspec spec/**/*_spec.rb` and Minitest `rake test` and expect intentional failures.
|
48
74
|
Now check the $APPLICATION_ROOT/tmp/capybara folder for the automatic screen shots generated from failed tests.
|
49
75
|
|
76
|
+
Common problems
|
77
|
+
---------------
|
78
|
+
|
79
|
+
If you find that screen shots are not automatically being generated, then it's possible the `capybara-screenshot` gem is loading before your testing framework is loading, and thus unable to determine which framework to hook into. Make sure you include `capybara-screenshot` gem last in your Gemfile (order is respected by Bundler as of 0.10). Alternatively, manually require `capybara-screenshot` using one of the following based on your framework:
|
80
|
+
|
81
|
+
require 'capybara-screenshot/cucumber' # For Cucumber support
|
82
|
+
require 'capybara-screenshot/rspec' # For RSpec support
|
83
|
+
require 'capybara-screenshot/minitest' # For MiniSpec support
|
84
|
+
|
85
|
+
[Raise an issue on the Capybara-Screenshot issue tracker](https://github.com/mattheworiordan/capybara-screenshot/issues) if you are still having problems.
|
86
|
+
|
50
87
|
Repository
|
51
88
|
----------
|
52
89
|
|
data/lib/capybara-screenshot.rb
CHANGED
@@ -33,8 +33,8 @@ module Capybara
|
|
33
33
|
|
34
34
|
def self.capybara_root
|
35
35
|
return @capybara_root if defined?(@capybara_root)
|
36
|
-
|
37
|
-
capybara_tmp_path = Capybara.save_and_open_page_path.
|
36
|
+
#If the path isn't set, default to the current directory
|
37
|
+
capybara_tmp_path = Capybara.save_and_open_page_path || '.'
|
38
38
|
|
39
39
|
@capybara = if defined?(Rails)
|
40
40
|
Rails.root.join capybara_tmp_path
|
@@ -70,15 +70,19 @@ Capybara::Screenshot.class_eval do
|
|
70
70
|
|
71
71
|
register_driver(:selenium) do |driver, path|
|
72
72
|
driver.browser.save_screenshot(path)
|
73
|
-
end
|
73
|
+
end
|
74
74
|
|
75
75
|
register_driver(:poltergeist) do |driver, path|
|
76
76
|
driver.render(path, :full => true)
|
77
|
-
end
|
77
|
+
end
|
78
78
|
|
79
79
|
register_driver(:webkit) do |driver, path|
|
80
80
|
driver.render(path)
|
81
|
-
end
|
81
|
+
end
|
82
|
+
|
83
|
+
register_driver(:webkit_debug) do |driver, path|
|
84
|
+
driver.render(path)
|
85
|
+
end
|
82
86
|
end
|
83
87
|
|
84
88
|
# Register filename prefix formatters
|
@@ -97,16 +101,5 @@ if defined?(Cucumber::RbSupport::RbDsl)
|
|
97
101
|
require 'capybara-screenshot/cucumber'
|
98
102
|
end
|
99
103
|
|
100
|
-
if defined?(RSpec)
|
101
|
-
|
102
|
-
# RSpec hooks afterwards, and thus executed first
|
103
|
-
require 'capybara/rspec'
|
104
|
-
require 'capybara-screenshot/rspec'
|
105
|
-
end
|
106
|
-
|
107
|
-
begin
|
108
|
-
require 'minitest/unit'
|
109
|
-
require 'capybara-screenshot/minitest'
|
110
|
-
rescue LoadError
|
111
|
-
# mini test not available
|
112
|
-
end
|
104
|
+
require 'capybara-screenshot/rspec' if defined?(RSpec)
|
105
|
+
require 'capybara-screenshot/minitest' if defined?(MiniTest)
|
@@ -1,12 +1,30 @@
|
|
1
1
|
RSpec.configure do |config|
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
# use the before hook to add an after hook that runs last
|
3
|
+
config.before(:type => :request) do
|
4
|
+
Capybara::Screenshot::RSpecCache.add_after(example.example_group) do
|
5
|
+
if Capybara::Screenshot.autosave_on_failure && example.exception
|
6
|
+
filename_prefix = Capybara::Screenshot.filename_prefix_for(:rspec, example)
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
+
saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
|
9
|
+
saver.save
|
8
10
|
|
9
|
-
|
11
|
+
example.metadata[:full_description] += "\n Screenshot: #{saver.screenshot_path}"
|
12
|
+
end
|
10
13
|
end
|
11
14
|
end
|
12
|
-
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# keep a record of which example groups we've added an after hook to and add if one does not exist
|
18
|
+
module Capybara
|
19
|
+
module Screenshot
|
20
|
+
class RSpecCache
|
21
|
+
@@example_groups = []
|
22
|
+
def self.add_after(example_group, &block)
|
23
|
+
unless @@example_groups.include?(example_group)
|
24
|
+
example_group.after :each, &block
|
25
|
+
@@example_groups << example_group
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -1,6 +1,8 @@
|
|
1
|
+
require 'capybara/dsl'
|
2
|
+
|
1
3
|
module Capybara::Screenshot
|
2
4
|
describe Capybara do
|
3
|
-
|
5
|
+
|
4
6
|
it 'should add screen shot methods to the Capybara module' do
|
5
7
|
::Capybara.should respond_to(:screen_shot_and_save_page)
|
6
8
|
::Capybara.should respond_to(:screen_shot_and_open_image)
|
@@ -19,10 +19,10 @@ describe Capybara::Screenshot::Saver do
|
|
19
19
|
let(:timestamp) { '2012-06-07-08-09-10' }
|
20
20
|
let(:file_basename) { "screenshot-#{timestamp}" }
|
21
21
|
let(:screenshot_path) { "#{capybara_root}/#{file_basename}.png" }
|
22
|
-
|
22
|
+
|
23
23
|
let(:driver_mock) { mock('Capybara driver').as_null_object }
|
24
24
|
let(:page_mock) { mock('Capybara session page', :body => 'body', :driver => driver_mock).as_null_object }
|
25
|
-
let(:capybara_mock) {
|
25
|
+
let(:capybara_mock) {
|
26
26
|
mock(Capybara).as_null_object.tap do |m|
|
27
27
|
m.stub(:current_driver).and_return(:default)
|
28
28
|
m.stub(:current_path).and_return('/')
|
@@ -34,7 +34,7 @@ describe Capybara::Screenshot::Saver do
|
|
34
34
|
context 'html filename' do
|
35
35
|
it 'should have default format of "screenshot-Y-M-D-H-M-S.html"' do
|
36
36
|
capybara_mock.should_receive(:save_page).with('body', "#{file_basename}.html")
|
37
|
-
|
37
|
+
|
38
38
|
saver.save
|
39
39
|
end
|
40
40
|
|
@@ -42,7 +42,7 @@ describe Capybara::Screenshot::Saver do
|
|
42
42
|
saver = Capybara::Screenshot::Saver.new(capybara_mock, page_mock, true, 'custom-prefix')
|
43
43
|
|
44
44
|
capybara_mock.should_receive(:save_page).with('body', "custom-prefix-#{timestamp}.html")
|
45
|
-
|
45
|
+
|
46
46
|
saver.save
|
47
47
|
end
|
48
48
|
end
|
@@ -80,7 +80,7 @@ describe Capybara::Screenshot::Saver do
|
|
80
80
|
capybara_mock.should_not_receive(:save_page)
|
81
81
|
driver_mock.should_not_receive(:render)
|
82
82
|
|
83
|
-
saver.save
|
83
|
+
saver.save
|
84
84
|
end
|
85
85
|
|
86
86
|
describe "with selenium driver" do
|
@@ -93,7 +93,7 @@ describe Capybara::Screenshot::Saver do
|
|
93
93
|
driver_mock.should_receive(:browser).and_return(browser_mock)
|
94
94
|
browser_mock.should_receive(:save_screenshot).with(screenshot_path)
|
95
95
|
|
96
|
-
saver.save
|
96
|
+
saver.save
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -105,7 +105,7 @@ describe Capybara::Screenshot::Saver do
|
|
105
105
|
it 'should save driver render with :full => true' do
|
106
106
|
driver_mock.should_receive(:render).with(screenshot_path, {:full => true})
|
107
107
|
|
108
|
-
saver.save
|
108
|
+
saver.save
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
@@ -117,7 +117,19 @@ describe Capybara::Screenshot::Saver do
|
|
117
117
|
it 'should save driver render' do
|
118
118
|
driver_mock.should_receive(:render).with(screenshot_path)
|
119
119
|
|
120
|
-
saver.save
|
120
|
+
saver.save
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe "with webkit debug driver" do
|
125
|
+
before do
|
126
|
+
capybara_mock.stub(:current_driver).and_return(:webkit_debug)
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'should save driver render' do
|
130
|
+
driver_mock.should_receive(:render).with(screenshot_path)
|
131
|
+
|
132
|
+
saver.save
|
121
133
|
end
|
122
134
|
end
|
123
135
|
|
@@ -130,14 +142,14 @@ describe Capybara::Screenshot::Saver do
|
|
130
142
|
it 'should save driver render' do
|
131
143
|
driver_mock.should_receive(:render).with(screenshot_path)
|
132
144
|
|
133
|
-
saver.save
|
145
|
+
saver.save
|
134
146
|
end
|
135
147
|
|
136
148
|
it 'should output warning about unknown results' do
|
137
|
-
# Not pure mock testing
|
149
|
+
# Not pure mock testing
|
138
150
|
saver.should_receive(:warn).with(/screenshot driver for 'unknown'.*unknown results/).and_return(nil)
|
139
151
|
|
140
|
-
saver.save
|
152
|
+
saver.save
|
141
153
|
end
|
142
154
|
end
|
143
155
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: capybara-screenshot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: capybara
|
@@ -100,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
100
|
version: '0'
|
101
101
|
segments:
|
102
102
|
- 0
|
103
|
-
hash:
|
103
|
+
hash: 2034660228649349470
|
104
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
109
|
version: '0'
|
110
110
|
segments:
|
111
111
|
- 0
|
112
|
-
hash:
|
112
|
+
hash: 2034660228649349470
|
113
113
|
requirements: []
|
114
114
|
rubyforge_project: capybara-screenshot
|
115
115
|
rubygems_version: 1.8.24
|