Skip to main content

Capture Screen Shot on Failure Test cases using TestNG.

Capture Screen Shot on Failure Test cases using TestNG.







Hi guys in this post we are going to see, how to capture ScreenShot on Failure Test cases using TestNG.

Taking Screenshot on the failure is very important to understand the Bug clearly to the manual Testers and developers.

Scenarios for Testcase Failures:

  1. Unable to find elements in the Webpages
  2. Timeout to finding Webelements.
  3. Assertion Failure
  4. Application Error 

In this post we are going to see how this going to work in action.

In this Example we are going to forcefully fail the testcase  by Asserting the title.(Giving the wrong Title)


Steps:

  1. We are creating a new class to capture ScreenShots alone.

import java.io.File;
import java.io.IOException;

import javax.print.attribute.standard.MediaSize.Other;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public class CaptureScreenshot {
      
       public static void takescreenshot(WebDriver driver,String Screenshotname) throws IOException
       {
              TakesScreenshot takescreenshot=(TakesScreenshot)driver;
              File source=takescreenshot.getScreenshotAs(OutputType.FILE);
              FileUtils.copyFile(source, newFile("./Screenshots/"+Screenshotname+".png"));
              System.out.println("Screenshot Taken Successfully!!!!");
             
       }

}


This class will just capture the screenshots by calling this Method in Test class

CaptureScreenshot.takescreenshot(driver, "Titlefailed");




      2. Create a new Class for the Test.

      3. On BeforeTest annotation Write code to  Open Browser and Navigate to http:\\automationplace.blogspot.com



@BeforeTest
       public void Setup()
       {
              driver=new FirefoxDriver();
              driver.manage().window().maximize();
              driver.get("http:\\automationplace.blogspot.com");
             
              //Make testcase fail by checking the Title
             
       }


       4. On the @Test annotation, Just verify the title (Here am forcefully failing the Testcase by giving wrong title on Expected String in Assert.equal method)

String ExpectedTitle="Automation";

 forcefully failing the Testcase by giving wrong title on the ExpectedTitle.

@Test
       public void verifytitle()
       {
              String ActualTitle=driver.getTitle();
              System.out.println(ActualTitle);
              //Removed "Place" string from Title to fail testcase
              Assert.assertEquals(ActualTitle, ExpectedTitle);
       }


     5. Capture ScreenShot on the @AfterMethod,

     Note :

1. We will use ITestResult Interface which will provide us the test case execution status and test case name.
2.@AfterMethod is another annotation of TestNG which will execute after evert test execution whether test case pass or fail @AfterMethod will always execute.

@AfterMethod
       public void TearDown(ITestResult result) throws IOException
       {
              //If the Testcase fail then only it enters to if condition block
             
              //.getStatus will return Test "Pass" or "Fail"
             
              System.out.println("Testcase status is"+result.getStatus());
              System.out.println("Iresult status is"+result.FAILURE);
             
              if(result.FAILURE == result.getStatus())
              {
                     //Now we need to capture Screenshot
                     //use CaptureScreenshot Class to Take Screenshot
                    
                     CaptureScreenshot.takescreenshot(driver"Titlefailed");
                    
              }
             
             
              driver.quit();
       }


1.If the Testcase fail then only it enters to if condition block

2.getStatus will return Test "Pass" or "Fail"




It will Store ScreenShot on the Project folder like this








Source Code: 


Class to take ScreenShot:  CreateScreenShot.java

import java.io.File;
import java.io.IOException;

import javax.print.attribute.standard.MediaSize.Other;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;


public class CaptureScreenshot {
      
       public static void takescreenshot(WebDriver driver,String Screenshotname) throws IOException
       {
              TakesScreenshot takescreenshot=(TakesScreenshot)driver;
              File source=takescreenshot.getScreenshotAs(OutputType.FILE);
              FileUtils.copyFile(source, newFile("./Screenshots/"+Screenshotname+".png"));
              System.out.println("Screenshot Taken Successfully!!!!");
             
       }

}



