Selenium or Watir Webdriver, how to save resized screenshot?
Asked Answered
J

2

1

I try to get a resized 400x400 screenshot of google. I try this both in Selenium and Watir with no success.

require 'watir-webdriver'
b = Watir::Browser.new
b.goto 'google.com'
b.window.resize_to(400,400)
b.driver.save_screenshot("screenshot.jpg")

I always get the screenshot with the original browser size.

Any idea how can I save it resized to 400x400?

Jauch answered 24/7, 2013 at 7:58 Comment(0)
S
4

At present, WebDriver defines a screenshot as a "full page screenshot". That is, the entire DOM should be represented by the image generated by the save_screenshot method. The fact that the Chrome driver doesn't generate a screenshot of the full DOM is a bug in the Chrome driver. So the real answer is that there is no way to generate a screenshot of only the browser view port using WebDriver.

Having said that, it might be possible to use other programmatic means to accomplish this, depending on your OS. On Windows, for example, it would be pretty easy to get the desktop window's window handle (HWND), capture the image of the desktop (using the Windows GetDesktop and PrintWindow APIs), and cropping it using the coordinates supplied by the WebDriver Window API.

Suiter answered 24/7, 2013 at 12:16 Comment(1)
Any idea of a solution for os x?Jauch
M
0

I was able to reproduce the problem with Firefox, but Chrome generates resized screenshot. My guess is that it is a bug in FirefoxDriver. Take a look if the problem is already reported in Selenium bug tracker and if not, report it.

Meryl answered 24/7, 2013 at 8:59 Comment(2)
i40.tinypic.com/2va06dy.png this is what I see using the Chrome Driver. Does this happen to you?Jauch
Yes, something like that.Schnorr

© 2022 - 2024 — McMap. All rights reserved.