捕捉視頻

捕捉視頻

有時候我們未必能夠分析故障只需用日誌文件或截圖的幫助。有時捕獲完整的執行視頻幫助。讓我們瞭解如何捕捉視頻。

我們將利用Monte媒體庫的執行相同。

配置

第1步:導航到URL - http://www.randelshofer.ch/monte/index.htmll和下載屏幕記錄JAR,如下圖所示。

selenium_ide_174

第2步:下載後,添加JAR文件添加到當前項目的庫。

selenium_ide_175

第3步:我們會利用Java的AWT包來初始化顯卡配置。

GraphicsConfiguration gc = GraphicsEnvironment .getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration();

第4步:它採用下列參數創建ScreenRecorder的一個實例。

參數

描述

顯卡配置

提供了有關顯示畫面,例如大小和分辨率信息。

視頻壓縮格式

電影與幀/秒的數字輸出格式(AVI)。

鼠標光標和刷新速率的顏色

指定的鼠標光標的顏色和刷新速率

音頻格式

如果'NULL'音頻不會被記錄。

示例

我們將捕獲簡單的測試執行視頻 - 百分比計算。

import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.WebDriver; import org.openqa.selenium.By; import org.monte.media.math.Rational; import org.monte.media.Format; import org.monte.screenrecorder.ScreenRecorder; import static org.monte.media.AudioFormatKeys.*; import static org.monte.media.VideoFormatKeys.*; import java.awt.*; 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 FirefoxDriver(); // Start Capturing the Video screenRecorder.start(); //Puts a Implicit wait, Will wait for 10 seconds before throwing exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Launch website driver.navigate().to("http://www.calculator.net/"); //Maximize the browser driver.manage().window().maximize(); // Click on Math Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Percent Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Enter value 10 in the first number of the percent Calculator driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 50 in the second number of the percent Calculator driver.findElement(By.id("cpar2")).sendKeys("50"); // Click Calculate Button driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click(); // Get the Result Text based on its xpath String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText(); File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("D:screenshotsscreenshots1.jpg")); //Print a Log In message to the screen System.out.println(" The Result is " + result); //Close the Browser. driver.close(); // Stop the ScreenRecorder screenRecorder.stop(); } }

輸出

錄製的視頻保存在「C:users<>Videos」文件夾,如下圖所示。

selenium_ide_176