We all know that selenium is one of the most preferred tool suites for Automation testing of web applications. Have you ever wondered the limitations of Selenium?
Selenium does not provide support to handle window based pop-ups (like download popups, upload popups). Interacting with windows dialog is a limitation in selenium. To perform any action on a web element, we need a locator for the element. But Windows pop-ups don’t have any locators, as they are not part of the webpage, they are native OS pop-ups. So here comes the concept of Robot class to handle such pop-ups.
Robot Class: — In Selenium scripts we use Robot class to automate the browser and desktop pop-ups. Often used in Upload/Download files to/from browsers. To perform mouse and keyboard actions we use Robot class. Robot class is very easy to use with the automation process. It can be easily integrated with Java automation frameworks.
Sometimes we get confused between Action class and Robot class.
Actions class simulates a mouse. It doesn’t move the mouse cursor. Whereas Robot class enables selenium to use an actual mouse.
Useful Robot Class Methods for selenium
• keyPress() : Presses a given key • keyRelease() : Releases a given keyboard key • mousePress() : Presses mouse button based on the input value • mousePress(1) : Presses Primary key • mousePress(2) : Presses Secondary key • mouseRelease() : Releases the mouse button • mouseMove() : Moves mouse pointer to given screen coordinates mouseWheel() : Scrolls the mouse, if the given value is negative scrolled down, in case of positive value wheel scrolled up
Drawbacks of Robot Class in Automation:-
Robot class may fail if permissions are not granted to allow popups or download file is not authorized on client computer etc.
Parallel running must be avoided as Robot class Executes actual mouse commands, so a computer cannot have two mice.
When you use the keyPress event then you should use keyRelease event also. If you cant use keyRelease then it will remain pressed and consume memory in the background. This is the major drawback.
Where I used Robot Class:-
I was working on a Job scraping project, where I need to scrape all the data related to jobs. Job id, Job location, job title, job description are the fields I supposed to scrape from the Glassdoor website and import the data to csv file. For job search we often go to career websites such as DICE, Indeed, Monster, Glassdoor etc., where we need to upload our resume. I want to automate this whole process. So I used Robot class to upload my resume.
Here is how I did it.
Test Scenario 1. Access the website url http://www.glassdoor.com. 2. Sign in with proper credentials. 3. Locate the web elements which leads to upload resume link and upload resume. The user clicks on the Choose File button first to enter the file path to be uploaded. Here, pop-up to select file is Desktop Windows appears. Let’s use the Robot class methods to enter the file path. Here are the steps that we need to follow.
1.Import package: Robot class has to import first, to use. import java.awt.Robot; 2. Instantiate: A robot class object is needed to invoke its methods. So, let’s instantiate the Robot class. Robot rb = new Robot(); 3. Invoke method: Now invoke the required method on robot object. rb.<required_method>(); This is the code that I used to upload a file.
These are the screenshots of the result that we get from the above code.
From this blog, we learned how to initialize, invoke, call robot class for file upload. I Wish this helps you in identifying the use and advantages of Robot class.
Comments