Screenshot of Failing Tests in Selenium

Screenshots in Selenium WebDriver

Last updated on

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 messages 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.

How to take Screeshot

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


Take screenshot on failure

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.
One more thing to mention here is in order to uniquely identify the screenshot file, we are naming it as the name of the test method appended with the test parameters (passed through a data-provider). For getting the test name and parameters we are using the getName() and getParameters() methods of ITestResult interface. In case you are not using any data-provider(like in case of this demo) then you can just have the getName() method to print the test method name.

@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("errorScreenshots\\" + testResult.getName() + "-" 
				+ Arrays.toString(testResult.getParameters()) +  ".jpg"));
	} 
}


Demo with Google calculator

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

public class ScreenshotDemo{
	String driverExecutablePath = "lib\\chromedriver.exe";
	WebDriver driver;
	
	@BeforeTest
	public void setup(){
		System.setProperty("webdriver.chrome.driver", driverExecutablePath);
		driver = new ChromeDriver();
		
		//Set implicit wait of 3 seconds
		driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
	}
	
	
	@Test
	//Tests google calculator
	public void googleCalculator() throws IOException{
		
		//Launch google
		driver.get("http://www.google.co.in");
		
		//Write 2+2 in google textbox
		WebElement googleTextBox = driver.findElement(By.id("lst-ib"));
		googleTextBox.sendKeys("2+2");
		
		//Hit enter
		googleTextBox.sendKeys(Keys.ENTER);
		
		//Get result from calculator
		WebElement calculatorTextBox = driver.findElement(By.id("cwtltblr"));
		String result = calculatorTextBox.getText();
		
		//Intentionaly checking for wrong calculation of 2+2=5 in order to take screenshot for failing test
		Assert.assertEquals(result, "5");
	}
		
	@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("errorScreenshots\\" + testResult.getName() + "-" 
					+ Arrays.toString(testResult.getParameters()) +  ".jpg"));
	   }        
	}
	
}

Please comment below to share your views or ask any questions and don’t forget to check our complete selenium tutorial here.

Selenium webdriver Tutorial

1 thought on “Screenshots in Selenium WebDriver”

Leave a Comment