9

I have created an automation program using java selenium.I have used TestNG framework. I want to record (video) of the screen those are getting executed during the script execution so it is better to track the failed/passed scenario and view the execution process.

Can any one help me with this, how to record the screen during running the automation suite execution.

Arghavan
  • 1,125
  • 1
  • 11
  • 17
Zoso619
  • 163
  • 1
  • 3
  • 15
  • Are you sure you want a video? Seems a bit heavy for failed tests. You can take a screenshot as detailed here: http://stackoverflow.com/questions/3422262/take-a-screenshot-with-selenium-webdriver – Steve Apr 02 '15 at 14:40

2 Answers2

4

Problems with solution mentioned before :-

All solutions answered to record video, records test execution from start to end. If automation suite run for hours then this won't be practical and optimal solution.

Main purpose of record video is to SEE what exactly happened when automation test case failed. So precisely testers need video recording of LAST 15 SECONDS BEFORE TEST CASE FAILED. They don't need any recording for PASSED test cases

Solution in theory :-

On Windows 10 onwards, Windows Xbox Game bar [Windows+G] has ability to capture LAST 15 seconds [customizable] of video. Keyboard shortcut Windows+Alt+G is use to capture last 15 seconds of video using XBox Game Bar and it would be stored in folder mentioned in settings.

Selenium automation can exploit this recording feature of Windows Xbox Game bar. In your testNG automation project, in onTestFailure method of testNG listener just add code to keypress Windows+Alt+G to capture last 15 seconds video. This would capture video for ONLY failed test cases and never for PASS test cases. If you are using Java then you can use Robot library to send keypress programatically.

Screenshots showing Windows XBox game Bar and it's setting to capture last 15 seconds.

enter image description here

enter image description here

Solution in Code :-

I am calling below recordFailedTCVideo() method from testNG listner's
public void onTestFailure(ITestResult result) method. This will just record last 15 seconds of video ONLY for failed test cases.[and not for PASS test cases]

Video explanation :- https://www.youtube.com/watch?v=p6tJ1fVaRxw

public void recordFailedTCVideo(ITestResult result) {
    //private void pressKey() {
    System.out.println("In recordFailedTCVideo::***In Try Block *** Video for test case failed " + result.getName());
    commonUtility.logger.error("BaseTest::recordFailedTCVideo::***In Try Block ***  Video for test case failed " + result.getName());
    
        try {
            // Useing Robot class to keypres Win+Alt+G which will capture last 15 seconds of video
            Robot r = new Robot();
            r.keyPress(KeyEvent.VK_WINDOWS );
            Thread.sleep(1000);
            r.keyPress(KeyEvent.VK_ALT );
            Thread.sleep(1000);
            r.keyPress(KeyEvent.VK_G );
            Thread.sleep(5000);
            
            r.keyRelease(KeyEvent.VK_WINDOWS);
            Thread.sleep(1000);
            r.keyRelease(KeyEvent.VK_ALT);
            Thread.sleep(1000);
            r.keyRelease(KeyEvent.VK_G);
            Thread.sleep(5000);
          
            /// Copy Video saved to desired location
            
            File srcDir = new File(commonUtility.prop.getProperty("VIDEO_CAPTURE_DEFAULT_LOCATION"));

            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd HHmmss");  
            LocalDateTime now = LocalDateTime.now();  
          
            String destination = ".\\ScreenshotsAndVideos\\" + dtf.format(now) ;
            File destDir = new File(destination);

            try {
                System.out.println("In RecordFailedTCVideo::Source Folder is "+ srcDir +" Destination Folder = " + destDir);
                commonUtility.logger.error("In RecordFailedTCVideo::Source Folder is "+ srcDir +" Destination Folder = " + destDir);

                FileUtils.moveDirectory(srcDir, destDir);
                
            } catch (IOException e) {
                e.printStackTrace();
            }
                    
        } catch (Exception e) {
            System.out.println("In recordFailedTCVideo::***In Catch Block ***\n" +e);
            commonUtility.logger.error("BaseTest::recordFailedTCVideo::***In Catch Block *** \n"+e );
            
            e.printStackTrace();
        }
    //}
}

Further Video explanation :- https://www.youtube.com/watch?v=p6tJ1fVaRxw

Constraints :-

This solution is not for non-Windows platforms. XBar Game utility would not record Windows Explorer , text files etc. Although it records browsers without problem.

Vikas Piprade
  • 272
  • 2
  • 7
3

See this API (Monte Library): http://www.seleniummonster.com/boost-up-your-selenium-tests-with-video-recording-capability/

and this link: http://unmesh.me/2012/01/13/recording-screencast-of-selenium-tests-in-java/

Example Code (from above links):

public void startRecording() throws Exception
{
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
this.screenRecorder = new ScreenRecorder(gc,
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, 24, FrameRateKey, Rational.valueOf(15),QualityKey, 1.0f,KeyFrameIntervalKey, 15 * 60),new Format(MediaTypeKey,MediaType.VIDEO, EncodingKey, "black",FrameRateKey, Rational.valueOf(30)),null);
this.screenRecorder.start();
}
public void stopRecording() throws Exception
{
this.screenRecorder.stop();
}
LittlePanda
  • 2,496
  • 1
  • 21
  • 33