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



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



  • 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();
        }
    }
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2