Pages

Friday 16 January 2015

Taking screenshots on test failure in testNG + Selenium Web driver.

Taking screenshot on test failure is very important part of the test execution process especially when you are executing large number of test cases.

Follow the below mentioned steps to take the advantages of this feature :
  1. Use TestNG 
  2. Create Your own listener and override the methods of TestListener adapter class 
Create a new listener class as below:
package Your_package_name;
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.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;

public class ScreenShotOnFailure extends TestListenerAdapter {
 @Override
 public void onTestFailure(ITestResult result) {
  WebDriver driver = WebDriverManager.getDriverInstance();
  if(driver!=null){
   System.out.println("Snapshot for: " + result.getMethod().getMethodName());
  File file=new File("Screenshot_File");
  if(!result.isSuccess()){
   File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
       //Needs Commons IO library
       try {
         FileUtils.copyFile(scrFile, new File(file.getAbsolutePath()+ "/screenshot/shot_" + result.getMethod().getMethodName()  + "()" + ".jpg")); 
         } 
       catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
     Reporter.setCurrentTestResult(result);
     }
  }
  }
 }


By using ScreenShotOnFailure class, it will override the onTestFailure method of TestListenerAdapter Class when exception is thrown.

Now call the listener in the testng.xml file as below:

<listeners  useDefaultListeners="false">
       <listener class-name="org.testng.reporters.TestHTMLReporter" />  
       <listener class-name="org.testng.reporters.JUnitXMLReporter" />
       <listener class-name="org.testng.reporters.XMLReporter" />
       <listener class-name="package_name.ScreenShotOnFailure"/>    
</listeners>


Use @listeners annotation to call the ScreenShotOnFailure listener class as below:

package package_name;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.TestListenerAdapter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(ScreenShotOnFailure.class)
public class TestDR extends TestListenerAdapter { 
  ReadXclData RD= new ReadXclData();
  ComProFile c = new ComProFile ();
 @BeforeClass
 public void setUp() throws Exception {
  WebDriverManager.startDriver(); 
 }
Now run your script and you will get the screenshots in screenshot folder as below:




Read previous post How to set all the locators in the Object Property file in Java

5 comments:

  1. Thanks a lot for sharing this post, really very helpful

    ReplyDelete
  2. hi sumer, There is test case with 5 steps, if my test case fails at 5th step so your code ll take screenshot for only 5th step or it ll take all 1 to 5th step.

    ReplyDelete
    Replies
    1. If anyone knw plz replay..

      Delete
    2. It will take the screenshot whenever an exception is thrown during execution process i.e step 5.

      But I am not yet clear about the 5 test steps.Are they belongs to the single page or separate one?

      Thanks

      Delete