当我们截取屏幕截图时,它将保存当前日期和时间

时间:2013-04-08 15:00:36

标签: selenium-webdriver

我正在创建两种方法,如下所示:

public static String currentDateTime() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Calendar cal = Calendar.getInstance();
    String cal1 = dateFormat.format(cal.getTime());
    return cal1;
}
public static void screenShot(WebDriver driver) throws IOException {
    File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    String datetime = GenericFunctions.currentDateTime();
    FileUtils.copyFile(scrnsht, new File("D:\\Screenshot1.png"+datetime));
}

但是,当我在我的WebDriver脚本中使用截屏方法然后创建了一个文件夹,我想要截取当前日期和时间的截图,请指出我错在哪里。

2 个答案:

答案 0 :(得分:1)

将“datetime”替换为“cal1”:

FileUtils.copyFile(scrnsht, new File("D:\\Screenshot1.png"+datetime));

OR

试试这个

Calendar calendar = Calendar.getInstance();
SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
String destFileName = screenShotPath.getAbsoluteFile()+File.separator+"failure_screenshots_"+formater.format(calendar.getTime())+".png";

答案 1 :(得分:0)

public static void screenShot(WebDriver driver) throws IOException {
File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String datetime = GenericFunctions.currentDateTime();
FileUtils.copyFile(scrnsht, new File("D:\\Screenshot1.png"+datetime));

}

您的文件将仅显示为Screenshot1.png,日期时间不会添加到任何地方。 而不是你可以FileUtils.copyFile(scrnsht, new File("D:\\Screenshot1"+datetime+".png"));

还建议使用1,你可以使用循环,或者如果已经存在的情况1中的函数添加2或类似的东西

相关问题