Abstract– Capturing screenshot is an essential feature of a test automation framework, They help in defect analyses once after test execution is over.
In this blog post we will see how to capture screenshot, there are different ways to capture screenshots.
1- Capture full Page
2- Capturng full page(enabling scrolldown)-Using Shutterbug
3- Capturing screenshot of particular web object-
1- Capturing full page(without doing scrolldown)-
Below method can help in capturing screenshot of full page(excluding scroll bar)
public void TestScenario() throws Exception
WebDriver driver ;
String exePath = "E:\\chromedriver_win32\\chromedriver.exe";
//Chrome driver declaration
driver = new ChromeDriver();
//Call take screenshot function
this.ScreenCapture(driver, "c://test.png") ; }
Above code is calling ScreenCapture, where 2 arguments are required- driver and file path, Have a look below
public static ScreenCapture(WebDriver driver,String filePath) throws Exception
//Referring web driver object to TakeScreenshot, which is being sent as an argument
TakesScreenshot scrShot =((TakesScreenshot)driver);
//Call getScreenshotAs method to create an image file
//Move image to new destination
File DestFile=new File(filePath);
//Copy file at destination location
2- Capturng full page(enabling scrolldown)-Using Shutterbug-
Shutterbug is a java jar file using which fullpage screenshot can be taken, Even when there is vertical scroolbar is available, Below code can be used afterconfiguring shutterbug jar file.
Call below code whenever fullpage screencapture is required, Only driver and screenshot name needs to be passed as an argument.
public void FullPageScreenCapture(driver, String screenshotname)
String timeStamp = new SimpleDateFormat(“yy_MM_dd_HH_mm_ss”).format(new Date());
String screenshot_name = null ;
screenshot_name = screenshotname+”_”+timeStamp;
3- Capturing screenshot of particular web object- Button, Lable, Radiobutton and checkbox’s etc
It may be required to captured only specific webobject in a webpage, sam ecan be done using below piece of code. In Which that particular webobject is passed while calling below method along with driver object.
public void WebObjectCapture(driver, )
WebElement ele = driver.findElement(By.name("q"));
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = ele.getLocation();
// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
ImageIO.write(eleScreenshot, "png", screenshot);
// Copy the element screenshot to disk
File screenshotLocation = new File("C:\\Screenshot\\images\\GoogleLogo_screenshot.png");
In User Interface/Browser automation testing it is recomended to capture all test logs, Pass, Fail therefore significance of screenshots is alot in test automation. We discuss this a lot in our Selenium Trainings.
A neatly crafted automation framework does have all required loogers configured which comprises of text logs along with screenshots etc.
Calling above screenshot method needs to be done in such a way so that minimal code is required to write, Best way is to call them in Listners class. While exteding ITestListners to a class there are couple of methods where above method can be calleds.
Methods in ITestListners are-
It is recomended to use screenshot capturing methods at onTestFailure only.