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.

Which includes-

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";

System.setProperty("webdriver.chrome.driver", exePath);

//Chrome driver declaration
driver = new ChromeDriver();

//Launch url
driver.get("http://www.google.com");

//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
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);

//Move image to new destination
File DestFile=new File(filePath);

//Copy file at destination location
FileUtils.copyFile(SrcFile, DestFile);
}

}

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.

Shutterbug download

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;
Shutterbug.shootPage(driver,ScrollStrategy.VERTICALLY).withName(screenshot_name).save(”D:\\screenshot\\”);

}

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, )
{
driver.get("http://www.google.com");
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(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);

// Copy the element screenshot to disk
File screenshotLocation = new File("C:\\Screenshot\\images\\GoogleLogo_screenshot.png");
FileUtils.copyFile(screenshot, screenshotLocation);

}

Summary

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-

1- onFinish
2- onStart
3- onTestFailureButWithSuccessPercentage
4- onTestFailure
5- onTestSkipped
6- onTestStart
7- onTestSuccess

It is recomended to use screenshot capturing methods at onTestFailure only.