-2

This question already has an answer here:

How to take ScreenShot & save it with different filenames by using Selenium & Java?

New contributor
user42556 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

marked as duplicate by Alexey R., Bharat Mane, João Farias, Nitin Rastogi, Kate Paulk 2 days ago

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

You can create a function and pass the desired name to it, something like this:

public static void takeScreenshot(String name) {
        File screenshotFile = ((TakesScreenshot) DriverFactory.getDriver()).getScreenshotAs(OutputType.FILE);
    String fileName = name;
    try {
        fileName += "_" + new Timestamp(System.currentTimeMillis()).toString() + ".png";
        FileUtils.copyFile(screenshotFile, new File(fileName));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
New contributor
José Pedro is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

For taking the screenshot use below code, In the code I have used a string called DateName which adds current date and time and this will create a new entry of screenshot which will be different from older one.

you can create a method and pass the driver and screenshot name and call that method any where u want it

String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
String screenshotName = NewScreenGrab

//this is the Interface which has different methods 
TakesScreenshot takesScreenshot = (TakesScreenshot) driver;
File screenshotSourceFile = takesScreenshot.getScreenshotAs(OutputType.FILE);

//Below code decides the destination of ur screenshot and type of file
String destination =  System.getProperty("user.dir") + "mention the destination where you want to store the screenshot"+ screenShotName + dateName +".png";

File screenshotDestinationFile = new File(destination);

//Below code copies the screenshot file to required destination
FileUtils.copyFile(screenshotSourceFile, screenshotDestinationFile.getAbsoluteFile());

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