捕捉视频
有时候我们未必能够分析故障只需用日志文件或截图的帮助。有时捕获完整的执行视频帮助。让我们了解如何捕捉视频。
我们将利用Monte媒体库的执行相同。
配置
第1步:导航到URL - http://www.randelshofer.ch/monte/index.htmll和下载屏幕记录JAR,如下图所示。
第2步:下载后,添加JAR文件添加到当前项目的库。
第3步:我们会利用Java的AWT包来初始化显卡配置。
GraphicsConfiguration gc = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration();
第4步:它采用下列参数创建ScreenRecorder的一个实例。
示例
我们将捕获简单的测试执行视频 - 百分比计算。
package com.demo.test; import static org.monte.media.FormatKeys.EncodingKey; import static org.monte.media.FormatKeys.FrameRateKey; import static org.monte.media.FormatKeys.KeyFrameIntervalKey; import static org.monte.media.FormatKeys.MIME_AVI; import static org.monte.media.FormatKeys.MediaTypeKey; import static org.monte.media.FormatKeys.MimeTypeKey; import static org.monte.media.VideoFormatKeys.CompressorNameKey; import static org.monte.media.VideoFormatKeys.DepthKey; import static org.monte.media.VideoFormatKeys.ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE; import static org.monte.media.VideoFormatKeys.QualityKey; import java.awt.AWTException; import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.monte.media.Format; import org.monte.media.FormatKeys.MediaType; import org.monte.media.math.Rational; import org.monte.screenrecorder.ScreenRecorder; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class webdriverdemo { private static ScreenRecorder screenRecorder; public static void main(String[] args) throws IOException, AWTException { GraphicsConfiguration gconfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); screenRecorder = new ScreenRecorder(gconfig, 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); WebDriver driver = new ChromeDriver(); // 开始捕获视频 screenRecorder.start(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.navigate().to("https://www.baidu.com/"); driver.manage().window().maximize(); for (int i = 0; i < 3; i++) { driver.findElement(By.id("kw")).sendKeys("selenium", Keys.ENTER); driver.navigate().forward(); driver.navigate().back(); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("D:screenshotsscreenshots1.jpg")); // 停止捕获视频 screenRecorder.stop(); } } |
输出
录制的视频保存在“C:users<<UserName>>Videos”文件夹,如下图所示。
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。