views:

62

answers:

1

Dear Friends, I have a slightly complicated case where I do not have source code (or the compiled class) the swing application that I am trying to run automatically.

I will try to do a series of tasks on this application, press some buttons, click on some parts etc. I want to be able to do this programatically.

Every single swing debugger/robot I have come across wants you to have the class you are launching and the debugger launches together with the class.

Problem here is my application is launched by me launching a JNLP application, which authenticates me (I have to enter username and password), then runs a bunch of classes on a remote server. And the swing application gets lauched.

I want to be at a point where I can now perhaps attach to the swing application and run it programatically. Sorry this seems too complicated, but this is the scenario here...

Perhaps there is no way to do it at all, please tell me so also....if that is the case...

thank you so much...

A: 

If you just know where to click it's not a problem doing your own Robot application. It normally only needs a start criteria - where the actual program is on the screen.

This might help you get going:

public class MyRobot extends Robot {

    public MyRobot(Point initialLocation) throws AWTException {

        setAutoDelay(20);

        // focus on the program
        click(initialLocation);

        // if you need to take screen shot use 
        BufferedImage screen = 
            createScreenCapture(
                new Rectangle(initialLocation.x, initialLocation.y, 200, 200));

        // analyze the screenshot...
        if(screen.getRGB(50, 50) > 3) /*do something :) */;


        // go to the correct field
        press(KeyEvent.VK_TAB);

        // press "a"
        press(KeyEvent.VK_A);

        // go to the next field
        press(KeyEvent.VK_TAB);

        // write something...
        type("Hello World..");
    }

    private void click(Point p) {
        mousePress(InputEvent.BUTTON1_MASK);
        mouseRelease(InputEvent.BUTTON1_MASK);
    }

    private void press(int key) {
        keyPress(key);
        keyRelease(key);
    }

    private void type(String string) {
        // quite complicated... see 
        //http://stackoverflow.com/questions/1248510/convert-string-to-keyevents
    }

    @SuppressWarnings("serial")
    public static void main(String[] args) throws Exception {
        final JDialog d = new JDialog();
        d.setTitle("Init");
        d.add(new JButton(
                "Put your mouse above the 'program' " +
                "and press this button") {
            {
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    synchronized (d) { d.notify(); }
                    d.dispose();
                }
            });}
        });
        d.setSize(200, 100);
        d.setVisible(true);
        // wait for it to be closed
        synchronized (d) {
            d.wait();
        }
        new MyRobot(MouseInfo.getPointerInfo().getLocation());
    }
}
dacwe