在我的脚本中,我需要在多个位置截取屏幕截图。我想将所有屏幕截图保存在同一文件夹中,每个文件都有一个唯一的名称。
有答案解释如何将时间/日期戳添加到文件中,这是我不需要的。该脚本将每周运行,并将覆盖前一周的图像文件。
我正在使用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"));
答案 0 :(得分:1)
正如所讨论的那样,你可以通过这个方法:
@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();
}
}
答案 1 :(得分:0)
首先在文件名中添加时间/日期。然后在您的代码中,检查它是否是新的一周。如果是这样,请删除目录的所有文件。
对于检查,您可以将变量保存到代码上次运行时的磁盘。然后在启动程序后,您应该检查保存的上次运行时间是否与现在时间不同。您还应该检查程序定期运行的时间。
答案 2 :(得分:0)
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.
答案 3 :(得分:0)
试用此示例程序以获取已启动网址的屏幕截图:
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();
}
}