我使用这段代码在中捕获屏幕快照:
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenshot, new File("C:\\projectScreenshots\\homePageScreenshot.png"));
} catch (IOException e) {
System.out.println(e.getMessage());
}但是,当我像下面这样将这些代码打包到函数中时,它会抛出NullpointerException。
public static void TakesScreenshot()
{
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenshot, new File("C:\\projectScreenshots\\homePageScreenshot.png"));
} catch (IOException e) {
System.out.println(e.getMessage());
}
}发布于 2022-06-24 13:17:13
我使用以下函数解决了NullpointerException问题:
public static void takeScreenshot(String file, WebDriver driver) {
try{
File scrFile=((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("C:\\Users\\"+file+".png"));
} catch(IOException ioe) {
ioe.getStackTrace();
}
}要调用上面的函数,我们需要在脚本中使用这一行:
takeScreenshot("Screenshot", driver);https://stackoverflow.com/questions/72741004
复制相似问题