2

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.

WebDriver webDriver=getCurrentWebDriver(); WebDriverWaitUtility.waitForPageLoad(WebDriverGUIUtilityContants.WaitTime, webDriver); WebDriverGUIUtility.captureScreenShot(webDriver); 
2
  • Which driver (browser) you are using? Commented Oct 4, 2016 at 5:35
  • i am using mozilla Commented Oct 6, 2016 at 7:50

4 Answers 4

3

If you use maven then you may use AShot to accomplish your task. For this you need to add dependency in your pom file:

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

And use the code snippet as follows:

Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(augmentedDriver); ImageIO.write(screenshot.getImage(), "PNG", new File("d:\\tmp\\results.png")); 

But, if you aren't using maven then download ashot jar (version: 1.5.2) file and add it to your build path. Here's the link your help: https://javalibs.com/artifact/ru.yandex.qatools.ashot/ashot

Hope, that might help you.

Sign up to request clarification or add additional context in comments.

2 Comments

hi boss what is the meaning ViewportPastingStrategy(1000)
naveen viewpastingstrategy means scrollup page by 1000 ms untill end of footer
2

@naveen, generally it happens with Chrome Browser. ChromeDriver is able to take screen shot of visible part. So the concept here is scroll through the page using Java script executor and take multiple images, then combining them to a single image. FirefoxDriver is able to take image of whole screen without issue. Here is an example

@Test(enabled=true) public void screenShotExample() throws IOException{ //WebDriver driver = new FirefoxDriver(); System.setProperty("webdriver.chrome.driver", "yourpath to chromeDriver\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.w3schools.com/"); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); driver.manage().window().maximize(); JavascriptExecutor jexec = (JavascriptExecutor)driver; jexec.executeScript("window.scrollTo(0,0)"); // will scroll to (0,0) position 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("F://MyFile"+ 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("F://MyFile"+ fileIndex+".jpg")); } }else{ File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg")); } // Combine all the .jpg file to single file driver.close(); driver.quit(); } 

To combine all image file you will find some help here. Hope this will help you.

Comments

0

In Selenium 4, FirefoxDriver provides a getFullPageScreenshotAs method that handles vertical and horizontal scrolling, as well as fixed elements (e.g. navbars). Other libraries are not necessary.

System.setProperty("webdriver.gecko.driver", "path/to/geckodriver"); final FirefoxOptions options = new FirefoxOptions(); // set options... final FirefoxDriver driver = new FirefoxDriver(options); driver.get("https://stackoverflow.com/"); File fullScreenshotFile = driver.getFullPageScreenshotAs(OutputType.FILE); // File will be deleted once the JVM exits, so you should copy it 

Comments

-2

Below is selenium with testng code to take google page screenshot

public class ScreenShot { public WebDriver d; Logger log; @Test public void m1() throws Exception { try { d=new FirefoxDriver(); d.manage().window().maximize(); d.get("https://www.google.co.in/?gfe_rd=cr&ei=4caQV6fxNafnugTjpIGADg"); String pagetitle=d.getTitle(); log = Logger.getLogger(FirefoxDriver.class.getName()); log.info("logger is launched.."); log.info("Title name : "+pagetitle); d.findElement(By.id("testing")).sendKeys("test"); d.findElement(By.cssSelector("input.gsfi")).sendKeys("gmail account"); } catch(Exception e) { System.out.println("something happened, look into screenshot.."); screenShot(); } } public void screenShot() throws Exception { Files.deleteIfExists(Paths.get("G:\\"+"Test results.png")); System.out.println("previous pics deleted..."); File scrFile = ((TakesScreenshot)d).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile,new File("G:\\"+"Test results.png")); } 

}

1 Comment

This suggestion includes a lot of code unrelated to the original question of taking a screenshot and also does not address the original question of taking a screenshot of the entire page (not just the visible portion)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.