Header Ads

How To Take ScreenShot in Selenium in Three Quick Ways

Hope you enjoyed my last post on how to get webElement count. Now with this post, I am going to write a code that will take screenshot with Selenium.It is not something new.It is already there in different websites. I am just compiling these into one post and that is as per normal basic framework.
So to start with I will assume, there is a small framework in place. That is the driver class which will have all the methods. For advance stuff,we might have a separate helper class too.
So lets get started:
The first approach: Using TakesScreenshot
public void getscreenshot() throws Exception 
     {
             File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
             FileUtils.copyFile(scrFile, new File("<<path with name of the file>>"));
     }
The second Approach: We will have a overloaded methods of takingscreenshot where it takes no parameter or one parameter which is the element.Also there are two ways to implement it. One is to give your element name and get the screenshot file as element name or set the file name with time stamp
public void takeSceenshot() throws IOException
 {
  TakesScreenshot oscn=(TakesScreenshot)driver;
  File oscnShot= oscn.getScreenshotAs(OutputType.FILE);
  File destfile=new File("D:\\My_projects\\Projects\\TestBase\\src\\Base\\Test.jpg");
  FileUtils.copyFile(oscnShot, destfile);
 }
 public void takeSceenshot(String elementType) throws IOException
 {
  TakesScreenshot oscn=(TakesScreenshot)driver;
  File oscnShot= oscn.getScreenshotAs(OutputType.FILE);
  File destfile=new File("D:\\My_projects\\Projects\\TestBase\\src\\Base\\Test"+elementType+".jpg");
  File destfile1=new File("D:\\My_projects\\Projects\\TestBase\\src\\Base\\Test"+GetTimeStampValue()+".jpg");
  FileUtils.copyFile(oscnShot, destfile);
  FileUtils.copyFile(oscnShot, destfile1);
 }
 public  String GetTimeStampValue()throws IOException{
     Calendar cal = Calendar.getInstance();       
     Date time=cal.getTime();
     String timestamp=time.toString();
     System.out.println(timestamp);
     String systime=timestamp.replace(":", "-");
     System.out.println(systime);
     return systime;
 }

Third Option could be:This using pure java with robot class.
protected void takeScreenShotMethod(){
     try{
         Thread.sleep(10000);
         long id = Thread.currentThread().getId();
         BufferedImage image = new Robot().createScreenCapture(new Rectangle(
             Toolkit.getDefaultToolkit().getScreenSize()));
         ImageIO.write(image, "jpg", new File("./"
             + id + "screenshot.jpg"));
     }
     catch( Exception e ) {
         e.printStackTrace();
     }
 }

I worked with these three ways to take screenshots.But mostly I like the second one. Now let us understand when we should use it. If the webpage is opening with some popup ,like rediff.com, or if in some cases webpage is minimized,It will take the screenshot of the webelement or the webpage even if new popups are opened.
But suppose you are using one application like desktop automation using selenium with a third party jars or applet automation, in that case we will go with third approach.
Do let me know if you agree or disagree with me.

Powered by Blogger.