• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / Get full length Screenshot using Robot class in Selenium

Get full length Screenshot using Robot class in Selenium

January 9, 2019 by Mukesh Otwani 24 Comments

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.

Filed Under: Basic Selenium

Reader Interactions

Comments

  1. Sambhav Dave says

    October 1, 2020 at 6:14 PM

    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.

    Reply
    • Mukesh Otwani says

      October 1, 2020 at 6:44 PM

      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://learn-automation.com/how-to-capture-screenshot-in-selenium-webdriver/

      Reply
  2. waqar says

    November 12, 2019 at 2:03 AM

    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.

    Reply
    • Mukesh Otwani says

      November 12, 2019 at 9:50 AM

      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

      Reply
  3. Mizgaan says

    July 8, 2019 at 4:12 PM

    how to print screenshot in an emailable report?

    Reply
    • Mukesh Otwani says

      July 8, 2019 at 4:54 PM

      Hi Mizgaan,

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

      Reply
  4. VENKAT says

    March 9, 2019 at 1:52 PM

    HI MUKESH

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

    Reply
    • Mukesh Otwani says

      March 11, 2019 at 11:54 AM

      Hi Venkat,

      Please check this link http://learn-automation.com/how-to-capture-screenshot-for-failed-test-cases-in-selenium-webdriver/

      Reply
  5. Vasu Repaka says

    January 26, 2019 at 8:10 PM

    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

    Reply
    • Mukesh Otwani says

      January 27, 2019 at 11:55 PM

      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”));

      Reply
      • Vamsi says

        June 2, 2019 at 7:16 PM

        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.

        Reply
        • Mukesh Otwani says

          June 3, 2019 at 11:04 AM

          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

          Reply
          • Vamsi says

            June 8, 2019 at 6:07 PM

            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

    January 27, 2017 at 12:56 PM

    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?

    Reply
    • Mukesh Otwani says

      January 28, 2017 at 6:03 PM

      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.

      Reply
  7. Neeraj Kumar says

    September 19, 2016 at 8:04 PM

    Hi Mukesh,

    I really thank full for you.

    Reply
    • Mukesh Otwani says

      September 19, 2016 at 10:47 PM

      Welcome Neeraj 🙂

      Reply
  8. ashok says

    May 24, 2016 at 8:16 PM

    Hi Mukesh,

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

    Reply
    • Mukesh Otwani says

      May 25, 2016 at 1:45 AM

      Your welcome Ashok and best of luck.

      Reply
  9. ashok says

    May 23, 2016 at 9:04 PM

    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?

    Reply
    • Mukesh Otwani says

      May 24, 2016 at 1:34 AM

      Hi Ashok,

      Follow this tutorial and try to customize it.

      Reply
    • Mukesh Otwani says

      May 24, 2016 at 1:39 AM

      Hi Ashok,

      This will help

      Reply
  10. uppuluri Ramesh says

    May 15, 2016 at 10:15 PM

    Could you please elaborate the second limitation ?

    Reply
  11. Biswa bhusan Mishra says

    May 3, 2016 at 2:39 PM

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

    Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?