要将每次执行的屏幕快照保存在selenium中的不同文件夹中

时间:2018-08-30 04:26:03

标签: java selenium selenium-webdriver

对于每次执行,屏幕快照应与日期和时间保存在不同的文件夹中。尝试使用以下代码,但无法正常工作。它是根据分钟数生成文件夹的,而不是执行时间。请帮助。。谢谢。

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];
}

enter image description here

2 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,则在一次“运行”中多次调用screenShot。因此,如果您希望文件夹具有“执行时间”或更确切地说是运行的开始时间,则还必须将其作为参数传递。否则,screenShot()将始终创建新的时间戳。 因此将签名更改为

public static String screenShot(WebDriver driver,
    String screenShotName, String testName, Date startTime) {...

,然后使用startTime代替Calendar对象。

答案 1 :(得分:0)

您必须在文件夹中添加测试名称,因为它会跟踪执行情况

如果您使用时间戳记,那么它也将更改为相同的测试

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;
    }

}