How to execute robot API code using testNG

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:


Sample 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 {

public static void execution() throws InterruptedException, AWTException {
WebDriver driver = new FirefoxDriver();
driver.get(“;); // 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.keyPress(KeyEvent.VK_DOWN); // press keyboard arrow key to select Save radio button
// press enter key of keyboard to perform above selected action

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s