前面提到的解决方案的问题:-
所有解决方案都回答了录制视频,从头到尾记录测试执行。如果自动化套件运行数小时,那么这将不是实用且最佳的解决方案。
录制视频的主要目的是查看自动化测试用例失败时究竟发生了什么。所以准确地说,测试人员需要在测试用例失败前的最后 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 、文本文件等。虽然它记录浏览器没有问题。