Sr. Software Architect, Passionate about Open source Tools & Technologies. Expert in Enterprise Web Application Development using Java/J2ee Platform. Worked significantly on Enterprise Data Grid, Transactional Data Management, B2B Integration and Performance & Code Instrumentation. Senthil has posted 8 posts at DZone. You can read more from them at their website. View Full User Profile

Write Your Own Custom Automation Using java.awt.Robot

02.10.2010
| 21549 views |
  • submit to reddit
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.

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();
}
}
}
0
Your rating: None
Published at DZone with permission of its author, Senthil Balakrishnan.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

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

Robot is powerful but a real pain to use. So we wrote a wrapper, called Cyborg, which has methods such as enterText(), cut(), selectAll(), clickTableHeader(), dragFrame() and so on. This is all explained in Swing Extreme Testing, which comes with LOTS of useful code and examples.

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

Cool, and simple, but proably easier if you use Selenium as it supports this in the context of functional testing a website. You could use robot to launch native applications and maybe use it to test native apps by using java, just a thought.

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

You can also create screenshot using Robot, it can be handy.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.