Run a Swing application programmatically (remote classes)

I have a somewhat complicated case when I do not have the source code (or compiled class) of the swing application that I am trying to run automatically.

I will try to perform a number of tasks in this application, click a few buttons, click on some parts, etc. I want to do this programmatically.

Every single Swing debugger / robot that I come across wants you to have a class that you are running, and a debugger running along with the class.

The problem is that my application starts when I launch the JNLP application, which checks me (I have to enter a username and password), and then runs a bunch of classes on the remote server. And the Swing app starts up.

I want to be at the point where I can now possibly join the download application and run it programmatically. Sorry, this seems too complicated, but this is the script here ...

There may be no way to do this at all, please tell me also if that is the case ...

+3
source share
1 answer

If you just know where to click, this is not a problem with your own Robot application. Usually this requires only an initial criterion - where the actual program is on the screen.

This may help you:

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());
    }
}
+2
source

Source: https://habr.com/ru/post/1748532/


All Articles