Automating GUI actions, including controlling a mouse and keyboard programmatically, is often useful. This can be used, for example, to demonstrate GUI usage.

java.awt.Robot class
This class has three main functionalities: mouse control, keyboard control, and screen capture. Here are some of the important methods.

Mouse control functions:
void mouseMove(int x, int y)
This function moves the cursor to the coordinate (x, y) which is defined with respect to the top-left screen corner
void mousePress(int buttons)
void mouseRelease(int buttons)
This pair of functions performs the button click. Their input argument is an OR’ed combination of java.awt.event.InputEvents
java.awt.event.InputEvent.BUTTON1_MASK   // left mouse button
java.awt.event.InputEvent.BUTTON2_MASK   // middle mouse button
java.awt.event.InputEvent.BUTTON3_MASK   // right mouse button

Keyboard control functions:
Keyboard action is emulated by the following pair of functions. Their keycodes are defined in java.awt.event.KeyEvent
void keyPress(int keycode)
void keyRelease(int keycode)
NOTE: Although java.awt.event.KeyEvent constants defines most of the US QWERTY keys, not all can actually be used with java.awt.Robot. Specifically, it appears that only the KeyEvent constants for unmodified keys can be used. See the following section for an example.

Utility functions:
The robot can be put to sleep for a desired duration (in milliseconds). Also, the calling routine can be blocked until the robot exhausts its command queue.
void delay(int ms)
void waitForIdle()

Example: The following program will open existing notepad file(sample.txt) using TestNG and Robot Class

package com.selcukes.scripts;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import org.testng.annotations.Test;
public class FileOpenTest {
 public static void setClipboardData(String string) {
  StringSelection stringSelection = new StringSelection(string);
  Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
 }
 @Test
 public void openTextFile() throws AWTException {
  //After opening window
  setClipboardData("D:\\RB\\sample.txt");
  //Native key strokes for CTRL, V and ENTER keys
  Robot robot = new Robot();
  robot.delay(1000);
  //Open Run command using WINDOWS+R shortcut
  robot.keyPress(KeyEvent.VK_WINDOWS);
  robot.keyPress(KeyEvent.VK_R);
  robot.delay(1000);
  robot.keyRelease(KeyEvent.VK_WINDOWS);
  robot.keyRelease(KeyEvent.VK_R);
  //Enter "Notepad" in Run command and press enter key
  robot.keyPress(KeyEvent.VK_N);
  robot.keyPress(KeyEvent.VK_O);
  robot.keyPress(KeyEvent.VK_T);
  robot.keyPress(KeyEvent.VK_E);
  robot.keyPress(KeyEvent.VK_P);
  robot.keyPress(KeyEvent.VK_A);
  robot.keyPress(KeyEvent.VK_D);
  robot.keyPress(KeyEvent.VK_ENTER);
  robot.keyRelease(KeyEvent.VK_ENTER);
  robot.delay(1000);
  //Shortcut for opening file is CTRL+O
  robot.keyPress(KeyEvent.VK_CONTROL);
  robot.keyPress(KeyEvent.VK_O);
  robot.delay(3000);
  //Paste file name 
  robot.keyPress(KeyEvent.VK_CONTROL);
  robot.keyPress(KeyEvent.VK_V);
  robot.keyRelease(KeyEvent.VK_V);
  robot.keyRelease(KeyEvent.VK_CONTROL);
  robot.keyPress(KeyEvent.VK_ENTER);
  robot.keyRelease(KeyEvent.VK_ENTER);
 }
}

Example: The following program will capture screen for every 10 seconds and stores the screenshot in specified location.

package com.selcukes.scripts;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ScreenCapture {
 private int delay = 10000;
 public static void main(String[] argv) throws InterruptedException {
  ScreenCapture screenCapture = new ScreenCapture();
  screenCapture.captureScreenShots();
 }
 boolean captureScreenShots() throws InterruptedException {
  boolean isSuccesful = false;
  Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
  try {
   Robot robot = new Robot();
   BufferedImage[] screenshots = new BufferedImage[20];
   Rectangle bounds = new Rectangle(0, 0, (int) size.getWidth(), (int) size.getHeight());
   for (int i = 1; i < screenshots.length; i++) {
    System.out.println("Running");
    screenshots[i] = robot.createScreenCapture(bounds);
    Thread.sleep(delay);
    try {
     ImageIO.write(screenshots[i], "jpeg", new File("D:/RB/images" + i + ".jpeg"));
      // Writing the array of screenshots to the Files on the specified path
    } catch (IOException e) {
     e.printStackTrace();
     isSuccesful = false;
    }
   }
  } catch (AWTException e1) {
   e1.printStackTrace();
  }
  return isSuccesful;
 }
}