4

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?

closed as unclear what you're asking by ECiurleo, Kate Paulk, IAmMilinPatel, Michael Durrant, bish Mar 29 '16 at 16:57

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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. – Bharat Mane Mar 24 '16 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 '16 at 11:38
5

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);
    }
    
  • 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- – Bharat Mane Mar 24 '16 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. – Niels van Reijmersdal Mar 24 '16 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
    I would avoid using Thread.sleep and go by Niels' answer instead. – FDM Mar 24 '16 at 9:09

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