为屏幕截图创建动态文件名 Selenium Webdriver

Create Dynamic File Names for Screenshots Selenium Webdriver

在我的脚本中,我需要在多个位置截取屏幕截图。我想将所有屏幕截图保存在同一个文件夹中,每个文件都有一个唯一的名称。

有解释如何将 time/date 标记附加到文件的答案,我不需要。该脚本每周 运行 并将覆盖前一周的图像文件。

我正在使用 Java 和 Webdriver。这是我拥有的:

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"));

首先在文件名中加上time/date。然后在您的代码中,检查是否是新的一周。如果是,删除目录下的所有文件。

为了进行检查,您可以将代码最后 运行 时间的变量保存到磁盘。然后在启动程序后,您应该检查保存的最后 运行 时间是否与现在时间不在同一周。您还应该定期检查程序 运行 的时间。

因此,正如所讨论的那样,您可以找到解决方法:

@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);

让我知道这是否适合您。

试试这个示例程序来获取启动的屏幕截图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();
    }
}