[Capybara] How I can take a screenshot ?

2633 views
Skip to first unread message

Alexander Klimenkov

unread,
Mar 13, 2011, 2:36:30 PM3/13/11
to Capybara
Hi All!

I am using cucumber + capybara with selenium driver. How I can take a
screenshot ?

I found some information about this problem (https://github.com/mocoso/
cucumber-screenshot, http://groups.google.com/group/ruby-capybara/browse_thread/thread/36ae20c033473c28),
but it does not work for me. I do not use Rails.

Any suggestions ?

Thanks

aslak hellesoy

unread,
Mar 14, 2011, 9:45:49 AM3/14/11
to ruby-c...@googlegroups.com, Alexander Klimenkov
On Sun, Mar 13, 2011 at 6:36 PM, Alexander Klimenkov
<alexander...@warecorp.com> wrote:
> Hi All!
>
> I am using cucumber + capybara with selenium driver. How I can take a
> screenshot ?
>

Try this: https://github.com/aslakhellesoy/cucumber/blob/master/examples/watir/features/support/screenshots.rb

Aslak

Jari Bakken

unread,
Mar 14, 2011, 11:09:20 AM3/14/11
to ruby-c...@googlegroups.com
On Sun, Mar 13, 2011 at 7:36 PM, Alexander Klimenkov
<alexander...@warecorp.com> wrote:
> Hi All!
>
> I am using cucumber + capybara with selenium driver. How I can take a
> screenshot ?
>

If you can get hold of the underlying driver from selenium-webdriver,
you can use this API to get nice screenshots (i.e. the full page):

driver.save_screenshot("/path/to/file.png")
driver.screenshot_as(:base64) => String

The interface is documented here:

http://rdoc.info/gems/selenium-webdriver/0.1.3/Selenium/WebDriver/DriverExtensions/TakesScreenshot

Jo Liss

unread,
Mar 14, 2011, 11:17:40 AM3/14/11
to ruby-c...@googlegroups.com, Alexander Klimenkov
On Sun, Mar 13, 2011 at 7:36 PM, Alexander Klimenkov
<alexander...@warecorp.com> wrote:
> I am using cucumber + capybara with selenium driver. How I can take a
> screenshot ?

This is admittedly not quite an answer to your question, but I'd like
to suggest that in many cases, the save_page (or save_and_open_page)
method will be good enough (though it will of course only save the
current state of the page in HTML, not take an actual screenshot).

Jo

--
Jo Liss
http://opinionated-programmer.com/

Jo Liss

unread,
Mar 14, 2011, 11:32:09 AM3/14/11
to ruby-c...@googlegroups.com, Jari Bakken
On Mon, Mar 14, 2011 at 4:09 PM, Jari Bakken <jari....@gmail.com> wrote:
> If you can get hold of the underlying driver from selenium-webdriver,
> you can use this API to get nice screenshots (i.e. the full page):
>
>  driver.save_screenshot("/path/to/file.png")
>  driver.screenshot_as(:base64) => String
>
> The interface is documented here:
>
>  http://rdoc.info/gems/selenium-webdriver/0.1.3/Selenium/WebDriver/DriverExtensions/TakesScreenshot

Nice. As it turns out, the Selenium driver can be accessed at
page.driver.browser, so you could write:

page.driver.browser.save_screenshot('file.png')

(Just tested this, and it works great.) Thanks for the tip!

Alex

unread,
Mar 14, 2011, 2:33:44 PM3/14/11
to ruby-c...@googlegroups.com
Thanks for All, this is help me!

I found one more solution.. May be it will be usefully for someone

File.open("path/to/file.png", 'wb') do |f|
f.write(Base64.decode64(page.driver.browser.screenshot_as(:base64)))
end

Reply all
Reply to author
Forward
0 new messages