Take Screenshot on Failure of Selenium Test Cases - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Saturday 27 June 2015

Take Screenshot on Failure of Selenium Test Cases


To Take Screenshot on failures we will be writing code in @AfterMethod

As we know that an @AfterMethod is called when a method with @Test annotation has finished.

So after every @Test annotation it will check if the test case is failing .If it fails @AfterMethod will take the Screenshot with the function name of the Test Case


@AfterMethod
       public void takeScreenShotOnFailure(ITestResult testResult) throws IOException
       {
              if (testResult.getStatus() == ITestResult.FAILURE)// Here ITestResult is an interface
              {
                     File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                     DateFormat dateFormat = new SimpleDateFormat("dd_MMM_yyyy__hh_mm_ssaa");
                     String destDir = "abc/surefire-reports/screenshots";
                     System.out.println("destdir"+destDir);
                     new File(destDir).mkdirs();
                     String destFile = dateFormat.format(new Date())+" "+testResult.getName()+ ".jpg";
                     System.out.println("destFile"+destFile);
                     try {
                           FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile));
                     } catch (IOException e) {
                           e.printStackTrace();
                     }
                    
              }
       }



Example

package
testSuite;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

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.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestListener extends TestListenerAdapter {
       WebDriver driver;
       String baseURL = "https://www.linkedin.com/";
       @BeforeTest
       public void setup() {
              driver = new FirefoxDriver();
              driver.manage().window().maximize();
       }


       @Test(priority=1)
       public void verifyLoginPageText() {
              driver.navigate().to(baseURL);
              System.out.println("Verify login page test started");
              WebElement element = driver.findElement(By.cssSelector(".header>h2"));
              String headerText = element.getText();
              Assert.assertEquals(headerText, "Get stted – it’s free.");
       }

       @AfterMethod
       public void takeScreenShotOnFailure(ITestResult testResult) throws IOException
       {
              if (testResult.getStatus() == ITestResult.FAILURE)
              {
                     File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                     DateFormat dateFormat = new SimpleDateFormat("dd_MMM_yyyy__hh_mm_ssaa");
                     String destDir = "failed-testcases/surefire-reports/screenshots";
                     System.out.println("destdir"+destDir);
                     new File(destDir).mkdirs();
                     String destFile = dateFormat.format(new Date())+" "+testResult.getName()+ ".jpg";
                     System.out.println("destFile"+destFile);
                     try {
                           FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile));
                     } catch (IOException e) {
                           e.printStackTrace();
                     }
                    
              }
       }

}


No comments:

Post a Comment