PAGE 16: CAPTURE SCREEN SHOT ON FAILURE TEST CASES USING TestNG

SCENARIOS FOR THE TEST CASE FAILURES

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


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.





No comments:

About Me

My photo
Pune, Maharastra, India
You can reach me out at : jimmiamrit@gmail.com