如何才能以最佳方式在 Selenium 2 中对测试进行截图?
我们可以在 Selenium webdriver 中对测试进行截图。捕捉截图是故障分析中至关重要的一步。这是一个三步骤的过程。
首先,我们将 webdriver 对象转换为名为 TakeScreenshot 的界面。然后需要使用 getScreenshotAs 方法来捕捉图像。要捕捉的图像的文件格式作为参数传递给该方法。
最后,将捕捉到图像的文件使用 FileUtils.copyFile 方法复制到一个位置。
语法
File s = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(s, new File("Image.png"));示例
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;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.apache.commons.io.FileUtils;
import java.io.File;
public class ScreenshotImg{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://tutorialspoint.com/index.htm");
//capture screenshot
File s = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(s, new File("Img.png"));
driver.quit();
}
}
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP