Saturday, July 14, 2007

Robot Class

Robot class is one of the most common classes used by guys doing automated testing. Giving some cool features to automate process. But still I find some thing lacking in this library or maybe I don't know how to use them.

Here I try to write a simple code, which write some text on a text editor and then close the editor without saving the file. Many things are hard coded in this example, like

robot.mouseMove(1420,20); // this is my desktop close button place in full screen mode.
So, if you want to run this code, just change this line according to your desktop. After changing run this code, open any editor say notepad within 10 seconds and then this example will write some text on it and then it will close the window.

import java.awt.Robot;
import java.awt.event.*;

public class MainClass {

public static void main(String[] args) throws Exception{


Robot robot = new Robot();
// giving you 10 sec to open any text editor.
robot.delay(10000);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_T);


robot.mouseMove(1420,20);
robot.mousePress( InputEvent.BUTTON1_MASK );
robot.mouseRelease( InputEvent.BUTTON1_MASK );
Thread.sleep(50);
robot.mousePress( InputEvent.BUTTON1_MASK );
robot.mouseRelease( InputEvent.BUTTON1_MASK );
robot.delay(1000);
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_ENTER);

}
}

This is some useless example, but one can thing of some good examples but using this class.

6 comments:

himanshu said...

nice article. I have a question how do I press combination key like Alt+M?

Vaibhav said...

Thanks Himanshu,

That will not go tough. MousePress Event on Alt and then MousePress on M and then MouseRelease on Alt, will do what you want.
And if you want something to be visual(not in this case) then give a delay.

Andrea Francia said...

Using Robot directly to create automated test can be a pain every time the windows layout change.
We use the Fest Swing library to do such test.

Vaibhav said...

Ya there is little pain and I guess thats the reason why Robot class is not a big hit in Java.

Thanks for the information !

Anonymous said...

> Using Robot directly to create automated test can be a pain every time the windows layout change.

Are you even Java programmers?

Implement UITestable on your components, and have a method that returns, or executes, a UI test on that component, taking a robot instance (or using a static call).

Each button could not only create a test for itself (knowing its coords) but could call a factory method to generate an interpolated series of events (using multiple square / linear algorithms for smoothness) to make the pointer move and click... this allows scripted training manuals that use the interface!

How do I know? Because I did it, and it was very little work... stop complaining!

MenuItems were fun, you can get the JMenu and popups and with one call it shows itself in a tree of menus, and selects itself. Very nice.

Vaibhav said...

thank you !