执行 JUnit
Selenium 测试时,它将创建浏览器会话的屏幕捕获以显示错误状况。您可以将这些屏幕捕获包括在测试用例或测试套件运行的执行结果中,以供日后查看。
关于此任务
在
JUnit
Selenium 适配器执行测试时,它会设置运行
JUnit
Selenium 测试的 JVM 中名为“selenium.screenshot.dir”的 Java™ 系统属性。此 Java 系统属性的值是运行该适配器的机器上的某个目录的完整路径。在每个
JUnit
Selenium 测试执行结束时,将搜索由“selenium.screenshot.dir”指定的目录以查找图像文件(.png、.jpg、.jpeg、.gif、.tif 和 .bmp)。如果找到任何图像文件,那么会在质量管理服务器上创建文件附件,并将对这些文件附件的引用包括在执行结果中。
示例
以下示例 JUnit 代码显示如何生成浏览器当前状态的屏幕捕获并将该屏幕捕获文件放置到由“selenium.screenshot.dir”指定的目录中。
@Test
public void testWikipediaJUnit4_test1() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("searchInput")).clear();
driver.findElement(By.id("searchInput")).sendKeys("blueberries");
takeScreenShot("blueberry2.png"); // Capture screenshot of current state
driver.findElement(By.name("go")).click();
driver.findElement(By.cssSelector("p > i > a[title=\"Vaccinium\"]")).click();
takeScreenShot("vaccinium2.png"); // Capture screenshot of current state
driver.findElement(By.linkText("huckleberry")).click();
takeScreenShot("huckleberry2.png"); // Capture screenshot of current state
}
private void takeScreenShot(String fname) throws Exception {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String imageFileDir = System.getProperty("selenium.screenshot.dir");
if (imageFileDir == null)
imageFileDir = System.getProperty("java.io.tmpdir");
FileUtils.copyFile(scrFile, new File(imageFileDir, fname));
}