I am using the below method for taking screenshots in my project: -
public String getScreenShot(String imageName) {
String snapShot = null;
try {
if (imageName.equals("")) {
imageName = "blank";
}
Calendar cal = Calendar.getInstance();
Date currentTimeStamp = cal.getTime();
String timeInFormat = formater.format(currentTimeStamp);
File src = driv.getScreenshotAs(OutputType.FILE);
String screenShotLocation = System.getProperty("user.dir") + "/screenPrints/";
File des = new File(screenShotLocation + imageName + "_" + timeInFormat + ".png");
FileUtils.copyFile(src, des);
snapShot = des.toString();
i++; //i counts the no of times control coming into getScreenShot method
System.out.println(i);
} catch (Exception e) {
System.out.println("The ScreenShot could not be taken\n" + e);
e.printStackTrace();
}
return snapShot;
}
I am calling the above method with the below line of code: -
screen = getScreenShot("");
It is going inside the getScreenShot() method 158 times and showing i value as 158 in console. But it is able to save the 134 screenprints in the destination folder. If we include 1 second gap before calling this method, it is able to save exact no of screenshots in the destination folder.
Thread.sleep(1000);
screen = getScreenShot("");
Seems like execution is so fast that it is skipping the saving part in the destination folder. Thread.sleep() is drastically decreasing the performance. Could someone suggest better solution for this. Thanks in advance.