4

I am trying to take screenshot using Selenium Webdriver.

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(File_Location));

Now the issue is that for IE and Firefox the size of the screenshots differ even though I set dimension of window.

Can anybody give me suggestion how can I get screenshots of same size for all browser?

Kevin
  • 217
  • 6
  • 19

2 Answers2

1

You can't.

The different drivers implement screenshots in different ways, IEDriver will produce differing sizes based upon the version of IE.

There have been a series of bugs raised on this, this one probably tells you all you need to know and validates this answer:

https://code.google.com/p/selenium/issues/detail?id=5332&can=1&q=screenshot%20size&colspec=ID%20Stars%20Type%20Status%20Priority%20Milestone%20Owner%20Summary

Specifically

Project Member #3 james.h....@gmail.com Screenshots in the current WebDriver API mean full page screenshots. Unlike other browsers, the only way to get IE to render the full page is to have the IE window large enough to draw the entire page. In other words, to resize the window. For IE, we must either live with a resize, or not take full-page screenshots. This is a limitation of IE; there's nothing the IE driver can do to work around it. If you want to take a screenshot of only the visible viewport, without a resize, you can use the Windows API PrintWindow function. Status: WorkingAsIntended

Ardesco
  • 7,281
  • 26
  • 49
  • I tried resizing the windows but no luck. I still get images of different size. Also how to implement PrintWindow function in java. Can you anybody provide a snippet of code that I can see. – Kevin Apr 16 '13 at 18:11
  • I guess I wasn't clear and didn't put the first line of my answer in 48pt bold... **YOU CAN'T** get screenshots of a consistent size. Also using a Java Robot class won't help unless you are running the test on the machine that is driving the browser. – Ardesco Apr 16 '13 at 18:12
  • I was trying to follow project member comment "For IE, we must either live with a resize". My bad! – Kevin Apr 16 '13 at 18:31
  • Can there be anything done taking same size screenshot for firefox? – Kevin Apr 23 '13 at 18:11
  • There is no guarantee as to the screenshot size in any driver binding. – Ardesco Apr 23 '13 at 20:51
  • @Ardescon any idea if this has changed? – pelumi Jun 30 '15 at 10:22
  • Not yet, however when the W3C spec comes out of draft it may change to the whole DOM (http://www.w3.org/TR/webdriver/#screenshots). Bear in mind that when the W3C spec first comes out the existing driver implementations will probably not be compliant – Ardesco Jul 05 '15 at 15:23
0

Try this:

EventFiringWebDriver efDriver = new EventFiringWebDriver(driver);
File scrFile = efDriver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(File_Location));
user2087450
  • 339
  • 1
  • 11