我会做一些事情来清理这个过程。
把你的屏幕截图代码放到一个函数中。
将日期时间戳记添加到您的屏幕截图文件名。这将给每个文件一个唯一的名称。
向截图文件名添加一个简短的错误字符串。这将使您能够快速查看每种错误类型的存在情况。奖励点数用于为每种错误类型创建一个文件夹,并只在该文件夹中放置该特定错误的屏幕截图。
您的脚本看起来像
String imageOutputPath = "the path to the folder that stores the screenshots";
if (driver.getTitle().contains("404"))
{
takeScreenshotOfPage(driver, imageOutputPath + "404 error " + getDateTimeStamp() + ".png");
}
else if (some other error)
{
takeScreenshotOfPage(driver, imageOutputPath + "some other error " + getDateTimeStamp() + ".png");
}
else if (yet another error)
{
takeScreenshotOfPage(driver, imageOutputPath + "yet another error " + getDateTimeStamp() + ".png");
}
和功能,采取截图
public static void takeScreenshotOfPage(WebDriver driver, String filePath) throws IOException
{
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage srcImage = ImageIO.read(srcFile);
ImageIO.write(srcImage, "png", new File(filePath));
}
和功能,使文件名友好日期时间戳
public static String getDateTimeStamp()
{
// creates a date time stamp that is Windows OS filename compatible
return new SimpleDateFormat("MMM dd HH.mm.ss").format(Calendar.getInstance().getTime());
}
你会最终结束w第i个是像下面这样的文件名,它们按错误类型整齐排序,每个文件名都有唯一的文件名。
404 error Dec 02 13.21.18.png
404 error Dec 02 13.22.29.png
404 error Dec 02 13.22.39.png
some other error Dec 02 13.21.25.png
some other error Dec 02 13.22.50.png
some other error Dec 02 13.22.56.png
yet another error Dec 02 13.21.34.png
yet another error Dec 02 13.23.02.png
yet another error Dec 02 13.23.09.png
你的问题是什么?如何创建截图;或者如何提出一个命名合理的命名方案? – GhostCat
我的问题是如何创建多个屏幕截图,我已经设法得到屏幕截图,但我不想更新上一屏幕截图 – Prakash
你看,这就是我的意思是**命名**方案。您正在覆盖您的屏幕快照,因为您始终使用相同的**名称**作为文件。因此:你应该想出一种使这些文件名具有唯一性的方法,并且可能“告诉”(以便文件名告诉你它被采用的路径)。 – GhostCat