Basic Selenium

Get full length Screenshot using Robot class in Selenium

Selenium Webdriver tutorial

Hi All welcome to Selenium Tutorials, in this tutorial we will discuss how to Get complete window Screenshot using Robot class in Selenium.

You must be having the question in your mind that why we need to take Screenshot using Robot class. I know everyone is aware of taking the screenshot using Selenium API but want to tell you that we have some limitation of this.

If you have not gone through screenshot part then below post will help you.

How to capture screenshot in Selenium

How to capture screenshot only on failures.

 

Limitation of screenshots by Selenium.

1- When any alert comes on screen and if you call screenshot method then it will fail because the alert is windows activity.

AlertIssue

If you are not aware of Alert in Selenium and how to handle then check out below article to get more info.

How to handle Alert in Selenium webdriver

 

2- When running cross-browser testing if need to verify that test is running on which browser then you won’t be able to verify because it captures only web view part.

Check out below post if you have not done any cross-browser testing yet.

How to perform cross browser testing in Selenium

 

Point to Screenshot using Robot class

1- We need to take help of Rectangle class and some other packages of AWT package. You can get full Robot Doc using this link.

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

 

2- I will suggest using this method only when you are unable to take screenshots using normal screenshot approach.

Program Screenshot using Robot class

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class CaptureScreenshot {

	public static void main(String[] args) throws Exception
	{

// This code will capture screenshot of current screen		
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
	    
// This will store screenshot on Specific location
ImageIO.write(image, "png", new File("C:\\Screenshot\\CurrentScreenshot.png")); 

	}

}

 

After execution of above code, you will get screenshot like below

Screenshot using Robot class

Screenshot using Robot class

 

You can also create the Utility class for this and start calling based on need. Hope you have enjoyed this article.

Please let me know your thoughts on this.

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

24 thoughts on “Get full length Screenshot using Robot class in Selenium

  1. Sambhav Dave says:

    Hello,

    I am facing a problem with Robot class wherein, if I minimise browser (at the time of the execution), then it takes the screenshot of the active window (and not the browser) . How to tackle this , please suggest.

    Thanks in advance.

    1. Hi Sambhav,

      That is how Robot class screenshot mechanism works. Robot always take visible screenshot. If you are looking for screenshot of browser then check this link https://vistasadprojects.com/mukeshotwani-blogs-v2/how-to-capture-screenshot-in-selenium-webdriver/

  2. waqar says:

    There is a scenario that I am trying to automate through selenium webdriver (Java) and facing trouble. Print preview is not accessible through selenium as it is within shadow-root. Our task is to download the pdf files from web portals. When we clicked on the print button on web, the print preview window is opened and we need to click on the Save button in this print preview window in order to download the pdf.

    1. Hi Waqar,

      Selenium only works web view of browser and print option doesn’t come on the same. I would recommend you to use third party Java api to achieve your task. Please refer this link https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013119-printing-web-page-to-pdf

  3. Mizgaan says:

    how to print screenshot in an emailable report?

    1. Hi Mizgaan,

      In order to customize testng emailable-report, you have to use IReporter interface(which is one of TestNG listener)

  4. VENKAT says:

    HI MUKESH

    IF ANY TEST SCRIPT FAIL. HOW TO TAKE SCREEN SHOT OF FAILED FUNCTIONALITY IN ORDER TO ATTACH TO THE REPORTS.

  5. Vasu Repaka says:

    Can this approach takes screeshot of a page which is longer than the desktop size. I mean when we need to scroll down the page
    Can you suggest any approach to do the same if this approach don’t capture

    1. Hi Vasu,

      This approach will take screenshot of web view only. In order to get screenshot of web page from top to bottom, you need to use aShot library. Here are the details of same
      Maven Dependency link https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot/1.5.4

      Code
      Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver);
      ImageIO.write(fpScreenshot.getImage(), “PNG”, new File(“C:\\aaa\\FullView.png”));

      1. Vamsi says:

        Hi Mukesh,

        In some websites, the header remains at top of the page even after scroll(Ex: http://automationtesting.in/taking-full-page-screenshot-in-selenium/), any idea how to handle it

        Thank You in advance.

        1. Hi Vamsi,

          What problem are you facing? In my opinion it is easy because you don’t need to call scroll mechanism in order click on any menu

          1. Vamsi says:

            Hi Mukesh,

            Thank You so much for your reply,

            If we use the below code to take the complete page screenshot for this type of websites – Ex: http://automationtesting.in/taking-full-page-screenshot-in-selenium/ then the header is getting repeated multiple times, so to handle this

            Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver);
            ImageIO.write(fpScreenshot.getImage(), “PNG”, new File(“C:\\aaa\\FullView.png”));

  6. Rohit says:

    How to take multiple screenshots?
    It is the same way that you have mentioned in the previous post of “how to take multiple screenshots”
    Can you just help me for this one?

    1. Hi Rohit,

      If you want to take multiple screenshots then you need to call screnshot method that much number of times. Or tell if you have specific requirements.

  7. Neeraj Kumar says:

    Hi Mukesh,

    I really thank full for you.

  8. ashok says:

    Hi Mukesh,

    Thank you so much for sharing the video. It would help me in taking next step regarding my requirement.

    1. Your welcome Ashok and best of luck.

  9. ashok says:

    Thank you so much for the valuable information.

    Is it possible to save the screen shots in word document with webdriver?
    i want to take screen shot and copy the current URL for all the pages and save them in word document for all pages in sequential order. Is there any way to do that?

    1. Hi Ashok,

      Follow this tutorial and try to customize it.

    2. Hi Ashok,

      This will help

  10. uppuluri Ramesh says:

    Could you please elaborate the second limitation ?

  11. Biswa bhusan Mishra says:

    Its a new concept Mukesh.
    Thanks for the much needed Info.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.