• Selenium Video Tutorials

Selenium - 捕获视频



Selenium Webdriver 可以借助另一个第三方 API 来捕获视频。捕获视频有助于调试失败的测试脚本。此外,还可以将测试执行的捕获视频与日志一起共享给开发人员,以便更快地解决问题。

捕获的测试用例视频可以作为证据添加到任何测试管理工具(如 JIRA、ALM 等)中。可以与其他项目利益相关者共享,以提高理解和可见性。

Monte 屏幕录制 API

Selenium Webdriver 本身无法捕获视频。它必须与另一个名为 Monte Screen Recorder 的 API 集成。为了使用 Monte Screen Recorder 来录制视频,我们需要添加与其相关的 Maven 依赖项。

使用 Monte 屏幕录制 API 捕获视频的步骤

步骤 1 - 导航到以下链接 -

https://mvnrepository.com/artifact/

Selenium Capture Videos 1

步骤 2 - 单击版本下方的 <version 链接>。在上图中,版本号为 0.7.7.0。

Selenium Capture Videos 2

将 Maven 选项卡下可用的依赖项添加到我们将创建的 Maven 项目的 pom.xml 中。添加此依赖项后,我们将更新并刷新我们的 maven 项目。在上面的示例中,以下依赖项如下所示 -

<!-- https://mvnrepository.com/artifact/com.github.stephenc.monte/monte-screen-recorder --> <dependency> <groupId>com.github.stephenc.monte</groupId> <artifactId>monte-screen-recorder</artifactId> <version>0.7.7.0</version> </dependency>

步骤 3 - 我们将借助从源代码获取的实用程序代码:https://www.randelshofer.ch/monte/ 将 Monte 屏幕录制 API 与我们的测试集成。

示例:捕获视频(录制测试)

让我们以以下页面为例,我们将从启动应用程序、单击“主要级别 1”标签旁边的复选框并退出浏览器开始录制整个测试。之后,我们将停止录制。测试完成后,整个录制内容将保存在测试项目下新创建的test-recordings文件夹中。在此文件夹下,将有一个名为 - main-<当前日期和时间> 的 .avi 文件,其中包含测试的整个录制内容。

Selenium Capture Videos 3

ScreenRecorderUtil.java 实用程序类的代码实现。

package org.example; import java.awt.AWTException; import java.awt.Dimension; import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import java.awt.Toolkit; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.monte.media.Format; import org.monte.media.FormatKeys.MediaType; import org.monte.media.Registry; import org.monte.media.math.Rational; import org.monte.screenrecorder.ScreenRecorder; import static org.monte.media.AudioFormatKeys.*; import static org.monte.media.VideoFormatKeys.*; public class ScreenRecorderUtil extends ScreenRecorder { public static ScreenRecorder screenRecorder; public String name; public ScreenRecorderUtil(GraphicsConfiguration cfg, Rectangle captureArea, Format fileFormat, Format screenFormat, Format mouseFormat, Format audioFormat, File movieFolder, String name) throws IOException, AWTException { super(cfg, captureArea, fileFormat, screenFormat, mouseFormat, audioFormat, movieFolder); this.name = name; } @Override protected File createMovieFile(Format fileFormat) throws IOException { if (!movieFolder.exists()) { movieFolder.mkdirs(); } else if (!movieFolder.isDirectory()) { throw new IOException("\"" + movieFolder + "\" is not a directory."); } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss"); return new File(movieFolder, name + "-" + dateFormat.format(new Date()) + "." + Registry.getInstance().getExtension(fileFormat)); } public static void startRecord(String methodName) throws Exception { File file = new File("./test-recordings/"); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int width = screenSize.width; int height = screenSize.height; Rectangle captureSize = new Rectangle(0, 0, width, height); GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); screenRecorder = new ScreenRecorderUtil(gc, captureSize, 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, file, methodName); screenRecorder.start(); } public static void stopRecord() throws Exception { screenRecorder.stop(); } }

ScreenRecorderUtil.java 的源代码:https://www.randelshofer.ch/monte/

测试类 CaptureVid.java 的代码实现。

package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class CaptureVid { public static void main(String[] args) throws Exception { // start screen recording ScreenRecorderUtil.startRecord("main"); // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage where we will identify checkbox driver.get("https://tutorialspoint.com/selenium/practice/check-box.php"); // Identify element then click WebElement c = driver.findElement(By.xpath("//*[@id='c_bs_1']")); c.click(); // verify if checkbox is selected Boolean result = c.isSelected(); System.out.println("Checkbox is selected: " + result); //Closing browser driver.quit(); // stop recording ScreenRecorderUtil.stopRecord(); } }

pom.xml 中的依赖项。

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>SeleniumJava</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>16</maven.compiler.source> <maven.compiler.target>16</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.11.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.github.stephenc.monte/monte-screen-recorder --> <dependency> <groupId>com.github.stephenc.monte</groupId> <artifactId>monte-screen-recorder</artifactId> <version>0.7.7.0</version> </dependency> </dependencies> </project>

请注意,在我们的示例中,实用程序文件 ScreenRecorderUtil.java 和测试类文件 CaptureVid.java 创建在同一个包下 - example

pom.xml 中的依赖项。

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>SeleniumJava</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>16</maven.compiler.source> <maven.compiler.target>16</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.11.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.github.stephenc.monte/monte-screen-recorder --> <dependency> <groupId>com.github.stephenc.monte</groupId> <artifactId>monte-screen-recorder</artifactId> <version>0.7.7.0</version> </dependency> </dependencies> </project>

输出

Checkbox is selected: true

Process finished with exit code 0

在上面的示例中,我们首先启动了一个应用程序并单击了复选框,并使用控制台中的消息验证了复选框是否已选中 - 复选框已选中:true

最后,收到消息进程已完成,退出代码为 0,表示代码已成功执行。

此外,视频录制文件名为main-2024-02-26 16.29.avi,位于test-recordings文件夹下,已在项目目录中创建。测试执行后刷新项目文件夹后,将可以看到此文件夹。单击它后,我们将看到我们示例中执行的所有测试步骤的视频。

Selenium Capture Videos 4

因此,在本教程中,我们讨论了如何使用 Selenium Webdriver 捕获视频。

广告