【问题标题】:Screen recording of a test execution in selenium using JAVA使用 JAVA 在 selenium 中执行测试的屏幕记录
【发布时间】:2015-06-07 13:38:40
【问题描述】:

我使用 java selenium 创建了一个自动化程序。我使用了 TestNG 框架。 我想记录在脚本执行期间正在执行的屏幕(视频),因此最好跟踪失败/通过的场景并查看执行过程。

谁能帮我解决这个问题,如何在运行自动化套件执行期间记录屏幕。

【问题讨论】:

标签: java selenium testng


【解决方案1】:

查看此 API(Monte 库):http://www.seleniummonster.com/boost-up-your-selenium-tests-with-video-recording-capability/

这个链接:http://unmesh.me/2012/01/13/recording-screencast-of-selenium-tests-in-java/

示例代码(来自上面的链接):

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();
}

【讨论】:

    【解决方案2】:

    前面提到的解决方案的问题:-

    所有解决方案都回答了录制视频,从头到尾记录测试执行。如果自动化套件运行数小时,那么这将不是实用且最佳的解决方案。

    录制视频的主要目的是查看自动化测试用例失败时究竟发生了什么。所以准确地说,测试人员需要在测试用例失败前的最后 15 秒内进行视频录制。 PASSED 测试用例不需要任何记录

    理论上的解决方案 :-

    在 Windows 10 及更高版本上,Windows Xbox 游戏栏 [Windows+G] 能够捕捉最后 15 秒 [可自定义] 的视频。键盘快捷键 Windows+Alt+G 用于使用 Xbox Game Bar 捕获最后 15 秒的视频,并将存储在设置中提到的文件夹中。

    Selenium 自动化可以利用 Windows Xbox 游戏栏的此录制功能。 在您的 testNG 自动化项目中,在 testNG 侦听器的 onTestFailure 方法中,只需将代码添加到按键 Windows+Alt+G 即可捕获最后 15 秒的视频。这将只为失败的测试用例捕获视频,而不会为通过测试用例捕获视频。如果您使用的是 Java,那么您可以使用机器人库以编程方式发送按键。

    显示 Windows XBox 游戏栏的屏幕截图,它设置为捕获最后 15 秒。

    代码中的解决方案 :-

    我在 testNG 列表的
    中调用下面的 recordFailedTCVideo() 方法 public void onTestFailure(ITestResult result) 方法。 这只会为失败的测试用例录制最后 15 秒的视频。[而不是通过测试用例]

    视频说明:-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();
            }
        //}
    }
    

    更多视频解释:- https://www.youtube.com/watch?v=p6tJ1fVaRxw

    约束:-

    此解决方案不适用于非 Windows 平台。 XBar Game 实用程序不会记录 Windows Explorer 、文本文件等。虽然它记录浏览器没有问题。

    【讨论】:

      猜你喜欢
      • 2015-11-04
      • 2019-10-03
      • 2021-05-23
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 2017-06-12
      相关资源
      最近更新 更多