Sunday 21 April 2013

Video recording of failed test Scripts in selenium framework

Failure analysis is very crucial part for any automation team. We can automate the tests , we can automate the process of executing the tests once build is arrived for testing,but automation tester have to have spare some time in manually analyzing the report to make sure whether it’s a product issue or not. To make failure analysis job bit easy, here I tried to explain how to capture the screens on failures and how to do video recording of the test during execution.

What is main challenge in failure analysis ? 

If test is taking 3 minutes to get executed and failing somewhere at last set of lines, during failure analysis, complete test needs to be rerun to find where exactly it’s failing. Instead of that, this feature will help us, to quickly play the video. In fast track, we can reach to the step, where exactly it failed and can take corrective action before rerun.
Sometimes it also happens test gets failed due to some intermittent time out issues, and it became difficult to reproduce those issues in rerun. For such failures this video file will support us in analyzing the page or component taking more than expected time to get rendered.


How can I Capture the screen shots on test failures with selenuim ? 

Selenium is having a method which capture the screen shot, but this method need to be use smartly in framework to capture the screen shots only when the test gets failed.so by referring the screen shot test can be fixed
Attached code is an example how to capture the screen shots on failures. 
public class KeywordException extends RuntimeException {

 /**
  * Throw this exception if keyword is failing.
  * @param message
  */
 public static String callerClass;
 public static String callingMethodName;
 private static WebDriver driver = BasicTestContext.driv;
 public CaptureScreenShotOnException(String message) {
  super(message);

  File scrFile = ((TakesScreenshot) driver)
    .getScreenshotAs(OutputType.FILE);
  String screenShotPath = "C:\\screenshot\\" + callerClass + "_"
    + callingMethodName + "_"
    + getCurrentTimeStamp("yyyyMMMdd_hhmmss") + ".png";
  try {
   FileUtils.copyFile(scrFile, new File(screenShotPath));
   System.out.println("Saved screenshot: " + screenShotPath);
   System.out.println("Failed : " + callerClass + "_"
     + callingMethodName + " . REASON : "+message);
  } catch (IOException e1) {
   e1.printStackTrace();
  }
 }
}
 
How to use it in your test ?

public void setText(String value) {

            if (INF_Common_Utility.SyncForElement("xpath=" + getRootXpath()))
                  browser.type("xpath=" + getRootXpath(), value);
            else
                  throw new CaptureScreenShotOnException("Timeout recieved while finding element with Xpath: xpath=" + getRootXpath());
                 
      }
Capturing the Video on failures for the selenium tests:

You can evolve your automation framework by introducing a feature of recording the selenium test execution.Using this feature selenium test execution can be recorded as a video file.it will generate one video file for each failed tests.
 
 How can I implement it in my framework ?

To implement this feature, we used Monte Media Library .This library is having a class ScreenRecorder.this class provides the api for recording screencast of Selenium Tests in Java.
To play the recorded video: ScreenRecoder supports “AVI” format for recording the video. To play the video file generated in  “AVI” format, you need to install TSCC Codec (Techsmith Screen Capture Codec).
/**
       * This method will start the recording of the execution. recorded file will
       * get generated in the folder name as className_methodName. recording can
       * be started from any particular time.
       */

      public void startRecording() {

            RecordingClassName = Reflection.getCallerClass(2).getSimpleName();
            RecordingMethodName = new Throwable().fillInStackTrace()
                        .getStackTrace()[1].getMethodName();
            fileRecordingLocation = "c://screenshot//" + RecordingClassName + "_"
                        + RecordingMethodName;
            RecordedFile = new File(fileRecordingLocation);

            GraphicsConfiguration gc = GraphicsEnvironment//
                        .getLocalGraphicsEnvironment()//
                        .getDefaultScreenDevice()//
                        .getDefaultConfiguration();
            try {
                  ScreenRecorder = new ScreenRecorder(gc, null, new Format(
                              MediaTypeKey, MediaType.FILE, MimeTypeKey, MIME_AVI),
                              new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey,
                                          ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,
                                          CompressorNameKey,
                                          ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey,
                                          (int) 24, FrameRateKey, Rational.valueOf(15),
                                          QualityKey, 1.0f, KeyFrameIntervalKey,
                                          (int) (15 * 60)), new Format(MediaTypeKey,
                                          MediaType.VIDEO, EncodingKey, "black",
                                          FrameRateKey, Rational.valueOf(30)), null,
                              RecordedFile);
                  ScreenRecorder.start();
            } catch (IOException e) {
                  e.printStackTrace();
            } catch (AWTException e) {
                  e.printStackTrace();
            }
      }

      /**
       * This method will stop the recording.
       */
      public void stopRecording() {
            if (ScreenRecorder != null) {
                  try {
                        ScreenRecorder.stop();
                  } catch (IOException e) {
                        e.printStackTrace();
                  }
            }
      }
