【发布时间】:2019-04-24 08:30:48
【问题描述】:
场景失败时尝试截屏时抛出空指针异常。我有一个动作类,我在其中污染了捕获屏幕截图方法。
public static String capture(WebDriver driver) throws NullPointerException, IOException {
File scrFile;
scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File Dest = new File("D:\\Dinu\\SeleniumReports\\Test" + System.currentTimeMillis() + ".jpeg");
String filepath = Dest.getAbsolutePath();
org.openqa.selenium.io.FileHandler.copy(scrFile, Dest);
return filepath;
}
范围报告是使用 Itestlisterner 接口实现的。下面给出的代码实现了给定的屏幕截图方法:
public synchronized void onTestFailure(ITestResult result) {
System.out.println((result.getMethod().getMethodName() + " failed!"));
test.get().fail(result.getThrowable());
try {
String screenshotPath = actions.capture(driver);
test.get().addScreenCaptureFromPath(screenshotPath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
【问题讨论】:
-
可能是因为它无法从您的源文件中找到快照图像。
-
但是 srcFile 有 TakeScreenshot 方法..
-
是的,它会在失败时在 src 文件上创建,但它不会复制到您的目标文件,因为它无法从 src 文件中获取数据。放置一个断点并调试它是否包含目标文件中的任何数据。
-
源文件没有被创建。它在这一行抛出空指针。scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
-
如何去掉空指针?
标签: selenium selenium-webdriver extentreports selenium-extent-report