java 为屏幕截图创建动态文件名 Selenium Webdriver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29043315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Dynamic File Names for Screenshots Selenium Webdriver
提问by user3977009x
In my script, I need to take screenshots at multiple locations. I want to save all screenshots in the same folder, with a unique name for each file.
在我的脚本中,我需要在多个位置截取屏幕截图。我想将所有屏幕截图保存在同一个文件夹中,每个文件都有一个唯一的名称。
There are answers explaining how to append time/date stamps to the file, which I do not need. The script will run every week and will overwrite the previous week's image files.
有一些答案解释了如何将时间/日期戳附加到文件中,我不需要。该脚本将每周运行并覆盖前一周的图像文件。
I'm using Java with Webdriver. Here is what I have:
我在 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"));
回答by Vivek Singh
So as discussed you can get a way through this:
因此,正如所讨论的,您可以通过以下方式解决问题:
@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();
}
}
回答by Rahul Anand
I designed below method to get screenshot file name:
我设计了以下方法来获取屏幕截图文件名:
/**
* 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"));
}
for parameter I used below to get current method name:
对于我在下面用于获取当前方法名称的参数:
String name = new Object(){}.getClass().getEnclosingMethod().getName();
getScreenShot(name);
let me know if this works for you.
让我知道这是否适合您。
回答by Sriram Rajendran
Try this Sample Program to get the Screenshot of Launched Url:
试试这个示例程序来获取 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();
}
}
回答by ch271828n
First add time/date to the file name. Then in your code, check if it is a new week. If so, delete all files of the dir.
首先在文件名中添加时间/日期。然后在您的代码中,检查它是否是新的一周。如果是这样,请删除该目录的所有文件。
For the check, you may save a variable to disk of the last running time of your code. Then after starting program, you should check if the saved last running time is not the same week as now time. You should also check time when program is running on a regular basis.
为了检查,您可以将变量保存到代码最后运行时间的磁盘中。然后在启动程序后,您应该检查保存的上次运行时间是否与现在时间不是同一周。您还应该定期检查程序运行的时间。