Class for the Testcase :FailedTestCase.java

import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class FailedTestcase {
      
       WebDriver driver;
       String ExpectedTitle="Automation";
      
       @BeforeTest
       public void Setup()
       {
              driver=new FirefoxDriver();
              driver.manage().window().maximize();
              driver.get("http:\\automationplace.blogspot.com");
             
              //Make testcase fail by checking the Title
             
       }
      
       //Failed Testcase
       @Test
       public void verifytitle()
       {
              String ActualTitle=driver.getTitle();
              System.out.println(ActualTitle);
              //Removed "Place" string from Title to fail testcase
              Assert.assertEquals(ActualTitle, ExpectedTitle);
       }
      
      
       @AfterMethod
       public void TearDown(ITestResult result) throws IOException
       {
              //If the Testcase fail then only it enters to if condition block
             
              //.getStatus will return Test "Pass" or "Fail"
             
              System.out.println("Testcase status is"+result.getStatus());
              System.out.println("Iresult status is"+result.FAILURE);
             
              if(result.FAILURE == result.getStatus())
              {
                     //Now we need to capture Screenshot
                     //use CaptureScreenshot Class to Take Screenshot
                    
                     CaptureScreenshot.takescreenshot(driver"Titlefailed");
                    
              }
             
             
              driver.quit();
       }
      


}


Run the program .

You find the Screenshot on the project folder after Execution.








----------------------------------------------------------------------------------------------------


package test;
import java.io.File;
import java.io.IOException;
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.firefox.FirefoxDriver;
import org.openqa.selenium.internal.selenesedriver.TakeScreenshot;
public class takingscrshot {

public static void main(String[] args) throws IOException{

 WebDriver driver= new FirefoxDriver();
 driver.get("https://www.google.com/");

 File scrFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
 FileUtils.copyFile(scrFile, new File("C:\\Local Disk D_9220122129\\tmp.jpg"));


}

}



Thats it..Have a great day.

Thankyou!!!!

Comments

Popular posts from this blog

Reporting in Selenium

How do you Handle reporting in your project ? Method 1 : Maven - Testng - Surefire report The project will be created in Maven with  SureFire Plugin  and  SuiteXmlFile  tag in the POM.xml . The testng.xml will be called in the POM.            <plugin>                 <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-surefire-plugin</artifactId>            <version>2.18.1</version>                 <configuration>                     <suiteXmlFiles>                         <suiteXmlFile>                             ...

Handling Modal Dialog Window in Selenium Webdriver

Handling Modal Dialog Window in Selenium Webdriver In this tutorial we are going to see how to handle " Modal Dialog Window "  using Selenium Webdriver Disadvantage using Selenium Use Can't handle elements which is inside  Modal Dialog Window. Using Robot Class Using Robot Class we can handle  Modal Dialog Window. In this tutorial Open  http://vodkabears.github.io/remodal/# Click on  Show  button In the  Modal Dialog Window.  Click on Follow button  Source Code : import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import java.awt.datatransfer.StringSelection; import java.awt.event.KeyEvent; public class robot_demo { public static void main(String[] args) throws InterruptedException, AWTException { WebDrive...

Autonium

About Autonium Autonium is a test automation framework for web applications. This framework is built by using Java and Selenium. It uses Maven project structure. Easy to understand and use it. Having basic idea on Java, Selenium, Maven and TestNG is good enough to start with Autonium. Let’s start Automation with Autonium --------------------------------------------------- Advantages Provides support to all major browsers –  Firefox, Chrome, IE and Safari . Capability of executing scripts on  Remote Machines  by using Selenium Server. Users can make use of Selenium Web driver methods and Autonium methods. Autonium simplifies  Test data management . Test data can be read from properties files. Scripts can be developed without  hard coding  test data . Identifiers can also be read from properties files and  hard coding   of identifiers can be avoided. Test data and Identifier v...