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.
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
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.
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.
Mukesh Otwani says
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/
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.
Mukesh Otwani says
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
Mizgaan says
how to print screenshot in an emailable report?
Mukesh Otwani says
Hi Mizgaan,
In order to customize testng emailable-report, you have to use IReporter interface(which is one of TestNG listener)
VENKAT says
HI MUKESH
IF ANY TEST SCRIPT FAIL. HOW TO TAKE SCREEN SHOT OF FAILED FUNCTIONALITY IN ORDER TO ATTACH TO THE REPORTS.
Mukesh Otwani says
Hi Venkat,
Please check this link http://learn-automation.com/how-to-capture-screenshot-for-failed-test-cases-in-selenium-webdriver/
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
Mukesh Otwani says
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”));
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.
Mukesh Otwani says
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
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”));
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?
Mukesh Otwani says
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.
Neeraj Kumar says
Hi Mukesh,
I really thank full for you.
Mukesh Otwani says
Welcome Neeraj 🙂
ashok says
Hi Mukesh,
Thank you so much for sharing the video. It would help me in taking next step regarding my requirement.
Mukesh Otwani says
Your welcome Ashok and best of luck.
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?
Mukesh Otwani says
Hi Ashok,
Follow this tutorial and try to customize it.
Mukesh Otwani says
Hi Ashok,
This will help
uppuluri Ramesh says
Could you please elaborate the second limitation ?
Biswa bhusan Mishra says
Its a new concept Mukesh.
Thanks for the much needed Info.