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