Write Your Own Custom Automation Using java.awt.Robot
The following tip shows how to use java.awt.Robot to create your own handy custom made automation. Running
this example will open up Firefox in your system and type in twitter.com, loading the page for you. Isn't that cool? Try it yourself.
Published at DZone with permission of its author, Senthil Balakrishnan.import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
public class RobotSample {
public static void main(String[] args) throws IOException {
try {
Robot robot = new Robot();
Runtime runtime = Runtime.getRuntime();
runtime.exec("D:\\Program Files\\Mozilla Firefox\\firefox.exe");
robot.delay(1000);
robot.keyPress(KeyEvent.VK_F6);
robot.keyPress(KeyEvent.VK_DELETE);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_T);
robot.delay(70);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_R);
robot.keyPress(KeyEvent.VK_DECIMAL);
robot.keyPress(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_M);
robot.keyPress(KeyEvent.VK_ENTER);
robot.delay(200);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)
Tags:





Comments
Thierry Milard replied on Wed, 2010/02/10 - 3:19pm
this is in fact interessant and usefull.
But still, I do have one question/criticise...
Ok, you type "twitter.com"
1) Your solution with Robot is 13 lignes long (my god !):
17.robot.keyPress(KeyEvent.VK_T);18.robot.keyPress(KeyEvent.VK_W);19.robot.keyPress(KeyEvent.VK_I);20.robot.keyPress(KeyEvent.VK_T);21.robot.delay(70);22.robot.keyPress(KeyEvent.VK_T);23.robot.keyPress(KeyEvent.VK_E);24.robot.keyPress(KeyEvent.VK_R);25.robot.keyPress(KeyEvent.VK_DECIMAL);26.robot.keyPress(KeyEvent.VK_C);27.robot.keyPress(KeyEvent.VK_O);28.robot.keyPress(KeyEvent.VK_M);29.robot.keyPress(KeyEvent.VK_ENTER);2) If only the robot could do this in 2 lignes :
17.robot.keysPress("TWITTER.COM");18.robot.keyPress(KeyEvent.VK_ENTER);Better no ?
Thierry
Tim Lavers replied on Wed, 2010/02/10 - 5:50pm
Senthil Balakrishnan replied on Wed, 2010/02/10 - 6:27pm
in response to:
Thierry Milard
Thierry,
Robot doesn't have api "keysPress", I guess it's left for brilliant developers to abstract later.
Thanks,
Senthil Balakrishnan
Senthil Balakrishnan replied on Wed, 2010/02/10 - 6:30pm
in response to:
Tim Lavers
Tim,
Sounds an interesting book " Swing Extreme Testing", will buy one !!!
Thanks,
Senthil Balakrishnan
Walter Bogaardt replied on Wed, 2010/02/10 - 7:49pm
Senthil Balakrishnan replied on Wed, 2010/02/10 - 8:49pm
in response to:
Walter Bogaardt
Walter,
I agree with you, robot is perfect fit for loading native apps. I have used selenium its a nice tool, I liked the Selenium RC(multi browser testing) the most.
Thanks,
Senthil Kumar
Gabor Farkas replied on Tue, 2010/02/16 - 7:48am