How to capture screenshot in Selenium using Java Robot class?

In the previous tutorial, we have learned the importance of keeping screenshot throughout the testing journey, no matter whether it is manual testing or the automation testing. We discussed core Takes Screenshot interface previously to take the screenshot. Today we are going to discuss some different technique to capture the screenshot in Selenium WebDriver. I believe you will find it interesting.

Before we jump to the tutorial I would like to highly recommend you going through below tutorials. These two tutorials will add benefits to this learning.

Above articles would be proved advantageous in your learning journey, hence, it will add extra miles to your knowledge.

Just for the knowledge recap, robot class is like any other java class written inside AWT. It basically handles all the activities which require human interventions like switching between tabs, keyboard events, mouse events, etc. On the other hand, taking screenshots of the failed test cases acts as the proof as well as marks the basis of further investigation for the root cause of the failure which simply acts as the support for the failure analysis.

For further assistance, you may refer Robot class doc from Oracle. Click here to read the doc.

capture screenshot in Selenium

There is no connection of this image with the content 🙂

Why should I use Robot class to takes screenshot instead of using Selenium method?

This technique is totally alternative to the previously discussed technique by using TakesScreenshot interface. When the previously discussed method fails then you can use this technique as another statement conditions. It is always good to have plan B.

Import Library-

You need to import a few classes from the AWT library before you proceed further. Here are those classes:

  • Rectangle Class (import java.awt.Rectangle)
  • Robot Class (import java.awt.Robot)
  • Toolkit Class (import java.awt.Toolkit)
  • BufferedImage Class (import java.awt.image.BufferedImage)

Once done then we can proceed with instance creation and calling of above classes.

Command to capture the screenshot using Robot class

You can use below code as the command to capture the screenshot using robot class. You can place this code wherever required to capture the screenshot.

Code’s Explanation

We are already familiar with the Robot class, so next jump to the explanation of the other instantiation. Here Rectangle class defines an area whose coordinates start with (0,0). As per the code, it defines the size in which screenshot will be captured. File creates the path where the captured screenshot will be saved. BufferedImage class is the sub-class of the Image class, it basically manipulates the images. ImageIO.write command write the image to the given destination with the designated file format.

Hope you won’t have any confusion in understanding the code. Let’s us look at a program which captures screenshot after the webpage loads.

One Response

Leave a Reply