How to Take Screenshot in Selenium

Step 1) Convert web driver object to TakeScreenshot Step 2) Call getScreenshotAs method to create image file Step 3) Copy file to Desired Location Example: In this example we will take screen capture of http://demo.guru99.com/V4/ & save it as C:/Test.png Here is the screenshot code in selenium:

What is Ashot API?

Ashot is a third party utility by Yandex supported by Selenium WebDriver to capture the Screenshots. It takes a screenshot of an individual WebElement as well as a full-page screenshot of a page, which is more significant than screen size.

How to download and configure Ashot API?

There are two methods to configure Ashot API

Using Maven Manually without using any tool

To configure through Maven:

Go to https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot Click on the latest version, for now. It is 1.5.4 Copy the Dependency code and add to your pom.xml file

Save the file, and Maven will add the jar to your build path And now you are ready!!!

To configure manually without any dependency tool

Go to https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot Click on the latest version, for now. It is 1.5.4 Click on the jar, download and save it on your machine

Add the jar file in your build path: In Eclipse, right-click on the project -> go to properties -> Build Path -> Libraries -> Add External jars Select the jar file Apply and Close

Capture Full Page Screenshot with AShot API

Step 1) Create an Ashot object and call takeScreenshot() method if you just want the screenshot for the screen size page. But if you want a screenshot of the page bigger then the screen size, call the shootingStrategy() method before calling takeScreenshot() method to set up the policy. Then call a method takeScreenshot() passing the webdriver, for example, Here 1000 is scrolled out time in milliseconds, so for taking a screenshot, the program will scroll for each 1000 msec. Step 2): Now, get the image from the screenshot and write it to the file. You can provide the file type as jpg, png, etc. Taking a full-page screenshot of a page which is bigger than screen size. Example: Here is the example of capturing a full-page screenshot of http://demo.guru99.com/test/guru99home/ and save to file “screenshot.jpg.” Due to using the ShootingStrategy class of Ashot API, we will be able to capture a full image of a page bigger than the screen size. Here is the screenshot code in selenium program:

Taking a screenshot of a particular element of the page

Example: Here is the example of capturing element screenshot of Guru 99 logo on http://demo.guru99.com/test/guru99home/ page and save to file “ElementScreenshot.jpg”. Here is the screenshot code in selenium:

Image Comparison using AShot

Summary

Ashot API is a freeware from Yandex. It is a utility for taking a screenshot in Selenium. It helps you to take a screenshot of an individual WebElement on different platforms like desktop browsers, iOS Simulator Mobile Safari, Android Emulator Browser. It can take a page screenshot of a page bigger than screen size. This feature has been removed in selenium version 3, so Ashot API is a good option. It can decorate the screenshots. It provides a screenshot comparison.