How to Take Different Types of Screenshots in Selenium WebDriver

Screenshots In WebDriver

Taking screenshots of the webpage under test is an important aspect of webdriver automation as it helps in analyzing the application status in case of failures. Knowing how the screen looked can be more valuable than getting the error/status messages from logs.

In a previous post (5 Solutions to Common WebDriver Doubts), we had covered how to take partial screenshots. Today we will be see how to take the different types of screenshots using Selenium WebDriver

We will be covering the following types of Screenshots
  • Regular Screenshot – Screenshot of the visible content
  • Partial Screenshot – Screenshot of a part of a page or an element
  • Full Page Screenshot – Screenshot of the full page (including below the fold content)

Regular Screenshot

Below is the code to take regular screenshot. Please note that this takes the full page screenshot for Firefox, but for ChromeDriver, only the visible content will be captured.

Partial Screenshot

Partial screenshot can be used to capture the screenshot of an element or any particular section of the screen. It takes the full screenshot and then crops out the required section using ImageIO library.

Full Page Screenshot

FirefoxDriver takes full page screenshots by default, but because of a bug in ChromeDriver, it only captures the visible content from the page in Chrome. There are a few ways to overcome this issue.

  • By zooming out to bring the full page contents into the visible screen, we can take the full page screenshot. But this reduces the clarity of the image.
SEE ALSO:   WebDriver Tutorial 4 : Better Window Handling
  • Another way would be to scroll through various sections of the page (using javascript) and combine the screenshots into one. This may not be a viable option if the page has static elements or if combining is not done properly.
  • Another method is to resize the window to the full size of the page (removing scrollbars) and taking screenshot. It is said to have worked in chrome in Perl and Ruby but I couldn’t make it work using Java. You can check the script here.
  • Ashot is a library which is used to make screenshots in WebDriver and its possible to make full page screenshots using it. You can find a tutorial for it here.

Further Reading:

Subscribe to our newsletter or follow us on Facebook or Twitter to get the latest updates.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *