Robot Class in Selenium: Selenium is one of the most top-ranked automation testing tools for web applications, and also, there are some tools and addons are present are also enhancing the performance orf selenium webdriver. For example, during automation, we need to use the keyboard or mouse to interact with popups, windows, and alerts. We Can handle this also with action class, but we can even 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 to implement the Robot API class in Selenium?
- Limitations
So lets to understand of 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 is interacting with pop-up, Alerts, Print Pop-ups, etc. and other windows system application 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 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 also easily integrate with any framework.
Importance of Robot class
- We Can handle both keyboard and mouse events using selenium webdriver.
- We Can handle the operation 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 released an 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 the use of this method, we can press the left button of the mouse. Ex: robot.mousePress(InputEvent.BUTTON1_MASK);
- MouseRelease(): This button helps us in a release the pressed mouse button.
We can also perform some other operation using robot API like scrolling mouse and capture the screenshot using the robot class.
Disadvantages of Robot Class
The mouse/keyboard will work only on the current instance that means suppose you are running your automation script and in that robot class event, and because of that, some new window or screen then, in that case, the keyboard or mouse event will not work. The event will remain in the previous window.
It is difficult to switch to different frames and windows using robot class Some events also depend upon the screen resolution like mouseMove() will not work as expected on different screen resolutions.
Screenshot Using Robot Class: How to Take Screenshot Using Robot Class using Java Selenium Program?
In our previous post we have discussed about various techniques to take the full page screenshot as well specific element screenshot. In this post we are going to discuss about how you can take the screen shot by using the robot class.
In our previous techniques we have used TakesScreenshot interface to take the screenshot. But in that there are few drawbacks are there like:
1. If an alert is present then it will not take the screenshot. That time it will give an exception stated “org.openqa.selenium.UnhandledAlertException”.
2. With TakesScreenshot you can take the screenshot of the visible area of the browser that is with out address bar and other opened tabs.
3. If you want to know on which URL you get that failure then 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. By using Robot class we can do some other operations also like different Keyboard and Mouse action. For the details post you can check our details articles about robot class.
3. If any alert also comes and you want to take the screenshot, you can do that by using Robot class.
Steps to capture screenshot using Robot class:
1. Create an object of Robot class.
2. Get the screen size as a rectangle.
3. Use the createScreenCapture method of Robot class which will capture the screenshot and store in the temp folder.
4. Save the captured image into an 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)); } } }
Leave a Reply