0

When I try to take a screenshot, I added a scroll so that it can take a significant screenshot and not only the header. Right now, the picture is still taken from the header/top of the page.

Am I doing something wrong?

Screenshot method :

public void ScreenShot() {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("scroll(0,450);");
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH_mm_ss");
        LocalDateTime now = LocalDateTime.now();
        String destDir = System.getProperty("user.dir") + "/" + "test-output/ScreenShots";
        String FileName = "SoftAssertScreenshot_" + dtf.format(now) + ".png";
        String DestFile = destDir + "/" + FileName;
        File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        File ScreenshotName = new File(DestFile);
        try {
            FileUtils.copyFile(file, ScreenshotName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Reporter.log("</br><font color='#73a9d0'>***************Screen Of the error****************</font>");
        Reporter.log("</br><img id='ErrorResult' src='" + DestFile + "' style='width:600px'/>");
    }

1 Answer 1

0

You can use Ashot it s the same principal as normal ScreenShot just it gives you a picture of the whole page This is the same code but with Ashot:

public void ScreenShot() {
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH_mm_ss");
    LocalDateTime now = LocalDateTime.now();
    String destDir = System.getProperty("user.dir") + "/" + "test-output/ScreenShots";
    String FileName = "SoftAssertScreenshot_" + dtf.format(now) + ".png";
    String DestFile = destDir + "/" + FileName;
    Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
    BufferedImage image = screenshot.getImage(); 
    try {
        ImageIO.write(image, "PNG", new File(DestFile));
        System.out.println("test");
    } catch (IOException e) {
        e.printStackTrace();
    }
    Reporter.log("</br><font color='#73a9d0'>***************Screen Of the error****************</font>");
    Reporter.log("</br><img id='ErrorResult' src='" + DestFile + "' style='width:600px'/>");
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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