5

I have created screenShot() method to take the screenshot of the web page. When I used this method in my code first time it will capture the screen and it will save the image with image1 name. When I used the same method screenShot() to capture screens for other pages with the use of this.screenShot(); this will again capture the current web page screen but it stores With the same name image1 in the same folder (which overrides to previously captured screen image1).

Now, I have to take screenshots for the number of web pages & want to store it into the same folder with different names like image1, image2, image3...etc. by using the same method screenShot().

public void screenShot() throws IOException, InterruptedException
{
    File scr = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    File dest = new File("filPath/image1.png");
    FileUtils.copyFile(scr, dest);
}

Any workaround to achieve this scenario?

2
  • But by using this script, I am taking only one Screenshot everytime, which is overrides it every time. What should I do If I want to take multiple screenshots by using time & date variable. Mar 24, 2016 at 8:22
  • your question seems more of an answer.. Can you rephrase it with whar you're requirements are? Do the screen shots need to be triggered by events or end of a particular class for example?
    – ECiurleo
    Mar 24, 2016 at 11:38

2 Answers 2

6

Two options:

  1. Generate a unique filename for each save, by appending a timestamp or a GUID.
    Also, see this question.

    public void screenShot() throws IOException, InterruptedException {
        File scr = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        String filename =  new SimpleDateFormat("yyyyMMddhhmmss'.txt'").format(new Date());
        File dest = new File("filePath/" + filename);
        FileUtils.copyFile(scr, dest);
    }
    
  2. Give your screenShot() function a filename parameter and pass a unique filename in each call, something like this:

    public void screenShot(string filename) throws IOException, InterruptedException {
        File scr = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        File dest = new File("filePath/" + filename);
        FileUtils.copyFile(scr, dest);
    }
    
2
  • Hey thanks Niels, but, When I used this code, Everytime it shows only one screenshot in destination file path, even though I was used the same method (screenshot) at 3 times- Mar 24, 2016 at 8:49
  • 3
    I didnt add seconds to the SimpleDateFormat, maybe you took the screenshot within the same minute. Added ss to the format to give you also seconds, you can also add milliseconds if needed. Do you have any developers close by? Ask them for help! :) Because this are relative basic programming problems. Mar 24, 2016 at 8:55
2

Yes, I have tried some different logic and it is working now.

public void screenShot() throws IOException, InterruptedException
{
    File scr=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

    File dest= new File("filPath/screenshot_"+timestamp()+".png");
    FileUtils.copyFile(scr, dest);
}

public string timestamp() {
    return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
}
1
  • 1
    I would avoid using Thread.sleep and go by Niels' answer instead.
    – FDM
    Mar 24, 2016 at 9:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.