Capturing Screenshot Using Robot Class In Selenium

Hello Folks,

In last post we have seen Taking Screenshot Using TakesScreenshot Interface In Selenium. I have explained about its drawbacks as well.

Drawbacks of using TakesScreenshot interface for taking screenshot in selenium:

  • It will not take screen shot when an alert is open.  It will give you an exception stating “org.openqa.selenium.UnhandledAlertException”.
  • It takes screenshot of visible area of browser. It will not capture address bar and tab opened.
  • If your requirement is to take screenshot with URL, it will not be possible through above interface.

Capturing screenshot in Selenium using Robot class:

  • Java provides a class named “Robot” which is present in java.awt package.
  • Using Robot class, we can control over the mouse and keyboard devices.
  • Robot class generates native system input events for the purposes of test automation of applications where control of the mouse and keyboard is needed.
  • Robot class will capture screenshot successfully if any alert is open.

Steps to capture screenshot using Robot class:

  • Create an object of Robot class.
  • Get the screen size as a Rectangle.
  • Use createScreenCapture of Robot class and capture screenshot. It will be temp image.
  • Define destination path for screenshot.
  • Write temp file in to permanent file.

Java code:

package TakingScreenshot;

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;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class CaptureScreenshotUsingRobot {
	
	
	public static String CaptureScreenShotWithTestStepNameUsingRobotClass(String testStepsName)
	{
		try{
			
			// Creating Robot class object
			Robot robotClassObject = new Robot();
			
			// Get screen size
			Rectangle screenSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
			
			// Capturing screenshot by providing size
            BufferedImage tmp = robotClassObject.createScreenCapture(screenSize); 
            
            // Defining destination file path
            String path=System.getProperty("user.dir")+"/ScreenCapturesPNG/"+testStepsName+System.currentTimeMillis()+".png";
            
            // To copy temp image in to permanent file
            ImageIO.write(tmp, "png",new File(path)); 
            return path;
            
		}catch(Exception e)
		{
			System.out.println("Some exception occured." + e.getMessage());
			return "";
		}
	}
	

	public static void main(String[] args) throws InterruptedException {
		
		System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver.exe");
		ChromeDriver driver= new ChromeDriver();
	
		driver.get("http://makeseleniumeasy.com/");
		CaptureScreenshotUsingRobot.CaptureScreenShotWithTestStepNameUsingRobotClass("URL Loading");
		
		

	}

}

Output:

You can see it takes screenshot of whole desktop. It gives extra information about screenshot such as URL, Time and date.

If you have any doubt, feel free to ask here.

If you like my posts, please like, comment, share and subscribe.

#ThanksForReading

#HappySelenium

 

9 thoughts on “Capturing Screenshot Using Robot Class In Selenium”

Leave a Reply

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