Reputation: 3
I would like to take a multiple screenshots in selenium using java.
For example, I am trying to navigate all the links in my website. While navigating, if there are errors (e.g. page not found, server error), I want to capture all the errors in the screenshots individually. currently it's overriding the previous one.
if(driver.getTitle().contains("404"))
{
System.out.println("Fail");
File scrFile = (TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File("outputfile"));
}catch(IOException e){
e.printStackTrace();
}
}
else
{
System.out.println("Pass");
}
Upvotes: 0
Views: 3719
Reputation: 11
You can use current Date Time Stamp and append this with the image format
Upvotes: 1
Reputation: 25700
I would do a few things to clean this process up.
Put your screenshot code into a function.
Add a date time stamp to your screenshot filename. That will give each file a unique name.
Add a short error string to the screenshot filename. That will enable you to quickly see how many of each error type are present. Bonus points for creating a folder for each error type and only placing screenshots of that particular error in that folder.
Your script will look like
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");
}
and the function to take a screenshot
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));
}
and the function to make a filename friendly date-time stamp
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());
}
What you will end up with are filenames like the below that are neatly sorted by error type, each with a unique file name.
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
Upvotes: 0
Reputation: 6304
To stop overwriting the output file you would have to give every screenshot a unique name.
Somewhere in the code, create the counter
int counter = 1;
then
FileUtils.copyFile(scrFile, new File("outputfile" + counter));
counter++;
So the counter gives the destination file a different name after every copyFile.
Upvotes: 1