Robot class in Selenium WebDriver handles Human Interventions

Category: Selenium WebDriver 204 0

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.

Robot class

 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.

Console Output

Robot Class Test Console output

 

Watch this video to learn more [Don’t forget to subscribe to our channel]

Related Articles

Add Comment