Hello Ninjas. We know that Selenium is one of the best test automation frameworks. It comes up with a lot of features, but there are some tasks that Selenium is not able to do. It cannot handle operating system-level tasks. It also cannot simulate the native keyboard and mouse events. This is what Robot Class helps Selenium to do.
In this article, we will discuss about Robot Class in Selenium. We will also understand why it is used and how we can use it in Selenium. If you are a beginner with Selenium, you must have a doubt about what Selenium is. Before moving on to the main topic, let us understand about Selenium.
What is Selenium?
Selenium is a widely used test automation framework. It is an open-source framework that makes it easy to use for everyone. It is primarily used forautomating web browsers.
Selenium is one of the most preferable frameworks. It provides various tools and libraries. That’s why it enables you to automate web applications across different browsers. It is supported by multiple programming languages(Java, Python, C#, etc).
What is Robot Class?
Selenium is one of the best automation frameworks. But if we want to automate the task of uploading a file or downloading a file to a particular location, it doesn't have any methods to do this task. Then Robot class helps Selenium to do this task.
Robot Class in Java is a predefined class. It was introduced in Java 1.3 version. It is a part of the Java AWT(Abstract Window Toolkit) package. It is used to simulate the mouse and keyboard events. It helps to interact with Windows, popups, and native applications such as a browser, notepad, calculator, etc. In simple words, we can say that it helps us to handle OS-level tasks. This thing we cannot handle with Selenium. That's why Robot Class helps to rescue from these types of tasks. Robot Class offers several predefined methods that help to do tasks quickly and efficiently.
Methods in Robot Class
Robot Class includes several methods that we can use to simulate keyboard and mouse events:
keyPress()
keyPress() method helps to simulate the key press event. Suppose we want to simulate the enter key from the keyboard then, we can write:
robot.keyPress(KeyEvent.VK_ENTER)
keyRelease()
keyRelease() method helps to simulate the key release event. Suppose we want to simulate the down arrow key from the keyboard then, we can write:
robot.keyRelease(KeyEvent.VK_DOWN)
mouseMove()
mouseMove() method helps to simulate the mouse cursor to move the specified coordinates(x,y). Suppose we want to move the cursor to the coordinates(400,500), then we can write:
robot.mouseMove(400, 500)
mousePress()
mousePress() method helps to simulate the mouse button press event. Suppose we want to press the left mouse button, then we can write:
robot.mousePress(InputEvent.Button1_DOWN_MASK)
And if you want to press the right mouse button:
robot.mousePress(InputEvent.Button3_DOWN_MASK)
mouseRelease()
mouseRelease() method helps to simulate the release of the mouse button. Suppose we want to release the left mouse button, then we can write:
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK)
Benefits of Robot Class
There are several benefits of using this Class with Selenium:
It helps to handle OS-level interactions.
It helps in simulating the native keyboard and mouse events.
It helps to increase the test coverage.
It offers Cross-browser and cross-platform compatibility.
It provides precise control over the automation flow.
Now you might have a doubt about how to implement Robot class in Selenium. Let us take a look at it.
Example of Robot Class in Selenium
Suppose we want to upload a file from our local machine to a website. For example, we are taking a demo website that has several tasks to do.
If we want to do it manually, then we can do this by visiting the file upload link and uploading a file manually.
We can do this same task by using the Robot class in Selenium because Selenium doesn't have any particular methods to do this task. To do this task, you need to follow some steps:
Step 1: Open your Eclipse and create a new Java project.
Step 2: Now, we have to add WebDriver, Selenium server, and ChromeDriver to our project. So, right-click on your project, then go to build and then go to configure build path.
Step 3:Now, go to libraries and add external jars. You can download these files from the official website of Selenium.
Step 4: Now, add the jar files to your project and click on apply.
Step 5: Now, create a package CodingNinjas and create a class SeleniumRobot under the same package.
Step 6: Now, we can write code in our class SeleniumRobot to perform the file upload task.
package codingninjas;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
// Creating a class SeleniumRobot
public class SeleniumRobot {
public static void main(String[] args) throws AWTException {
// Set the path of our chrome browser
System.setProperty("webdriver.chrome.driver", "C:\\Users\\naray\\Downloads\\chromedriver_win32\\chromedriver.exe");
// Using the WebDriver
WebDriver driver=new ChromeDriver();
// Accessing the URL
driver.get("http://the-internet.herokuapp.com/upload");
// Creating the Robot class object
Robot selRobo=new Robot();
// Selecting the path using StringSelection
StringSelection stringSelection=
new StringSelection("C:\\Users\\naray\\Downloads\\robot.jpg");
// This will help us to copy the path or to perform Ctrl+C operation
Toolkit.getDefaultToolkit()
.getSystemClipboard().setContents(stringSelection, null);
// Choosing the WebElement and performing a click on it
Actions actions=new Actions(driver);
WebElement choosingButton= driver.findElement(By.id("file-upload"));
actions.moveToElement(choosingButton).click().build().perform();
// Setting the delay of 2 seconds
selRobo.delay(2000);
// Doing Ctrl+V
selRobo.keyPress(KeyEvent.VK_CONTROL);
selRobo.keyPress(KeyEvent.VK_V);
// Releasing the pressed keys Ctrl+V
selRobo.keyRelease(KeyEvent.VK_CONTROL);
selRobo.keyRelease(KeyEvent.VK_V);
// Setting the delay of 2 seconds
selRobo.delay(2000);
// Pressing enter key to upload the file
selRobo.keyPress(KeyEvent.VK_ENTER);
selRobo.keyRelease(KeyEvent.VK_ENTER);
}
}
This code will give us the result:
Frequently Asked Questions
What do you understand by Robot class in Selenium?
The Robot class is a predefined class in Java. It is also a part of the Java Abstract Window Toolkit(AWT) package. It helps Selenium to simulate the keyboard and mouse events. It also helps to handle OS-level interactions.
How can we use the Robot class in Selenium?
We can use the Robot Class in Selenium by importing the class. Then we have to create an instance of this class. Then we can use its methods to simulate the keyboard and mouse events.
Is Robot class supports cross-browser testing?
Yes, the Robot class supports cross-browser testing. We can use it with Selenium for this task. It ensures consistent test automation across multiple browser environments.
Is the Robot class limited to a specific programming language?
The Robot class is part of the Java AWT package. It is primarily used with Java-based Selenium implementations. However, similar functionality may be available in other programming languages supported by Selenium, such as Python or C#, with their respective libraries.
Conclusion
In this blog, we have discussed about Robot Class in Selenium. AutoIT is one of the important languages that is used in Selenium for scripting. We have discussed why it is used in Selenium, along with steps to use it. You can check out our other blogs to enhance your knowledge: