What is the Robot class? and why I must use robot class in my Selenium automation framework as a Selenium automation engineer.
Hello, my name is Vishal, and in this blog, I will explain why you should use robot class. I was working on a project for one of our clients, and I was asked to test the web-file application’s upload feature. There was a button, and once we click the button, a window pop-up appeared, and I was asked to upload the file to the server of that web application. I was able to automate up until the button clicking part through selenium, but I was unable to automate the window’s pop using Selenium. I tried almost everything to automate that pop-up but failed. After doing some research, I got to know that we cannot use the Selenium application to automate the windows pop-ups.
Then I did more research on the subject and got to know about the Robot class.
What is Robot Class?
Before we start talking about how to use the robot class, we will first learn the basics of the robot class.
We deal with popups and alert many times in the java selenium web automation, using a method like a driver.switchTo(). Most of them can be easily handled using submethods like a driver.switchTo().alert().dismiss() or driver.switchTo().alert().accept() methods. But what if the pop-ups are system-generated, as shown in the following image?
As these pop-ups are not related to the webpage or browser, selenium will not be able to handle these pop-ups. In that case, we use robot class to tackle these situations.
Selenium’s Robot Class is used to enable automated testing for Java platform implementations. It generates input events in native systems for Test Automation, Self-Running Demos, and other applications that require mouse and keyboard control. It is simple to implement and integrate with an automated framework.
So this was about the introduction. In the next section of this, we will learn how to use that.
How to use robot class in selenium?
To understand how to use robot class in selenium, I am using this website. This website allows you to upload sample files.
So, before we begin automating, we must first comprehend the operation that will be carried out on this application. My primary goal here is to upload a text file from my machine’s download folder to the server of that web application. On that webpage, there is a button, and when we click it, a window appears, and I am asked to select the file that I want to upload to the server.
As previously stated, we will be able to automate up to the button-clicking stage, but we will be unable to control the windows pop-up using Selenium.
So, to control that part, we’ll use the robot class. You can use the following code.
Code:
//import dev.failsafe.internal.util.Assert;
import io.github.bonigarcia.wdm.WebDriverManager;
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;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
public class SeleniumExample {
private static WebDriver driver = null;
public static void main(String[] args) throws AWTException, InterruptedException {
//Initialize the Web-driver
driver = driverSetUpForChrome();
driver.get("https://cgi-lib.berkeley.edu/ex/fup.html");
String title = driver.getTitle();
System.out.println("Title of the page is "+ title);
// Locate the upload button
WebElement uploadButton = driver.findElement(By.xpath("//form//input[@Name=\"upfile\"]"));
StringSelection s = new StringSelection("C:\\Downloads\\SampleText.txt");
// Clipboard copy
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(s,null);
Actions actions = new Actions(driver);
actions.click(uploadButton).build().perform();
WebElement noteFiled = driver.findElement(By.xpath("//form//input[@Name=\"note\"]"));
actions.click(noteFiled).sendKeys("Uploading the text file.").build().perform();
WebElement pressButton = driver.findElement(By.xpath("//form//input[@type=\"submit\"]"));
actions.click(pressButton).build().perform();
}
public static WebDriver driverSetUpForChrome() {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
return driver;
}
public static void quitDriver() {
driver.quit();
}
}
When you run the above code, a pop-up window similar to the one shown below will appear on your screen. The system will prompt you for a file name here. In this case, we’ll use the robot class to select the file to upload to the server.
To do so, we must first copy the file to the clipboard. Which we can do with the following line of code.
StringSelection s = new StringSelection("C:\\Downloads\\SampleText.txt");
// Clipboard copy
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(s,null);
So, using the toolkit class, we copied the path to a string, which we need to paste into the pop window’s file field. We already know that we can copy and paste the content into fields by pressing the keyboard’s control and V buttons. Using the robot class, we will do the same thing.
To paste the copied file path, use the code below.
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
//releasing ctrl+v
Thread.sleep(1000);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_V);
Now, after pasting the file path, we need to press the enter key to select the file. we can do with the code below.
r.keyPress(KeyEvent.VK_ENTER);
//releasing enter
r.keyRelease(KeyEvent.VK_ENTER);
We have successfully uploaded the file to the server in this manner.
So this is how we can use the robot class to handle window pop-ups.
The complete code is provided below.
//import dev.failsafe.internal.util.Assert;
import io.github.bonigarcia.wdm.WebDriverManager;
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;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
public class SeleniumExample {
private static WebDriver driver = null;
public static void main(String[] args) throws AWTException, InterruptedException {
//Initialize the Web-driver
driver = driverSetUpForChrome();
driver.get("https://cgi-lib.berkeley.edu/ex/fup.html");
String title = driver.getTitle();
System.out.println("Title of the page is "+ title);
// Locate the upload button
WebElement uploadButton = driver.findElement(By.xpath("//form//input[@Name=\"upfile\"]"));
StringSelection s = new StringSelection("C:\\Downloads\\SampleText.txt");
// Clipboard copy
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(s,null);
Actions actions = new Actions(driver);
actions.click(uploadButton).build().perform();
Robot r = new Robot();
//pressing enter
//pressing ctrl+v
Thread.sleep(1000);
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
//releasing ctrl+v
Thread.sleep(1000);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_V);
//pressing enter
r.keyPress(KeyEvent.VK_ENTER);
//releasing enter
r.keyRelease(KeyEvent.VK_ENTER);
WebElement noteFiled = driver.findElement(By.xpath("//form//input[@Name=\"note\"]"));
actions.click(noteFiled).sendKeys("Uploading the text file.").build().perform();
WebElement pressButton = driver.findElement(By.xpath("//form//input[@type=\"submit\"]"));
actions.click(pressButton).build().perform();
}
public static WebDriver driverSetUpForChrome() {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
return driver;
}
public static void quitDriver() {
driver.quit();
}
}
More about Robot Class Methods and Use:
In this section, we will learn more about the feature and its uses.
Robot robot = new Robot();
At this line, we are initializing the robot class.
keyPress():
For example robot.keyPress(KeyEvent.VK DOWN): This function is one keyword and that keyword is the name of the button that you want to press. For example, if you want to press button V then you will have to pass the following object:
KeyEvent.VK_V
mousePress():
For example, robot.mousePress(InputEvent.BUTTON3 DOWN MASK) will perform a right mouse click.
mouseMove():
For example, robot.mouseMove(point.getX(), point.getY()) will move the mouse cursor to the X and Y coordinates supplied.
keyRelease():
For example, robot.keyRelease(KeyEvent.VK DOWN): This method releases the Keyboard’s down arrow key. If there are any keys that you have pressed using the KeyPress function, then you can use this function to release those keys. – mouseRelease(): For example, robot.mouseRelease(InputEvent.BUTTON3 DOWN MASK): This method will release your mouse’s right click.
Conclusion:
So, in this way, we learned about the robot class and its application in this blog. I hope you got the information you were looking for. please share it with your testing squad, and if you have any suggestions or questions, please leave them in the comment section.
11Vishal is proficient in a variety of programming languages, including Python, Java, and Javascript, C++, Jpython, C. He has also worked on a number of testing technologies, such as playright, selenium, and helenium. He is an tie loathing adventurer and thrill seeker, who believes in turning ideas into reality…