本人在学习完UiAutomator,继续selenium2java的时候,想把UiAutomator的一些方法搬到selenium2java里面来,期间遇到截图保存的一个坑,就是图片命名。由于window系统不允许“:”在文件名中出现,导致截图失败。
//截图命名为当前时间保存桌面
public static void takeScreenshotByNow(WebDriver driver) throws IOException {
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String file = "C:\\Users\\user\\Desktop\\"+getNow()+".png";
FileUtils.copyFile(srcFile,new File(file));
}
下面是getnow()的方法,坑就在这里了,我用汉字替换了“:”。
//获取当前时间
public static String getNow() {
Date time = new Date();
SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH点mm分ss秒");
String c = now.format(time);
return(c);
}
顺便写一个截图保存桌面的自定义名字的方法
//截图重命名保存至桌面
public static void takeScreenshotByName(WebDriver driver, String name) throws IOException {
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String file = "C:\\Users\\user\\Desktop\\"+name+".png";
FileUtils.copyFile(srcFile,new File(file));
}