簡體   English   中英

要將每次執行的屏幕快照保存在selenium中的不同文件夾中

[英]To save screenshots of each execution in different folder in selenium

對於每次執行,屏幕快照應與日期和時間保存在不同的文件夾中。 嘗試使用下面的代碼,但未按預期工作。它是根據分鍾數而不是執行時間生成文件夾。請幫助。。

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。 因此,如果您希望文件夾具有“執行時間”或更確切地說是運行的開始時間,則還必須將其作為參數傳遞。 否則,screenShot()將始終創建新的時間戳。 因此將簽名更改為

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

並使用startTime代替Calendar對象。

您必須在文件夾中添加testname,因為它將跟蹤執行

如果您使用時間戳記,則它也將更改為相同的測試

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM