Comments
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Cloud Expo on Google News

SYS-CON.TV
Cloud Expo & Virtualization 2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Cloud Computing & Enterprise IT: Cost & Operational Benefits
How and Why is a Flexible IT Infrastructure the Key To the Future?
Click For 2008 West
Event Webcasts
Frank's Java Code Stack #2 Raising Automated Events
Frank's Java Code Stack #2 Raising Automated Events

(October 18, 2002) - Many Java developers have asked me why is it not possible, or rather, most cumbersome to write a Java based Trojan. Since Java by itself is constrained by many useful glitches like byte code verification, type checking, and other painfully time-consuming formalities, Java programmers might shun JNI calls for raising platform specific events including key logging, screen capture, and so on. (Hey, give them a break!). Now forget about Trojans. How can you code Java software, which can act as a Computer Based Tutorial (CBT), wherein the functionality of the software is explained interactively through a set of dynamic events-with automated mouse moves, key presses, screen grabbing et all? Simple. The Robot class in the AWT package lets you append events to the platform dependent native Event queue. Let us create a small code, which outlines basic Event automation.

Code:

1.        /*---A Simple class which performs platform
2.        dependent automated tasks-----*/
3.        import java.awt.*;
4.        import java.awt.event.*;
5.        import java.awt.image.*;
6.        import java.io.*;
7.        import com.sun.image.codec.jpeg.*;
8.
9.        public class RobotJen{
10.
11.        /*---A simple method which grabs the screen upon
12.        getting the Width, Height and Screen Resolution
13.        ---*/
14.        public static void screenGrab
15.        (int x1, int y1, int x2, int y2)
16.        {
17.        try{
18.        System.out.println("Trying to grab screen...");
19.
20.        /*---java.awt.Robot class helps you in
21.        Automating most of the platform dependent tasks
22.        ---*/
23.        Robot afj=new Robot();
24.        BufferedImage shot = afj.createScreenCapture
25.        (new Rectangle(x1, y1, x2,y2));
26.
27.        /*--Write the captured screen pixel data
28.        as a JPEG File Stream
29.        ---*/
30.        OutputStream out = new BufferedOutputStream
31.        (new FileOutputStream("shot.jpg"));
32.        JPEGImageEncoder enc =
33.        JPEGCodec.createJPEGEncoder(out);
34.        enc.encode(shot);
35.        out.close();
36.
37.        }catch(Exception e){
38.        System.out.println("Error grabbing Screen!");}
39.        }
40.
41.        /*--- This method demonstrates how keys can be
42.        automatically pressed by adding Events in the
43.        platform's native queue
44.        ---*/
45.        public static void changeAppFocus()
46.        {
47.        try{
48.        Robot afj=new Robot();
49.        System.out.println
50.        ("Trying to change App focus...");
51.        afj.delay(1000);
52.
53.        /*-- Be sure to release the key after
54.        pressing it since keytype is logically
55.        keyPress+keyRelease. Else your system will
56.        act strangely
57.        ---*/
58.        /*--Trying to change App focus
59.        OS Dependent!!
60.        ---*/
61.        afj.keyPress(KeyEvent.VK_ALT);
62.        afj.keyPress(KeyEvent.VK_TAB);
63.        afj.keyRelease(KeyEvent.VK_TAB);
64.        afj.keyRelease(KeyEvent.VK_ALT);
65.
66.        }catch(Exception e){
67.        System.out.println("Error changing App focus!");}
68.        }
69.
70.        /*--- This method demonstrates how mouse pointer
71.        motion can be controlled through code. Note that
72.        unlike other AWT Events, mouseMove will move the
73.        mouse instantly and does not raise an event
74.        ---*/
75.        public static void moveMouse()
76.        {
77.        try{
78.        Robot afj=new Robot();
79.        System.out.println
80.        ("Trying to generate Mouse Events...");
81.
82.        /*---Move mouse pointer to a random
83.        location on the screen, click the right
84.        button and left button once
85.        ---*/
86.        afj.mouseMove(400,400);
87.        afj.mousePress(InputEvent.BUTTON3_MASK);
88.        afj.mouseRelease(InputEvent.BUTTON3_MASK);
89.        afj.delay(1000);
90.        afj.mousePress(InputEvent.BUTTON1_MASK);
91.        afj.mouseRelease(InputEvent.BUTTON1_MASK);
92.
93.        }catch(Exception e){
94.        System.out.println("Error raising events!");}
95.        }
96.
97.        public static void main(String ar[]){
98.        screenGrab(0,0,1024,768);
99.        changeAppFocus();
100.        moveMouse();
101.        }
102.        }

Note: The above code works only from JDK1.3 onwards. The code works fine with Linux 2.4.1 and all versions of Windows. However if you get AWTException while creating Robot Objects, check if you have privileges to write events to the native queue. This might happen if your X server does not support valid extensions like XTEST 2.2.

Assignment: Create a simple CBT, which uses mouse press and key press Events to illustrate the usability of a software.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Yes you can use this to automate existing apllications. What you need to understand is that we are forcing in events in the Platform Native queue. So you can automate any application including non-java by raising Key and Mouse Events.
Hint: If you want to automatically press a button in an existing application, just write an external patch which knows the screen co-ordinate of the button, and at a specified time move the mouse over that button (X,Y) and and perform a left click. You cannot compare JUnit with this humble Robot class.;-)

I am curious if this Robot class can be used to "attach" to a running Java app and "control" it in such a way that I can write test scenarios using this class to automate existing applications written in Java. This would allow developers to create full blown automation testing that can then be ran from an ant nightly build script and report results, so that the next day engineers could see the automation test results and know what to fix if anything broke.

Would this be possible? Or does it have to be done inline with the actual application? What is the difference, other than GUI control, of the Robot class as opposed to using JUnit to test.


Your Feedback
Frank Jennings wrote: Yes you can use this to automate existing apllications. What you need to understand is that we are forcing in events in the Platform Native queue. So you can automate any application including non-java by raising Key and Mouse Events. Hint: If you want to automatically press a button in an existing application, just write an external patch which knows the screen co-ordinate of the button, and at a specified time move the mouse over that button (X,Y) and and perform a left click. You cannot compare JUnit with this humble Robot class.;-)
Kevin wrote: I am curious if this Robot class can be used to "attach" to a running Java app and "control" it in such a way that I can write test scenarios using this class to automate existing applications written in Java. This would allow developers to create full blown automation testing that can then be ran from an ant nightly build script and report results, so that the next day engineers could see the automation test results and know what to fix if anything broke. Would this be possible? Or does it have to be done inline with the actual application? What is the difference, other than GUI control, of the Robot class as opposed to using JUnit to test.
Latest Cloud Developer Stories
Swisscom, the Swiss telecom, is going into the cloud business. Its subsidiary Swisscom IT Services AG has signed up with Red Hat as a Certified Cloud Provider and launched a public cloud Infrastructure-as-a-Service (IaaS) cloud targeting enterprise-class customers primarily in ...
Apache Deltacloud, the Red Hat-contributed ReSTful API that abstracts differences between clouds so services on any cloud can be managed – provided of course there’s a driver – has graduated from the Apache Foundation’s incubator and is now a full-fledged Top-Level Project (TLP)....
In a surprise move on Tuesday, January 10, Oracle wheeled out its Big Data Appliance. That’s the one it said in October would be ready sometime in the first half. Only nobody believed it meant early in the first half. Heck, it’s not even clear anybody thought Oracle could make ...
Rackspace Hosting, the service leader in cloud computing, on Thursday announced its acquisition of SharePoint911, an industry leader in SharePoint consulting, training, and "JumpStart" services within SharePoint. The unification of both companies provides capabilities to deliver ...
CloudLinux, Inc., on Thursday released CafeFS 3, a virtualized file system for shared hosters that cages each customer within its own virtualized file system. CageFS becomes part of CloudLinux OS at no additional charge. CloudLinux OS, the only commercially-supported Linux OS m...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE

Breaking Cloud Computing News

NASHVILLE, Tenn., Feb. 16, 2012 /PRNewswire/ -- Brookdale Senior Living Inc. (NYSE: BKD) (the "Co...