How to capture Screen Shot for failed test cases

In this post we will capture the screen shots in two ways:
1. By using @AfterMethod annotation method in test class.
2. By using TestNG listener ITestListener.

Let's have a look at the first method:
1. By using @AfterMethod annotation method in test class.

Utility Class:
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 TakeScreenShot {
 
 public static void screenShot(WebDriver driver, String filename) {
  TakesScreenshot takesScreenshot = (TakesScreenshot) driver;
  File source = takesScreenshot.getScreenshotAs(OutputType.FILE);
  try {
   FileUtils.copyFile(source, new File(System.getProperty("user.dir")+"\\ScreenShot\\"+filename+".png"));
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.getMessage();
  }
 }
}

TestClass:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.utility.TakeScreenShot;

public class ScreenShotTest {
 WebDriver driver;

 @BeforeMethod
 public void setup() {
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("https://www.google.com/");
 }
 @Test
 public void testCase1() {
  driver.findElement(By.name("q")).sendKeys("ScreenShot Demo");
  Assert.assertTrue(false);
 }
 @Test
 public void testCase2() {
  Assert.assertTrue(true);
 }
 @AfterMethod
 public void tearDown(ITestResult result) {
  if(result.FAILURE==result.getStatus())
  {
   TakeScreenShot.screenShot(driver, result.getName());
  }
  driver.close();
 }
}

2. By using TestNG listener ITestListener.

BaseClass: We have created baseclass where we kept setup method to launch the browser and application and screenShot method to capture the screenshots
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class BaseClass {
 
 public static WebDriver driver;

 public void setup() {
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("https://www.google.com/");
 }
 public void screenShot(String filename) {
  String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
  TakesScreenshot takesScreenshot = (TakesScreenshot) driver;
  File source = takesScreenshot.getScreenshotAs(OutputType.FILE);
  try {
   FileUtils.copyFile(source, new File(System.getProperty("user.dir")+"\\ScreenShot\\"+filename+"_"+dateName+".png"));
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.getMessage();
  }
 }
}

Utility Class: In ListenerClass we have classed screenShot method from base class. This class implements ITestListener
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import com.base.BaseClass;

public class ListenerClass extends BaseClass implements ITestListener {

 public void onTestStart(ITestResult result) {
  // TODO Auto-generated method stub
 }

 public void onTestSuccess(ITestResult result) {
  // TODO Auto-generated method stub 
 }
 public void onTestFailure(ITestResult result) {
  screenShot(result.getName()); 
 }
 public void onTestSkipped(ITestResult result) {
  // TODO Auto-generated method stub
 }
 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
  // TODO Auto-generated method stub 
 }
 public void onStart(ITestContext context) {
  // TODO Auto-generated method stub
 }

 public void onFinish(ITestContext context) {
  // TODO Auto-generated method stub
  
 }
}

We have two test classes here:
ScreenShotTest:
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import com.base.BaseClass;

public class ScreenShotTest extends BaseClass {
 
 @Test
 public void testCase1() {
  setup();
  driver.findElement(By.name("q")).sendKeys("ScreenShot Demo");
  Assert.assertTrue(false);
 }
 @Test
 public void testCase2() {
  setup();
  Assert.assertTrue(false);
 }
 @AfterMethod
 public void tearDown() {
  driver.close();
 }
}

2nd TestClass:
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import com.base.BaseClass;

public class TestClass extends BaseClass {
 
 @Test
 public void testCase3() {
  setup();
  driver.findElement(By.name("q")).sendKeys("ScreenShot Demo");
  Assert.assertTrue(true);
 }
 @Test
 public void testCase4() {
  setup();
  Assert.assertTrue(false);
 }
 
 @AfterMethod
 public void tearDown() {
  driver.close();
 }
}

TestNG.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
<listener class-name="com.utility.ListenerClass"></listener>
</listeners>
  <test name="Test">
    <classes>
      <class name="com.testPackage.ScreenShotTest"/>
      <class name="com.testPackage.TestClass"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Please refer below YouTube video for more details:


No comments:

Post a Comment