Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Screen recording of a test execution in selenium using JAVA

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.

like image 309
Zoso619 Avatar asked Apr 02 '15 14:04

Zoso619


People also ask

How do I record my screen for testing?

For manual tests and automated tests this would be the machine that runs the tests. Select Screen and Voice Recorder and then choose Configure. The Configure Diagnostic Data Adapter – Screen and Voice Recorder dialog box is displayed. (Optional) Select Enable voice recording to capture audio content in your recording.

Which Selenium tool provides record and playback feature?

Record and playback is one of the key features of Selenium IDE, among other features that help manual testers get a taste of automation and its benefits. Selenium IDE has a rich set of commands powered by Selenese, allowing you to record various interactions of a web app with the browser.

Can Selenium IDE can run HTML test cases recorded?

Selenium IDE (Integrated Development Environment) is primarily a record/run tool that a test case developer uses to develop Selenium Test cases. Selenium IDE is an easy-to-use tool from the Selenium Test Suite and can even be used by someone new to developing automated test cases for their web applications.

Can we automate video using Selenium?

My answer: you CAN and should automate video testing! In this post, I'd like to show you how.


2 Answers

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();
}
like image 91
LittlePanda Avatar answered Nov 14 '22 22:11

LittlePanda


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.

like image 29
Vikas Piprade Avatar answered Nov 15 '22 00:11

Vikas Piprade