Selenium
Selenium IDE Tutorial for Beginners
What is Selenium IDE? Selenium IDE (Integrated Development Environment) is the simplest tool in...
Robot Class in Selenium is used to enable automated testing for implementations of Java platform. It generates input events in native systems for test automation, self-running demos and other applications where users need control over mouse and keyboard. Robot class is easy to implement and it can be easily integrated with an automated framework.
Robot Class is used in Selenium because, in certain Selenium automation tests, users need control over keyboard or mouse to interact with OS windows like download pop-ups, print pop-ups, etc. and native applications like notepad, calculator, etc. Selenium Webdriver cannot handle these pop-ups/applications, so in Java version 1.3, robot class was introduced which can handle OS pop-ups/applications.
In this tutorial, you will learn,
The Robot Class Documentation in Selenium helps users to understand the basic definition, syntax and usage of all the methods and functions available in robot class in Java AWT package. Users can view the documentation on the Official Oracle website. Users can also create the documentation on their local machine themselves.
To create the documentation on local machine, follow the steps below-
Step 1) You will find the src.zip file in JDK folder. Copy src.zip and extract the same in some other folder or directory (say D: or E: )
Step 2) Extract src folder and Navigate to (path till src folder)/src/java/awt
Step 3) Copy the current location of awt folder and open command prompt.
Step 4) In cmd, change your current directory location to awt folder and type 'javadoc *.java' as shown below
Wait a while for the system to process, once completed you will see few HTML files in awt folder.
Step 5) Open index.html
Step 6) Here's you have full documentation of awt package, from the left navigation bar click on 'Robot' hyperlink (See 1 marked in below image).
Here you can also see all the methods and interfaces of Robot Class (See 2 marked in above image).
Robot Class methods can be used to interact with keyboard/mouse events while doing browser automation. Alternatively AutoIT can be used, but its drawback is that it generates an executable file (exe) which will only work on windows, so it is not a good option to use.
Some commonly and popular used methods of Robot Class during web automation:
Sample code to automate common use cases using Robot Class
Here is a sample code
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
class Excercise1 {
public static void main(String[] args) throws AWTException, InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://spreadsheetpage.com/index.php/file/C35/P10/"); // sample url
driver.findElement(By.xpath(".//a[@href=contains(text(),'yearly-calendar.xls')]")).click();
Robot robot = new Robot(); // Robot class throws AWT Exception
Thread.sleep(2000); // Thread.sleep throws InterruptedException
robot.keyPress(KeyEvent.VK_DOWN); // press arrow down key of keyboard to navigate and select Save radio button
Thread.sleep(2000); // sleep has only been used to showcase each event separately
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
// press enter key of keyboard to perform above selected action
}
} Check this video to see it in action Since, now you aware of basic methods of Robot Class so let's understand few more complex methods -
Suppose you do not want to use the click method for clicking at web element.
In such cases, you can use mouseMove method of the Robot class.
Step 1) mouseMove method takes x and y coordinates as parameters like robot.mouseMove(630, 420) where 630 indicates x-axis and 420 indicate y-axis. So, this method will move your mouse pointer from the current location to mentioned x and y intersection point.
Step 2) Next, we need to press the mouse button. We can use the method mousePress like robot.mousePress(InputEvent.BUTTON1_DOWN_MASK) .
Step 3) After press, the mouse needs to be released. We can use robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK) in order to release left click of a mouse.
Running code using testNG:
Running code using Testng requires maven dependency of testNG or referenced library of TestNG jar file.
TestNG maven dependency:
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.1.1</version> </dependency>
After adding maven dependency or jar file. You need to import Test annotation of testNG. Once it is all done, just Right click on the program code and click on Run As then click on TestNG… and you will find that code will start its execution using testNG API.
Here is the code
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Excersise1 {
@Test
public static void execution() throws InterruptedException, AWTException {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://spreadsheetpage.com/index.php/file/C35/P10/"); // sample url
Robot robot = new Robot();
robot.mouseMove(630, 420); // move mouse point to specific location
robot.delay(1500); // delay is to make code wait for mentioned milliseconds before executing next step
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); // press left click
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); // release left click
robot.delay(1500);
robot.keyPress(KeyEvent.VK_DOWN); // press keyboard arrow key to select Save radio button
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
// press enter key of keyboard to perform above selected action
}
}
Check this video to see it in action
What is Selenium IDE? Selenium IDE (Integrated Development Environment) is the simplest tool in...
In this tutorial, you will learn how to maximize, minimize or resize the browser using selenium...
Report generation is very important when you are doing the Automation Testing as well as for...
Reading a HTML Web Table There are times when we need to access elements (usually texts) that are...
Training Summary Selenium is a popular open-source web-based automation tool. This online course is...
Before we look into anything else, let's first understand - Why do we need reporting? When we are...