Capture multiple screenshots (not overriding the previous one) using aShot class in selenium webdriver

187 Views Asked by At

How to Capture multiple screenshots (not overriding the previous one)through selenium webdriver using aShot class. I am able to run 2 test cases and can see screenshot is being captured for both of the test cases however screenshot only got copied in the folder for the current case.Below code i am using to take full screenshot using aShot class.

public static void captureFullPage() throws IOException

{

screenshotName = d.toString().replace(":", "_").replace(" ", "_")+ "PNG";
Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
String dest = System.getProperty("user.dir") + "\\TestOutput\\" + screenshotName;

ImageIO.write(screenshot.getImage(), "PNG", new File(dest));

//return dest;

}

2

There are 2 best solutions below

0
On

Write code as

public void screenshotByAshot() throws IOException {
    WebDriverManager.chromedriver().setup();
    ChromeDriver driver = new ChromeDriver();
    driver.get("URL");

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("ddMMyyyy_HHmmss");
    LocalDateTime now = LocalDateTime.now();
    String date = dtf.format(now);

    AShot ashot = new AShot();
    Screenshot ss = ashot.shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
    BufferedImage img = ss.getImage();
    String dest = System.getProperty("user.dir");
    File path = new File(dest + "//folder_name/file_name" + date + ".png");
    ImageIO.write(img, "png", path);
}

This code will help you take screenshot with different names each time as it includes timestamp in its name so that overriding of previous screenshots will not happen and you will get all of your screenshots. make changes in this code as per your taste.

0
On

I think you are overriding your first taken screenshot by the second one, so you need to define a different path/folder where you want to save your second screenshot, to keep both screenshots under the different names