I have a screen shot issue. When I am capturing a screen, it takes only the Visible screen. I want to capture the entire page. Here below is my code.
I found several solutions but that does not suit me. I am looking for a solution where the final screenshot is a single merged image.
I tried ashot library:
<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>1.5.2</version>
</dependency>
And use the code snippet as follows:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
WebDriver driver = new ChromeDriver(capabilities);
driver.get("https://stackoverflow.com/questions/10245641/refreshing-web-page-by-webdriver-when-waiting-for-specific-condition");
String screenName = "poc";
String USER_DIR = "user.dir";
String DOWNLOADED_FILES_FOLDER = "downloadFiles";
Screenshot screenshot2 = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(screenshot2.getImage(), "PNG", new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER + File.separator + screenName + ".jpg"));
I tried scroll by javascript + tack any screenshot:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
WebDriver driver = new ChromeDriver(capabilities);
driver.get("http://www.w3schools.com/");
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
JavascriptExecutor jexec = (JavascriptExecutor) driver;
boolean isScrollBarPresent = (boolean) jexec.executeScript("return document.documentElement.scrollHeight>document.documentElement.clientHeight");
long scrollHeight = (long) jexec.executeScript("return document.documentElement.scrollHeight");
long clientHeight = (long) jexec.executeScript("return document.documentElement.clientHeight");
int fileIndex = 1;
if (driver instanceof ChromeDriver) {
if (isScrollBarPresent) {
while (scrollHeight > 0) {
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
org.apache.commons.io.FileUtils.copyFile(srcFile, new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER + File.separator + fileIndex + ".jpg"));
jexec.executeScript("window.scrollTo(0," + clientHeight * fileIndex++ + ")");
scrollHeight = scrollHeight - clientHeight;
}
} else {
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
org.apache.commons.io.FileUtils.copyFile(srcFile, new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER + File.separator + fileIndex + ".jpg"));
}
} else {
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
org.apache.commons.io.FileUtils.copyFile(srcFile, new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER + File.separator + fileIndex + ".jpg"));
}
// Combine all the .jpg file to single file
driver.close();
driver.quit();
This solution return a lot of picture and Combine all the image file to single file is not easy.
I tried MultiScreenShot library:
https://github.com/Pavan-and-Vikas-Utilities/MultiScreenShot
This solution return a lot of picture and Combine all the image file to single file is not easy.
Sample (produced by FastStone Capture) of result with this url (I looking for this result): Refreshing web page by WebDriver when waiting for specific condition