How to Take Screenshot using Selenium ShutterBug
How to Take Screenshot using Selenium ShutterBug will discuss how we can take a screenshot of a particular website page when you work with selenium.
Need of Screenshot for Automated Testing:
From my own experience, getting screenshots from an automation framework whenever an automated test has problems or points of failure is a very helpful feature. This ability can visually indicate a bug or malfunction in the application’s code, a problem in the design of the test, or other types of irregularities. Selenium’s API provides the necessary code for adding this capability. Along with that we have different third part API’s which supports screenshot feature in selenium. Out of those Ashot is one of the familiar way which we discussed earlier.
Why ShutterBug?
Here is one more pretty easy way to take screenshot using ShutterBug. Beauty for shutterbug is Ultimate. It can do many cool features like Taking screenshot for particular element, Comparing different screens in run time, Marking particular element during screenshot , Blurring the image.. etc . Let’s go in detail of each about shutterbug.
To start with it , Add below dependency to maven project or Download jar files here.
<!-- https://mvnrepository.com/artifact/com.assertthat/selenium-shutterbug --> <dependency> <groupId>com.assertthat</groupId> <artifactId>selenium-shutterbug</artifactId> <version>0.9</version> </dependency>
Syntax:
Shutterbug.shootPage(<driver>).save();
In above syntax, shoot page is a method which captures the visible area on browser . Save method creates a folder with folder name as “screenshots” in run time and saves screenshot with name as “date and timestamp” in png format.
Sample Code:
public class ShutterbugScreenshot { @Test public void takeScreenshot() { WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.get("http://automationtesting.in"); Shutterbug.shootPage(driver).save(); driver.quit(); } }
However, We can get screenshot with our desired name in shutterbug.
Syntax:
Shutterbug.shootPage(<driverobject>). withName(<desiredname>).save();
In above syntax, shoot page is a method which captures the visible area on browser . withName method assigns desired name to screenshot take by shootpage method.Save method creates a folder with folder name as “screenshots” and saves screenshot in that folder.
Sample Code:
public class ShutterbugScreenshotwithName { @Test public void takeScreenshotwithDesiredName() { WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.get("http://automationtesting.in"); Shutterbug.shootPage(driver).withName("automationtestingscreenshot").save(); driver.quit(); } }
“Shutterbug.shootPage(driver).withName(“automationtestingscreenshot”).save()” — will take screenshot and saves it with “automationtestingscreenshot” name.
We can do many more with shutterbug. Please go through our next blogs to learn more on Shutterbug.
Reference Link:
https://github.com/assertthat/selenium-shutterbug