Robot Class in Selenium Webdriver
Last updated on 21st Sep 2020, Artciles, Blog
- Using Robot Class we can handle keyboard and mouse events in Selenium.
- Robot Class can help in upload / download files using selenium web driver.
- Methods in Robot Class can be effectively used to do the interaction with popups in Web Applications.
- Robot Class can easily be integrated with current automation framework (keyword, data-driven or hybrid).
Why Robot Class?
- In certain Selenium Automation Tests, there is a need to control keyboard or mouse to interact with OS windows like Download pop-up, Alerts, Print Pop-ups, etc. or native Operating System applications like Notepad, Skype, Calculator, etc.
- Selenium Webdriver cannot handle these OS pop-ups/applications.
Subscribe For Free Demo
Error: Contact form not found.
Benefits of Robot Class
- Robot Class can simulate Keyboard and Mouse Event
- Robot Class can help in upload/download of files when using selenium web driver
- Robot Class can easily be integrated with current automation framework (keyword, data-driven or hybrid)
Robot Class Handle Below Options
- Press Escape key from keyboard using Different Robot class Methods.
- Handle Mouse Buttons Using Robot class.
- Scroll Mouse Using Robot class.
- Screen Capture Using Robot class.
Package
Using below packages we handle Robot Class Methods.
- import java.awt.Robot;
- import java.awt.event.KeyEvent;
- import java.awt.AWTException;
Methods under Robot Class are:
- .keyPress();
- .mousePress();
- .mouseMove();
- .keyRelease();
- .mouseRelease();
Press Escape key on keyboard using Different Robot class Methods:
1.keyPress()
Using keyPress() Method we Press any key from KeyBoard.
- robot.keyPress(KeyEvent.VK_ESC);// This will press the Escape key on the keyboard
2..keyRelease()
Using keyRelease() Method we Release the Pressed any key from KeyBoard.
- robot.keyRelease(KeyEvent.VK_CAPS_LOCK);// This will release the CAPS_LOCK key
3..mousePress()
Using mousePress() Method we press the Left mouse button from Mouse.
- robot.mousePress(InputEvent.BUTTON1_MASK);// This will press the Left mouse button.
4. .mouseRelease()
Using mouseRelease() Method we Release pressed Left mouse button from Mouse.
- robot.mouseRelease(InputEvent.BUTTON1_MASK);// This will release the Left mouse button.
5. .mouseMove()
Using mouseMove() Method used to Move cursor pointer to X and Y coordinates.
- robot.mouseMove(coordinates.getX(), coordinates.getY());//This will move the mouse pointer to X and Y coordinates.
Use Of Robot Class In Java
When and where we can use Robot Class in Java to enhance the functionality effectively in an automation framework?
As we all know that Selenium can be used to automate web applications. These web applications in Selenium use the underlying web object (locators) of the web app to determine the position on the web page and operate accordingly. Now in certain cases, we see that we need to handle windows authentication pop-up or any other windows pop-up while automating on the Windows operating system.
Selenium cannot automate Windows actions but we can use Robot Class in Java to accomplish the task.
Java Robot Class Examples
Example 1: Using Robot Class to Automate some Windows functionalities
Sometimes any small part/module of the entire web application may be designed in flash. We can use the Robot class to handle that part. Since web objects remain hidden for the part designed in flash.
Another important feature that can be automated using Robot Class in Java is Rich Text Editors, which sometimes remain embedded in web applications. There are several operational functions in RTF editors which is quite easy to automate using this class.
Example 2:Using Robot Class for automating RTF fields
Note: There are other open-source tools available in the market like Auto IT (specially designed to automate windows GUI), Sikuli (image processing tool), etc. to accomplish similar tasks.
This is true but Robot Class in Java comes with JDK. Thus, we do not require any other tool to install, this is a straight advantage if one is using a Java-based automation framework.
Limitations:
But methods mentioned above to control Keyboard and Mouse have some limitations also. Consider some of those following limitations while writing automation scripts:
- Most of the methods like mouseMove are dependent on screen resolution, so, the method may perform differently on different screens.
- This class acts only on the window in focus, so the behavior may differ when multiple windows open.
- Switching between different frames or windows is difficult with Robot methods.
Conclusion:
To conclude, the purpose of this tutorial is to get an introduction to the Robot class. We will be covering Keyboard Events and Mouse Events methods in more detail in our next articles.
Are you looking training with Right Jobs?
Contact Us- Selenium Webdriver
- How to Handle Alert and Pop-ups in Selenium WebDriver?
- Selenium Tutorial
- How to Take A Screenshot in Selenium WebDriver?
- Selenium Interview Questions and Answers
Related Articles
Popular Courses
- Software Testing Training Course
11025 Learners
- Protractor Training
12022 Learners
- Bdd With Cucumber Training
11141 Learners
- What is Dimension Reduction? | Know the techniques
- Difference between Data Lake vs Data Warehouse: A Complete Guide For Beginners with Best Practices
- What is Dimension Reduction? | Know the techniques
- What does the Yield keyword do and How to use Yield in python ? [ OverView ]
- Agile Sprint Planning | Everything You Need to Know