0

I need to automate a shooter which takes a screenshot of the whole page of the website. I tried a solution, but only a part of the page is taken. Somebody can help? Below you see the code that I tried.

public void captureScreenshot(WebDriver driver, String screenshotName) {
    // Take the screenshot only is the feature is activated
    if (isActivate) {

        try {
            // before to take the screenshot
            utils.sleep(1);

            TakesScreenshot ts = (TakesScreenshot) driver; 
            File source = ts.getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(source, new File(dirPath + fileSep + screenshotName
                + "_" + strDateStamp + ".png"));
            String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";
            System.setProperty(ESCAPE_PROPERTY, "false");
            URL path = new File(dirPath + fileSep + screenshotName + "_"
                + strDateStamp + ".png").toURI().toURL();
            String test = "<a href=" + path + "> click to open the screenshot "
                + screenshotName + "</a>";
            Reporter.log(screenshotName + test + "<br>");
        } catch (Exception e) {
            System.out.println("Exception while taking screenshot " + e.getMessage());
        }
    }
}
Roberto Pegoraro
  • 1,313
  • 2
  • 16
  • 31
  • IEDriver will take full screenshot. (Not just visible portion of page.) However some sites will load more content upon scroll. – pcalkins Jun 18 '19 at 18:29

2 Answers2

0

Hi you might want to try below code. It works for me for taking whole page screenshot in windows for both chrome as well as firefox.

public void getScreenshotOfWholePageWithScreenshotName(WebDriver driver, String path, String name) {
    try {
        Thread.sleep(2000);
        Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
        ImageIO.write(fpScreenshot.getImage(), "PNG", new File(path + "\\" + date() + " - " + name + ".png"));
        Thread.sleep(2000);
   } catch (Exception ex) {
        ex.printStackTrace();
   }
}

In order to work above code, you will have to use following library:

<dependency>
    <groupId>ru.yandex.qatools.ashot</groupId>
    <artifactId>ashot</artifactId>
    <version>1.5.2</version>
</dependency>

I am using maven hence, i am adding the dependency in POM.xml file.

Cheers!

Roberto Pegoraro
  • 1,313
  • 2
  • 16
  • 31
Suraj Jogdand
  • 308
  • 2
  • 17
0

Can be done using Shutterbug:

https://www.assertthat.com/posts/selenium_shutterbug_make_custom_screenshots_with_selenium_webdriver

To make a full page screenshot:

Shutterbug.shootPage(driver, ScrollStrategy.WHOLE_PAGE).save();

Sources on github https://github.com/assertthat/selenium-shutterbug

Provides ability to make full page screenshot in Chrome and some other extended features.

Glib Briia
  • 86
  • 4