Robot Class in Selenium WebDriver Using Java

Robot Class in Selenium: Selenium is one of the top-ranked automation testing tools for web applications, and there are some tools and add-ons that enhance the performance of the Selenium webdriver. For example, during automation, we must use the keyboard or mouse to interact with popups, windows, and alerts. We can also handle this with action class, but we can even do such things with the help of robot API with selenium.

In this post, we are going to discuss:

  • What is a Robot class?
  • Importance of Robot class
  • Methods to implement this class
  • How do you implement the Robot API class in Selenium?
  • Limitations

So, let’s understand the above topics in a simple manner and also with some real-time examples.

During testing any web application, when we are running our automation script, it interacts with pop-ups, Alerts, Print Pop-ups, etc., and other Windows system applications like Notepad, Skype, Calculator, etc. So, to interact with such elements, we need to control the keyboard and mouse so we can do all such operations with the help of Robot Class. Robot class is introduced in the Java 1.3 version. With the help of this class, we can control the mouse and keyboard devices. its implementation is easy, and we can easily integrate it with any framework.

Importance of Robot class

  • We Can handle both keyboard and mouse events using Selenium webdriver.
  • We can handle operations like file upload and download using Selenium webdriver.
  • Robot class methods can effectively interact with popups.
  • We can easily integrate with any automation framework.

Methods to implement this class

Let us go through some of the regular methods of the robot class, which we are frequently using in our automation script:

  • KeyPress(): This method we use when we want to press any key, for example, robot.keyPress(keyEvent.VK_UP). This will press the up key of the keyboard.
  • KeyRelease(): This method is used to release a press key on the keyboard, ex: robot.keyRelease(keyEvent.VK_CAPS_LOCK);
  • MouseMove(): This method comes to use when you want to move the mouse from one coordinate to another x & y coordinates. Ex: robot.mouseMove(coordinates.get.X(), coordinates.get.Y());
  • MousePress(): With this method, we can press the mouse’s left button. Ex: robot.mousePress(InputEvent.BUTTON1_MASK);
  • MouseRelease(): This button helps us release the pressed mouse button.

We can also perform other operations using robot API, like scrolling the mouse and capturing the screenshot using the robot class.

Disadvantages of Robot Class

The mouse/keyboard will work only on the current instance. This means that you are running your automation script in that robot class event, and because of that, there is some new window or screen; in that case, the keyboard or mouse event will not work. The event will remain in the previous window.

Switching to different frames and windows using the robot class isn’t easy. Some events also depend upon the screen resolution, like mouseMove() will not work as expected on different screen resolutions.

Screenshot Using Robot Class: How do you take a screenshot using a robot class using the Java Selenium Program?

In our previous post, we discussed various techniques to take page screenshots and specific element screenshots. In this post, we will discuss how you can take the screenshot by using the robot class.

In our previous techniques, we have used the TakesScreenshot interface to take screenshots. But in that there are a few drawbacks, like:

1. If an alert is present, it will not take the screenshot. That time it will give an exception stating “org.openqa.selenium.UnhandledAlertException”.
2. With TakesScreenshot, you can screenshot the browser’s visible area without the address bar and other opened tabs.
3. If you want to know which URL you got that failed, you can’t find the URL here.

Capture Screenshot Using Robot Class:

1. Java Programming language provides a class name “Robot”, which is present in java.awt package.
2. Using the Robot class, we can do other operations like different Keyboard and Mouse actions. For the details post, you can check our detailed articles about robot class.
3. If any alert also comes and you want to take a screenshot, you can use the Robot class.

Steps to capture screenshots using Robot class:

1. Create an object of the Robot class.
2. Get the screen size as a rectangle.
3. Use the createScreenCapture method of the Robot class, which will capture the screenshot and store it in the temp folder.
4. Save the captured image into a permanent folder using the write method.

public class screenshot_robot
{
	public static void main(String[] args) throws InterruptedException 
	{
		public static void getScreenShot(String path)
		{
			Robot r=new Robot();
			Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
			Rectangle screenRect=new Rectangle(screensize);
			BufferedImage image=r.createScreenCapture(screenRect);
			ImageIO.write(image, "png",new File(path));
		}
	}
}

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment