Screenshots in Selenium WebDriver


While doing manual testing, we always have the machine in front of us to check what happened when the test case failed. In automation we rely on the assertion message that we print in case of failure. In addition to that we can also have screenshot of the browser in case of failure due to assertion or unavailability of any web element.
The code to take the screenshot make use of getScreenshotAs method of TakesScreenshot interface. Following code will take screenshot of the web page opened by webDriver instance.

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\testScreenShot.jpg"));

Now in order to take screenshot in case of test failure we will use AfterMethod annotation of TestNG. In the AfterMethod annotation we will use ITestResult interface's getStatus() method that returns the test result and in case of failure we can use the above commands to take screenshot. Code snippet to take screenshot on test failure-

@AfterMethod 
public void takeScreenShotOnFailure(ITestResult testResult) throws IOException { 
	if (testResult.getStatus() == ITestResult.FAILURE) { 
		File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
		FileUtils.copyFile(scrFile, new File("D:\\testScreenShot.jpg")); 
	} 
}

Following is the complete sample script for google calculator test that is intentionally made to fail by asserting result for 2+2 as 5, in order to test the screenshot functionality. Just change the path of the screenshot file to the desired location and run the test script.

package myTestPackage;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

public class calculatorTest {

	//Create firfox driver's instance
	WebDriver driver = new FirefoxDriver();
	
	@Test
	//Tests google calculator
	public void googleCalculator() throws IOException{
		try{
		
		//Set implicit wait of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		
		//Launch google
		driver.get("http://www.google.co.in");
		
		//Write 2+2 in google textbox
		WebElement googleTextBox = driver.findElement(By.id("gbqfq"));
		googleTextBox.sendKeys("2+2");
		
		//Click on searchButton
		WebElement searchButton = driver.findElement(By.id("gbqfb"));
		searchButton.click();
		
		//Get result from calculator
		WebElement calculatorTextBox = driver.findElement(By.id("cwos"));
		String result = calculatorTextBox.getText();
		
		//Intentionaly checking for wrong calculation of 2+2=5 in order to take screenshot for faling test
		Assert.assertEquals(result, "5");
	}
	
	catch(Exception e){
		Assert.fail(); //To fail test in case of any element identification failure		
		}
	
	}
		
	@AfterMethod
	public void takeScreenShotOnFailure(ITestResult testResult) throws IOException {
		if (testResult.getStatus() == ITestResult.FAILURE) {
			System.out.println(testResult.getStatus());
			File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
			FileUtils.copyFile(scrFile, new File("D:\\testScreenShot.jpg"));
	   }        
	}
	
}