对于每个执行,屏幕截图应保存在带有日期和时间的不同文件夹中。尝试使用以下代码,但它没有按预期工作。它正在根据分钟而不是执行生成文件夹。请帮助。.提前谢谢。
public static String screenShot(WebDriver driver,
String screenShotName, String testName) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
SimpleDateFormat formater1 = new SimpleDateFormat("dd_MM_yyyy_hh_mm");
try {
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File targetFile = new File("iWealthHKTestAutomation/resources/Screenshots_"+formater1.format(calendar.getTime())+"/"+ screenShotName+formater1.format(calendar.getTime()) + ".png");
FileUtils.copyFile(screenshotFile, targetFile);
return screenShotName;
} catch (Exception e) {
System.out.println("An exception occured while taking screenshot " + e.getCause());
return null;
}
}
public String getTestClassName(String testName) {
String[] reqTestClassname = testName.split("\.");
int i = reqTestClassname.length - 1;
System.out.println("Required Test Name : " + reqTestClassname[i]);
return reqTestClassname[i];
}
在此处输入图像描述
如果我理解正确,您会在一次"运行"中多次调用屏幕截图。因此,如果您希望文件夹具有"执行时间",或者更确切地说是运行的开始时间,则还必须将其作为参数传递。否则,screenShot(( 将始终创建一个新的时间戳。 因此,将签名更改为
public static String screenShot(WebDriver driver,
String screenShotName, String testName, Date startTime) {...
并使用 startTime 而不是日历对象。
您必须在文件夹中添加测试名称,因为它将跟踪执行
如果您使用时间戳,那么对于相同的测试,它也将发生变化
public static String screenShot(WebDriver driver,String screenShotName, String
testName) {
try {
File screenshotFile = ((TakesScreenshot)
driver).getScreenshotAs(OutputType.FILE);
File targetFile =
new File("iWealthHKTestAutomation/resources/Screenshots_"
+ testName /* pass testname param here like this*/
+ "/"
+ screenShotName
+ String.valueOf(new
SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()))
+ ".png");
FileUtils.copyFile(screenshotFile, targetFile);
return screenShotName;
} catch (Exception e) {
System.out.println("An exception occured while taking screenshot " + e.getCause());
return null;
}
}