Java Robot Tutorial


Writing automated tests for graphical user interfaces presents unique challenges.
One tool provided by Java is the AWT Robot.

According to Sun,

    "This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations."

Basically, a Robot object makes it possible for your program to temporarily take over control of the mouse and the keyboard.

A word of caution

A runaway Java Robot object has the ability to wrest control away from the human user, so you need to be a little careful.  For example, if you allow your Java Robot program to go into an infinite loop, making mouse moves, clicking the mouse, and entering keystrokes, you may find that the only practical way to regain control of your computer is to either turn off the power or press the reset button to force your computer to restart.

Similarly, the robot is not confined to the program under test: it will click anywhere on the desktop, potentially closing other open windows or performing undesirable tasks.

Using the Robot

To use the robot, make an instance of Robot class, make an instance of the GUI you want to test (make sure it's visible and has focus), then issue robot method calls to perform the desired GUI operations.  You can click on buttons, fill in text fields, tab between input fields, and so on.

The Robot class provides several instance methods by which your program can produce mouse and keyboard input, just as though that input were being provided by a human user:

    mouseMove - Moves the mouse pointer to a set of specified absolute screen coordinates given in pixels.
    mousePress - Presses one of the buttons on the mouse.
    mouseRelease - Releases one of the buttons on the mouse.
    keyPress - Presses a specified key on the keyboard.
    keyRelease - Releases specified key on the keyboard.

This statement moves the mouse pointer to position (100,10) on the screen:

  robot.mouseMove(100,10);

This statement makes the robot pause by causing the thread to sleep for two seconds (2000 milliseconds).  This is helpful if Swing is taking a while to refresh a window, or if you want a moment to observe the screen before anything else happens.

  robot.delay(2000);

Strengths and Weaknesses

AWT Robot can be used for almost any graphical application.  It doesn't use the Swing event thread, it uses native mouse and keyboard events, so you can even drive non-Java applications.   You can even switch between multiple applications running simultaneously.  It's built-in and requires no third-party libraries. 
Unfortunately it's quite tedious to write the test code because it's at such a low level.  The robot command are very sensitive; if you alter the GUI the test code may have to be altered also.  It can be difficult to get the timing of the robot to match the speed at which GUI changes happen.

Complete Example

Download this NetBeans project for a GPA calculator application.
The user enters the letter grade earned in a course and the number of units earned,
and when the button is pressed, the GPA is calculated.  

GPA calc
Each subsequent computation adds to the total.

Run the application manually (the main method is in the GUI class).  
Enter a grade of "B" for a 4 unit course and click "Compute".
Observe the result.  Now enter a grade of "A" for a 2 unit course and click "Compute".
Observe the cumulative total is computed.

Run the GPATest. It should pass.

Run the RobotTester class.  DO NOT TOUCH the keyboard or mouse while the robot is executing.
Observe the GUI appears and then the text fields are completed automatically by the robot.
Observe the button is clicked and the results appear.
The GUI should then close and the robot terminates.

View the RobotTester class.

First, make a GUI and set visible.
        gui = new GUI();
        gui.setVisible(true);


Initialize the Robot.
        robot = new Robot();
        robot.setAutoDelay(40);
        robot.setAutoWaitForIdle(true);
        robot.delay(500);


Next, enter input data into text fields, tabbing from one to the next.
        type("b");
        type(KeyEvent.VK_TAB);
        type("4");
        type(KeyEvent.VK_TAB);
        robot.delay(500);


Now, trigger the Compute button by pressing the space bar.
        type(KeyEvent.VK_SPACE);

Pause to allow the human observer to verify the result.
        robot.delay(2000);

Notice the robot has not "assert" methods, so there is no way to automatically verify results.  A human inspector is required to look at the visual display and verify correctness.

Exercise

Add to the RobotTester the commands need to enter a grade of A for a 2 unit course.
Enter a delay at the end so you can inspect the output fields.
Tip:  Hit tab several times to move the focus back to the first input field.
Tip:  You'll have to erase the data in a field before entering a new value.