How To Take Entire Page Screenshots In Selenium


capture full page screenshots using selenium

Normally, selenium can take screenshots using ‘TakeScreenshot’ interface. But many times selenium automation testers require full webpage screenshots for the purpose of reporting and validation. If we take screenshots using ‘TakeScreenshot’ interface we can only capture a view point of a webpage.

We can capture the default screenshot using the code written below.


 import java.io.File;  
 import java.util.concurrent.TimeUnit;  
 import org.apache.commons.io.FileUtils;  
 import org.openqa.selenium.OutputType;  
 import org.openqa.selenium.TakesScreenshot;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 public class FullPage_Screenshot {  
   public static void main(String[] args) throws Exception {  
    System.setProperty("webdriver.chrome.driver", "D:/workspace/TestProject/src/chromedriver.exe");   
  WebDriver driver = new ChromeDriver() ;  
     driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);  
     driver.manage().window().maximize();    
     try{          
       File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);   
       FileUtils.copyFile(scrFile, new File("D:\\screenShot1.png"));  
       System.out.println("Screenshot is captured and stored in your D: Drive");  
     }    
     catch (Exception e)  
     {   
       System.out.println("Error in loading the Google page");  
     }   
   }   
 }  
To take the screenshot of an entire page in selenium we need to take the help from javascript. Because ‘selenium java’ can execute javascript and javascript can interact with web browser very well, so taking help from javascript can be a very useful process.

 package capturescreenshot;  
 import java.awt.Graphics2D;  
 import java.awt.image.BufferedImage;  
 import java.io.ByteArrayInputStream;  
 import java.io.File;  
 import java.io.IOException;  
 import javax.imageio.ImageIO;  
 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;  
 /**  
 * Full Page Screenshot utility for Selenium Webdriver  
 *  
 * @author  
  */  
 public class ScreenShot {  
   private static int scrollTimeout = 0;  
   public ScreenShot(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 allH = Integer.parseInt(getFullHeight(wd));  
     int allW = getFullWidth(wd);  
     int winH = getWindowHeight(wd);  
     int scrollTimes = allH / winH;  
     int tail = allH - winH * scrollTimes;  
     BufferedImage finalImage = new BufferedImage(allW, allH, BufferedImage.TYPE_4BYTE_ABGR);  
     Graphics2D graphics = finalImage.createGraphics();  
     for (int n = 0; n < scrollTimes; n++) {  
       js.executeScript("scrollTo(0, arguments[0])", winH * n);  
       waitForScrolling();  
       BufferedImage part = getScreenshotNative(wd);  
       graphics.drawImage(part, 0, n * winH, 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 * winH, null);  
     }  
     graphics.dispose();  
     return finalImage;  
   }  
   public static void EShot(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", "D:/workspace/TestProject/src/chromedriver.exe");  
     WebDriver driver = new ChromeDriver();  
     driver.get("https://en.wikipedia.org/wiki/India");  
      ScreenShot.EShot(driver, "d:/test" + ".png");  
      driver.quit();  
   }  
 }  

Share this

Related Posts

Previous
Next Post »

3 comments

comments
August 21, 2017 at 11:31 PM delete

nice blog has been shared by you. before i read this blog i didn't have any knowledge about this. but now i got some knowledge about this. so keep on sharing such kind of an interesting blogs.
Devops Training in Bangalore

Reply
avatar
November 23, 2017 at 2:04 AM delete

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

Reply
avatar
April 23, 2018 at 1:09 AM delete

I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.

software testing course in chennai

Reply
avatar