/**
* to delete the recorded file.if the test is passing then this method will delete the recorded file.
*/
      public void deleteRecordedFile() {
            if (RecordedFile != null) {
                  String[] children = RecordedFile.list();
                  for (int i = 0; i < children.length; i++) {
                        new File(RecordedFile, children[i]).delete();
                        System.out.println("Deleted this " + RecordedFile + "//"
                                    + children[i]);
                  }

                  // The directory is now empty so delete it
                  RecordedFile.delete();
            }
      }

      /**
       * This method works similar to the org.testng.Assert.assertTrue and also
       * performs following actions: 
       * it takes the screenshot of the active screen if the assertion fails.
       * it also deletes the recorded vedio file if the test is passed. Call this
       * method only once, at the end of the test.
       *
       * @param condition
       * @param message
       */
      public void finalAssertTrue(boolean condition, String message) {
            stopRecording();
            if (condition)
                  deleteRecordedFile();
            _assertTrue(condition, message);
      }

How to use it in the test ?
public class MySeleniumTest extends BasicSeleniumTest {

 @TestDetails(author = "Bhushan Pawar", description = "Show the demo of video recording.")
 @Test
 public void testToGenerateVideoFile() {

  startRecording();

  /**
   * <> 

   * <> 

   * <>
   * 
   */
  finalAssertTrue(condition,
    "test is failed and I want to see the video file where exactly it failed !!");
 }

This way, Video recording feature will assist the one who is maintaining the tests  .
Happy Automation testing !

15 comments:

  1. This works fine locally, but when I run it on Jenkins, in a VM, all I get is black videos. Help please..

    ReplyDelete
    Replies
    1. Sorry Andre, I missed out this comment.
      is your issue still open

      Delete
  2. Hi,
    I am also getting blank video while running script over jenkins.

    Any help?

    ReplyDelete
    Replies
    1. Hi Sorathiya,
      may i know where is the selenium server running

      Delete
    2. Let me explain u in detail

      We have 4 windows machine for Jenkins.
      1 is master 3 are slave.

      Selenium is running on all machines.

      Now I trigger my build with code u provided.

      Video get recorded properly but it shows only black screen.

      May be monte doesn't support remote execution!!!

      If monte is not working any suggestion what should we do??
      Thanks for reply.

      Delete
    3. Hi! I'm having the same issue. Did you manage to solve this issue?
      Any help would be much appreciated!

      Delete
    4. No, unfortunately, I couldn't solve it to have a sound and video recording too. I only had sound without screen recording :(

      Delete
  3. Can you help me to use, the ScreenRecorder with sound?
    I would like to record the sound and screen in one file as a movie.

    ReplyDelete
  4. Excellent Post, I welcome your interest about to post blogs. It will help many of them to update their skills in their interesting field.If you want to apply converted studying for Hadoop Real-Time Training and Live High-Level Knowledge instructor Trained from Hadoop Training Programme Course in Velachery to reach us
    Hadoop Training in Chennai
    Live Hadoop Scenario Training Institute in Chennai

    ReplyDelete
  5. Thanks for sharing useful information. I learned something new from your bog. Its very interesting and informative. keep updating. If you are looking for any Hadoop admin related information, please visit our website Hadoop admin training in bangalore

    ReplyDelete
  6. Your site is amazing and your blogs are informative and knowledgeable to my websites.This is one of the best tips in my life. I have in quite some time.Nicely written and great info.Thanks to share the more information's.
    web designing training in chennai

    web designing training in tambaram

    digital marketing training in chennai

    digital marketing training in tambaram

    rpa training in chennai

    rpa training in tambaram

    tally training in chennai

    tally training in tambaram

    ReplyDelete
  7. Your technical information related with java programming is very useful and interesting. Also share updated details about java in your website. Thanks for sharing this article.
    oracle training in chennai

    oracle training in velachery

    oracle dba training in chennai

    oracle dba training in velachery

    ccna training in chennai

    ccna training in velachery

    seo training in chennai

    seo training in velachery

    ReplyDelete