Keyboard and Mouse activities are a physical activity where human intervention is required. Until now we discussed automation of various scenarios but those were related to virtual programs only, No requirement of keyboard and mouse triggering. Since Keyboard and Mouse activities are performed by human only, so we require any technology which could replace human. Hence, Robot is the next generation human being who does our work. So here in Selenium, we will also use a Robot by calling Robot class to perform human activities like handling Keyboard as well as Mouse events.
So let us first know something in brief about Robot then we will further proceed with its uses in Selenium WebDriver.
Recommended for you: How to handle Radio and Checkbox button in Selenium?
What is Robot Class in Java?
Robot class, basically, is a kind of another class in Java, which is under the AWT library. It performs multiple tasks like opening a new tab, switching between tabs, Sending commands and Controls from the keyboard, Clicks from mouse, etc.
Thus, It is the solution to all your complicated Selenium tasks where human interventions are required.
How to handle the code when Robot class fails?
In Java, we mainly handle runtime as well as the compile-time failure through an exception. Therefore, we will catch exception which it throws.
Now the question comes here, what kind of exception it throws?
So to answer your question, I’d say Robot class throws AWT exception and you need to catch it wisely before your Selenium test gets terminated.
What are the methods in Robot class?
We use keyPress() and keyRelease() method in Robot class for our Selenium WebDriver test cases.
Do we need to pass any arguments in these methods?
Yes, we need to pass arguments in the above methods. Because they are arguments only which direct Robot class to perform a certain set of operations for your Selenium WebDriver test cases.
How to declare and use Robot class for Selenium WebDriver?
You simply create an instance of Robot as you do for any other class.
Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_ENTER);
rob.keyRelease(KeyEvent.VK_ENTER);
Tip: Once you gave the command to press the key, afterward you also have to give instruction to release the same key; after all, it’s a robot. It hears and follows the instruction given.
Code for Robot Class in Selenium WebDriver
Here is a complete snippet of code where Robot utilized efficiently to perform search activities on the website.
package SeleniumTester; import org.testng.annotations.Test; import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class RobotClass { @Test public void robotClass() throws InterruptedException { //Define System Property to Identify browser System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe"); //Calling ChromeDriver from WebDriver Interface WebDriver driver = new ChromeDriver(); //Wait time load browser driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Test URL driver.get("https://www.inviul.com"); //Window Maximize driver.manage().window().maximize(); Thread.sleep(3000); //Search box Element driver.findElement(By.id("s")).sendKeys("webdriver"); try{ //Creating Instance of Robot class Robot rob = new Robot(); //Calling robot class to press Enter key on Keyboard rob.keyPress(KeyEvent.VK_ENTER); //Calling robot class to release Enter key on Keyboard rob.keyRelease(KeyEvent.VK_ENTER); //Handaling AWT Exception } catch (AWTException e){ e.printStackTrace(); } Thread.sleep(3000); //Driver Closure driver.close(); driver.quit(); } }
Console Output