Permalink
Browse files

Support `Capybara::Session#save_screenshot`

  • Loading branch information...
1 parent 5b47225 commit 1c161c6092e5936c1cf4983ed230ca1a6dc812ef @tricknotes tricknotes committed Jul 10, 2012
View
@@ -474,6 +474,14 @@ that this may break with more complicated expressions:
result = page.evaluate_script('4 + 4');
```
+### Saving screenshot
+
+In drivers which support it, you can save screenshot:
+
+```ruby
+page.save_screenshot('screenshot.png')
+```
+
### Debugging
It can be useful to take a snapshot of the page as it currently is and take a
@@ -27,6 +27,10 @@ def evaluate_script(script)
raise Capybara::NotSupportedByDriverError
end
+ def save_screenshot(path, options={})
+ raise Capybara::NotSupportedByDriverError
+ end
+
def response_headers
raise Capybara::NotSupportedByDriverError
end
@@ -62,6 +62,10 @@ def evaluate_script(script)
browser.execute_script "return #{script}"
end
+ def save_screenshot(path, options={})
+ browser.save_screenshot(path)
+ end
+
def reset!
# Use instance variable directly so we avoid starting the browser just to reset the session
if @browser
View
@@ -41,7 +41,7 @@ class Session
:body, :html, :current_url, :current_host, :evaluate_script, :source,
:visit, :within, :within_fieldset, :within_table,
:within_frame, :within_window, :current_path, :save_page,
- :save_and_open_page, :reset_session!
+ :save_and_open_page, :save_screenshot, :reset_session!
]
DSL_METHODS = NODE_METHODS + SESSION_METHODS
@@ -282,6 +282,16 @@ def save_and_open_page(file_name=nil)
Capybara.save_and_open_page(body, file_name)
end
+ ##
+ #
+ # Save a screenshot of page
+ #
+ # @param [String] path A string of image path
+ # @option [Hash] options Options for saving screenshot
+ def save_screenshot(path, options={})
+ driver.save_screenshot(path, options)
+ end
+
def document
@document ||= Capybara::Node::Document.new(self, driver)
end
@@ -324,3 +324,19 @@
@driver.body.should match %r{http://.*/referer_base}
end
end
+
+shared_examples_for "driver with screenshot support" do
+ describe '#save_screenshot' do
+ let(:image_path) { File.join(Dir.tmpdir, 'capybara-screenshot.png') }
+
+ before do
+ @driver.visit '/'
+ @driver.save_screenshot image_path
+ end
+
+ it "should generate PNG file" do
+ magic = File.read(image_path, 4)
+ magic.should eq "\x89PNG"
+ end
+ end
+end
@@ -0,0 +1,29 @@
+shared_examples_for "session with screenshot support" do
+ describe "#save_screenshot" do
+ let(:image_path) { File.join(Dir.tmpdir, 'capybara-screenshot.png') }
+
+ before do
+ @session.visit '/'
+ @session.save_screenshot image_path
+ end
+
+ it "should generate PNG file" do
+ magic = File.read(image_path, 4)
+ magic.should eq "\x89PNG"
+ end
+ end
+end
+
+shared_examples_for "session without screenshot support" do
+ describe "#save_screenshot" do
+ before do
+ @session.visit('/')
+ end
+
+ it "should raise an error" do
+ running {
+ @session.save_screenshot 'raise_error.png'
+ }.should raise_error(Capybara::NotSupportedByDriverError)
+ end
+ end
+end
@@ -13,6 +13,7 @@
it_should_behave_like "driver without status code support"
it_should_behave_like "driver with cookies support"
it_should_behave_like "driver with referer support"
+ it_should_behave_like "driver with screenshot support"
unless RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
it "should not interfere with forking child processes" do
View
@@ -213,6 +213,7 @@
it_should_behave_like "session"
it_should_behave_like "session without javascript support"
+ it_should_behave_like "session without screenshot support"
it "should be possible to include it in another class" do
klass = Class.new do
@@ -55,6 +55,7 @@
it_should_behave_like "session"
it_should_behave_like "session without javascript support"
+ it_should_behave_like "session without screenshot support"
it_should_behave_like "session with headers support"
it_should_behave_like "session with status code support"
end
@@ -20,6 +20,7 @@
it_should_behave_like "session"
it_should_behave_like "session with javascript support"
+ it_should_behave_like "session with screenshot support"
it_should_behave_like "session without headers support"
it_should_behave_like "session without status code support"
end

0 comments on commit 1c161c6

Please sign in to comment.