Skip to content

Commit

Permalink
Support Capybara::Session#save_screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
tricknotes committed Jul 10, 2012
1 parent 5b47225 commit 1c161c6
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/selenium/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion lib/capybara/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions lib/capybara/spec/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions lib/capybara/spec/session/screenshot.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/driver/selenium_driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/session/rack_test_session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/session/selenium_session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1c161c6

Please sign in to comment.