Monday, November 14, 2016

Take Screenshot of any Website using Java

In this blog post, we will take screenshot of any given set of website using Java Selenium.

Language Used:
Java

Git Repository:
https://github.com/csanuragjain/recorder/tree/master/WebsiteScreenshot

Pre-requisite:

chromedriver.exe
1) Create a folder named driver.
2) Add the chromedriver.exe from https://sites.google.com/a/chromium.org/chromedriver/downloads and place the chromedriver.exe under driver folder

Pom dependency:
1) Add below 2 pom dependency:
   <dependency>  
    <groupId>org.apache.commons</groupId>  
    <artifactId>commons-io</artifactId>  
    <version>1.3.2</version>  
    <scope>compile</scope>  
   </dependency>  
   <dependency>  
    <groupId>org.seleniumhq.selenium</groupId>  
    <artifactId>selenium-java</artifactId>  
    <version>2.42.2</version>  
    <scope>compile</scope>  
   </dependency>  


Program:

initDriver method:
      public static void initDriver()  
      {  
           System.setProperty("webdriver.chrome.driver",  
                     "./driver/chromedriver.exe");  
           driver=new ChromeDriver();  
           driver.manage().window().setPosition(new Point(-2000, 0));  
      }  

How it works:
1) First we tell the location of the chromedriver.exe to our java program. Since we placed it under driver folder in current project directory so we set the same.
2) Now we initialize our webdriver using a chromedriver.
3) We set the position to (-2000,0) so that the browser gets minimized.

capture method:
      public void capture(String site) throws IOException  
      {  
           screenshotNum++;  
           driver.get(site);  
           File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
           FileUtils.copyFile(scrFile, new File("site"+screenshotNum+".png"));  
           System.out.println("Took Screenshot for "+site+" and saved as "+"site"+screenshotNum+".png");  
      }  

How it works:
1) screenshotNum shows the number of screenshots taken. This is used to name the screenshot saved.
2) we navigate to the site for which we need the screenshot.
3) Now we take the screenshot by calling getScreenshotAs function.
4) Now we copy the File pointing to screenshot into our local computer system by calling copyFile method. (copyFile is obtained using the maven dependency org.apache.commons)

destroy method:
      public static void destroy()  
      {  
           driver.quit();  
      }  

How it works:
1) we call the quit method to dispose the driver

main method:
      public static void main(String[] args) throws IOException {  
           initDriver();  
           WebPageScreenShotTaker ws=new WebPageScreenShotTaker();  
           ws.capture("https://www.google.com");  
           ws.capture("https://facebook.com");  
           destroy();  
      }  

How it works:
1) We call initDriver to intialize the webdriver
2) We call the capture method which takes screenshot of the passed website
3) We call the destroy method to close our webdriver


Sample Saved Screenshot:


Full Program:
 package com.cooltrickshome;  
 import java.io.File;  
 import java.io.IOException;  
 import org.apache.commons.io.FileUtils;  
 import org.openqa.selenium.OutputType;  
 import org.openqa.selenium.Point;  
 import org.openqa.selenium.TakesScreenshot;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 public class WebPageScreenShotTaker {  
      static int screenshotNum=0;  
      static WebDriver driver=null;  
      public static void main(String[] args) throws IOException {  
           initDriver();  
           WebPageScreenShotTaker ws=new WebPageScreenShotTaker();  
           ws.capture("https://www.google.com");  
           ws.capture("https://facebook.com");  
           destroy();  
      }  
      public static void initDriver()  
      {  
           System.setProperty("webdriver.chrome.driver",  
                     "./driver/chromedriver.exe");  
           driver=new ChromeDriver();  
           driver.manage().window().setPosition(new Point(-2000, 0));  
      }  
      public void capture(String site) throws IOException  
      {  
           screenshotNum++;  
           driver.get(site);  
           File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
           FileUtils.copyFile(scrFile, new File("site"+screenshotNum+".png"));  
           System.out.println("Took Screenshot for "+site+" and saved as "+"site"+screenshotNum+".png");  
      }  
      public static void destroy()  
      {  
           driver.quit();  
      }  
 }  

Hope it helps :)

No comments:

Post a Comment