0

enter image description here I'm trying to capture a full page screenshot but the website returns with height: 0; because this is a default value given in client website. 1600 ,0 in body section. In every page we have different elements in div where we can see height in pixel. For example, please find attached screenshot of html tags ..
Can anyone please guide me how to take the full page screenshot. I have checked with Ashot and Ashot is also not able to capture the full page screenshot. As per client requirement I need to capture screenshot without using Ashot jar file .
I am using selenium with javascript for capturing images .. this code is working fine with other website because all websites is returning values in body section .. In attached image if you see this div is fixed in all pages and other then we have total 3 height name in html tag with different div and in every page div name is changed . Can any one suggest solutions ..

 import java.util.concurrent.TimeUnit;    
    import javax.imageio.ImageIO;    
    import org.openqa.selenium.Dimension;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.remote.Augmenter;

    import com.epath.smoketest.pageobjects.Login;import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.IOException;

    import com.epath.smoketest.pageobjects.Navigation;

    public class Screenshoptcapture {

        private static int scrollTimeout = 0;

        public Screenshoptcapture(int timeout) {
            scrollTimeout = timeout;
        }

        private static String getFullHeight(WebDriver driver) {
            JavascriptExecutor js = (JavascriptExecutor) driver;
            return js.executeScript("return document.body.scrollHeight").toString();
        }

        private static int getFullWidth(WebDriver driver) {
            JavascriptExecutor js = (JavascriptExecutor) driver;
            return ((Long) js.executeScript("return window.innerWidth", new Object[0])).intValue();
        }

        private static int getWindowHeight(WebDriver driver) {
            JavascriptExecutor js = (JavascriptExecutor) driver;
            return ((Long) js.executeScript("return window.innerHeight", new Object[0])).intValue();
        }

        private static void waitForScrolling() {
            try {
                Thread.sleep(scrollTimeout);
            } catch (InterruptedException ignored) {
            }
        }

        private static BufferedImage getScreenshotNative(WebDriver wd) {
            ByteArrayInputStream imageArrayStream = null;
            TakesScreenshot takesScreenshot = (TakesScreenshot) new Augmenter().augment(wd);
            try {
                imageArrayStream = new ByteArrayInputStream(takesScreenshot.getScreenshotAs(OutputType.BYTES));
                return ImageIO.read(imageArrayStream);
            } catch (IOException e) {
                throw new RuntimeException("Can not parse screenshot data", e);
            } finally {
                try {
                    if (imageArrayStream != null) {
                        imageArrayStream.close();
                    }
                } catch (IOException ignored) {
                }
            }
        }

        public static BufferedImage getScreenshot(WebDriver wd) {
            JavascriptExecutor js = (JavascriptExecutor) wd;
            int allHeight = Integer.parseInt(getFullHeight(wd));
            int allWidth = getFullWidth(wd);
            int winHeight = getWindowHeight(wd);
            int scrollTimes = allHeight / winHeight;
            int tail = allHeight - winHeight * scrollTimes;
            BufferedImage finalImage = new BufferedImage(allWidth, allHeight, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D graphics = finalImage.createGraphics();
            for (int n = 0; n < scrollTimes; n++) {
                js.executeScript("scrollTo(0, arguments[0])", winHeight * n);
                waitForScrolling();
                BufferedImage part = getScreenshotNative(wd);
                graphics.drawImage(part, 0, n * winHeight, null);
            }
            if (tail > 0) {
                js.executeScript("scrollTo(0, document.body.scrollHeight)");
                waitForScrolling();
                BufferedImage last = getScreenshotNative(wd);
                BufferedImage tailImage = last.getSubimage(0, last.getHeight() - tail, last.getWidth(), tail);
                graphics.drawImage(tailImage, 0, scrollTimes * winHeight, null);
            }
            graphics.dispose();
            return finalImage;
        }

        public static void screenshotUsingjavaScript(WebDriver wd, String filename) {
            try {
                ImageIO.write(getScreenshot(wd), "PNG", new File(filename));
            } catch (IOException e) {
                System.out.println(e);
            }
        }

        public static void main(String argc[]) {
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\Desktop\\Required Software For Automation\\chromedriver.exe");

            WebDriver driver = new ChromeDriver();
            driver.manage().window().fullscreen();
            driver.get(" ");        
            Screenshoptcapture.screenshotUsingjavaScript(driver, "C:\\Users\\admin\\Desktop\\Required Software For Automation\\Userlist.png");
            Utils.pauseTestExecution(8);

            driver.quit();
        }
    }
Tarun Dabbs
  • 67
  • 13
  • There is no absolute method to get height of the page across all browsers. You need to try multiple methods - Refer :https://stackoverflow.com/questions/1145850/how-to-get-height-of-entire-document-with-javascript – Shivam Mishra Jun 28 '18 at 08:49
  • I have executed code with given solutions but same this is not capturing full page .. It is capturing only capturing visible section on window screen .. Scroll bar start in middle of the screen if you see in attached screenshot .. at the time of capturing screenshot this scroll bar is not moving down . – Tarun Dabbs Jun 28 '18 at 09:59

1 Answers1

0

Perhaps your body actually has height 0. Maybe you have direct children with float: or with position: absolute/fixed (so they're taken out from the box)? Maybe body has height: 0; or max-height: 0;.

Danon
  • 2,771
  • 27
  • 37
  • In body we have no children . we have max-height: in different div's . – Tarun Dabbs Jun 28 '18 at 09:49
  • In every page we have 3 height variable in html tag. But when we hover on one div then we can see height I am attaching one more screenshot related new div – Tarun Dabbs Jun 28 '18 at 12:50
  • Well, maybe this div has a positive height, but it doesn't necessarily mean that the body has more than 0px height. Open your page in a developer tool (like Chrome's devtools) and locate the body and check whether it really has a positive height. – Danon Jun 28 '18 at 12:58