Automation Using Selenium Webdriver
Showing posts with label How to capture screenshot for failed test cases in Selenium Webdriver. Show all posts
Showing posts with label How to capture screenshot for failed test cases in Selenium Webdriver. Show all posts

Friday 7 October 2016

How to capture screenshot for failed test cases in Selenium Webdriver

How to capture screenshot for failed test cases in Selenium Webdriver

Writing Selenium Webdriver script is not enough everyone can design script nowadays. We need to design script in such a way that we can utilize script code as much as possible.This article will talk about Capture screenshot in selenium for failed test cases.
I am a big fan of screenshots in Automation because it helps me a lot to identify the exact issue.

Generally, scripts fail in 2 situations.

1-If script has some issue (some locator has been changed or application has some changes)- In this case, we need to maintain our Selenium script.
2-Due to application issue- In this case, we need to inform to respective point of contact (Manual Tester or Developer)

How to Capture screenshot in selenium for failed test cases

Previously I have covered a post on capture screenshot in Selenium so if you have not gone through the previos post then I will highly recommend you to please go through the post and youtube as well.

Today we will see something different How to capture a screenshot for failed test cases in Selenium Webdriver.
Here I will be using two new topics which will help us to achieve the same.
1-We will use ITestResult Interface which will provide us the test case execution status and test case name.
Please refer official doc for ITestResult
2- @AfterMethod is another annotation of TestNG which will execute after every test execution whether test case pass or fail @AfterMethod will always execute.
Please refer official doc for @AfterMethod
If you are new to TestNG and want to explore more on TestNG then I will recommend you to please go through below TestNG topics.
I have covered a couple of topics for TestNG. You can check all TestNG Tutorial here
Now let’s get started and will see through a complete program
// Create a package in eclipse
package captureScreenshot;

// Import all classes and interface
import java.io.File;
import library.Utility;
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.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class FacebookScreenshot {
// Create Webdriver reference
WebDriver driver;
@Test
public void captureScreenshot() throws Exception
{
// Initiate Firefox browser
driver=new FirefoxDriver();
// Maximize the browser
driver.manage().window().maximize();
// Pass application url
driver.get("http://www.facebook.com");
// Here we are forcefully passing wrong id so that it will fail our testcase
driver.findElement(By.xpath(".//*[@id='emailasdasdas']")).sendKeys("jaganthota");
}
// It will execute after every test execution 
@AfterMethod
public void tearDown(ITestResult result)
{
// Here will compare if test is failing then only it will enter into if condition
if(ITestResult.FAILURE==result.getStatus())
{
try 
{
// Create refernce of TakesScreenshot
TakesScreenshot ts=(TakesScreenshot)driver;
// Call method to capture screenshot
File source=ts.getScreenshotAs(OutputType.FILE);
// Copy files to specific location here it will save all screenshot in our project home directory and
// result.getName() will return name of test case so that screenshot name will be same
FileUtils.copyFile(source, new File("./Screenshots/"+result.getName()+".png"));
System.out.println("Screenshot taken");
catch (Exception e)
{ 
System.out.println("Exception while taking screenshot "+e.getMessage());
}
// close application
driver.quit();
}
}
Above code will execute fine and if test case will fail it will capture the screenshot. Check below screenshot
                                       

Above code is fine but still we need to enhance our code so that we can reuse.

We will create Utility class which will have one method which will capture the screenshot.
Code to create method for screenshot
package library;
import java.io.File;import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
public class Utility 
{
public static void captureScreenshot(WebDriver driver,String screenshotName)
{
try 
{
TakesScreenshot ts=(TakesScreenshot)driver;
File source=ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File("./Screenshots/"+screenshotName+".png"));
System.out.println("Screenshot taken");
catch (Exception e)
{
System.out.println("Exception while taking screenshot "+e.getMessage());
}
}