簡體   English   中英

為屏幕截圖創建動態文件名 Selenium Webdriver

[英]Create Dynamic File Names for Screenshots Selenium Webdriver

在我的腳本中,我需要在多個位置截取屏幕截圖。 我想將所有屏幕截圖保存在同一個文件夾中,每個文件都有一個唯一的名稱。

有一些答案解釋了如何將時間/日期戳附加到文件中,我不需要。 該腳本將每周運行並覆蓋前一周的圖像文件。

我在 Webdriver 中使用 Java。 這是我所擁有的:

String screenshots;
screenshots = "C:/eStore/Projects/Screenshots/Catalog/";  //location for images

File screenshots = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshots, new File("screenshots + 01Belts.jpg"));

因此,正如所討論的,您可以通過以下方式解決問題:

@AfterMethod
public void tearDown(ITestResult result) throws IOException {
    String location = "C:/eStore/Projects/Screenshots/Catalog/";  //location for images
    String methodname = result.getName(); // fetching test method name
    try {
        File screenshots = ((TakesScreenshot) augmentedDriver)
                               .getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(
            screenshots,
            new File(location + methodName + "_" + ".png");
    } catch (Exception e) {
          e.printStackTrace();
    } finally {
          driver.quit();
    }
}

首先在文件名中添加時間/日期。 然后在您的代碼中,檢查它是否是新的一周。 如果是這樣,請刪除該目錄的所有文件。

為了檢查,您可以將變量保存到代碼最后運行時間的磁盤中。 然后在啟動程序后,您應該檢查保存的上次運行時間是否與現在時間不是同一周。 您還應該定期檢查程序運行的時間。

我設計了以下方法來獲取屏幕截圖文件名:

/**
     * Take window screeshot
     * @param fileName
     * @return null
     * @throws IOException 
     */
    public void getScreenShot(String fileName) throws IOException{
        DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy h-m-s");
        Date date = new Date();
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("E:\\selenium logs\\"+fileName+"-"+dateFormat.format(date)+".png"));
    }

對於我在下面用於獲取當前方法名稱的參數:

String name = new Object(){}.getClass().getEnclosingMethod().getName();
getScreenShot(name);

讓我知道這是否適合您。

試試這個示例程序來獲取 Launched Url 的屏幕截圖:

package com.simple.com;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;    

public class ScreenShot {
    public static WebDriver driver;
    @Parameters({"filename"})
    @Test
    public static void snap(String filename) throws IOException {

        System.setProperty("webdriver.gecko.driver", "D:\\Selenium\\geckodriver-v0.18.0-win32\\geckodriver.exe");
        driver = new FirefoxDriver();

        String url ="https://www.inc.com/peter-economy/17-super-positive-quotes-that-will-inspire-you-to-be-exceptional.html";
        driver.get(url);

        DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy h-m-s");
        Date date = new Date();
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        filename="Super_Selenium";
        FileUtils.copyFile(scrFile, new File("D:\\SeleniumScreenshot_pass\\"+filename+"-"+dateFormat.format(date)+".png"));
        driver.quit();
    }
}

暫無
暫無

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

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