通过使用下面的脚本,我每次只获取一个屏幕截图,它会覆盖相同的屏幕截图。
如果我想在间隔的某个小时间拍摄不同的屏幕截图,我该怎么办?
public void screenShot() throws IOException, InterruptedException
{
File scr=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File dest= new File("filPath/1.png");
FileUtils.copyFile(scr, dest);
Thread.sleep(3000);
}
答案 0 :(得分:2)
只需使用当前时间命名文件,此示例在当前分钟和小时值之后命名文件(当然您可以使用秒,毫秒,天,等等):
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
String directory = "filPath";
String fileName = "snapshot_"+ hour + "_"+ minute +".png";
File dest = new File(directory, fileName);
答案 1 :(得分:2)
您应该为文件名添加时间戳,例如
public void screenShot() throws IOException, InterruptedException
{
File scr=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File dest= new File("filPath/screenshot_"+timestamp()+".png");
FileUtils.copyFile(scr, dest);
Thread.sleep(3000);
}
public string timestamp() {
return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
}