2

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.

Rachit Jain
  • 103
  • 2
  • 11

3 Answers3

2

Possibly several files getting the same name. I think, your timeInFormat is formatted up to seconds & you are taking multiple screenshots within second. To get around this problem you can format timeInFormat up to millis or micros. There is also other hack though. Just change the line

String timeInFormat = formater.format(currentTimeStamp);

to

String timeInFormat = formater.format(currentTimeStamp) + String.valueOf(i);

I hope that helps.

optimistic_creeper
  • 2,739
  • 3
  • 23
  • 37
  • Yes, I used SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss"), that was not able to take multiple screenprints within the same second. Now I am using milliseconds also i.e. SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss.SSS") and it is taking all the screenprints. Thank you so much :) – Rachit Jain Apr 20 '18 at 09:04
1

There are a couple of things which you need to consider as follows :

  • If you are saving the screenshots with a name including the text blank you can change the signature of the method as :

    public String getScreenShot() //removing String imageName
    

    You can call this method from main()/@Test as follows :

    screen = getScreenShot();
    
  • When you invoke getScreenshotAs() of-coarse you have to cast the WebDriver instance driv as follows :

    TakesScreenshot ts = (TakesScreenshot) driv;
    File src = ts.getScreenshotAs(OutputType.FILE);
    
  • You need to decide what you want to return either a String as des.toString(); or int as i

  • Your code block for the getScreenShot() method with minor tweeks will be as follows :

    public int getScreenShot() {
    
        String snapShot = null; //possibly not needed, we are returning int i
        int i = 0;
        String imageName = "blank";
        try {
            Calendar cal = Calendar.getInstance();
            Date currentTimeStamp = cal.getTime();
            String timeInFormat = formater.format(currentTimeStamp);
            TakesScreenshot ts = (TakesScreenshot) driv;
            File src = ts.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(); //not needed as we are not using, returning int i
            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 i;
    }
    
  • Now you can call the getScreenShot() from main()/@Test as follows :

    int screen = getScreenShot();
    

Reference

You can find a detailed discussion in How to take screenshot with Selenium WebDriver

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Really appreciate your efforts and time.. but now my function is working as i included milliseconds also SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss.SSS") And as you suggested, I removed the argument and starting the image name as "Image_20_04_2018_02_27_36.523.png" Thanks :) – Rachit Jain Apr 20 '18 at 09:22
0

Try this code I hope you'll solve the issue

public String captureScreen(String fileName) {
if(fileName =="") {
    fileName="Screenshot";  }

File destFile=null;
Calendar calendar =Calendar.getInstance() ;
SimpleDateFormat formater= new SimpleDateFormat("dd_MM_yyy_hh_mm_ss");
File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

try {

     String reportDirectory= new File(System.getProperty("user.dir")).getAbsolutePath()+"./src/main/java/com/test/automation/Demo/screenshot/";
     destFile= new File((String)reportDirectory + fileName +"-" + formater.format(calendar.getTime())+ ".png");
     FileUtils.copyFile(srcFile,destFile );

    }
catch(IOException e) 
  {
    e.printStackTrace();
  }
   return destFile.toString();

}