I'm not looking for a GUI test tool to do this, I want to do it from
my own java application.
TLDR: checkout the source of assertj-swing and decide only after that if you need to write your own.
I would like to ask for your patience as I still start my answer off referencing an existing testing tool. assertj-swing (fork of FEST) shows off the following example in their getting started guide.
public class SimpleCopyApplicationTest {
private FrameFixture window;
@BeforeClass
public static void setUpOnce() {
FailOnThreadViolationRepaintManager.install();
}
@Before
public void setUp() {
SimpleCopyApplication frame = GuiActionRunner.execute(() -> new SimpleCopyApplication());
window = new FrameFixture(frame);
window.show(); // shows the frame to test
}
@Test
public void shouldCopyTextInLabelWhenClickingButton() {
window.textBox("textToCopy").enterText("Some random text");
window.button("copyButton").click();
window.label("copiedText").requireText("Some random text");
}
@After
public void tearDown() {
window.cleanUp();
}
}
See: http://joel-costigliola.github.io/assertj/assertj-swing-getting-started.html
This example differs from how browser automation tests (for example with Selenium) usually would work. Instead of running the test against a real instance of your app, it wraps your apps GUI container (higher or lower level) like a JFrame in another object called Fixture. The examinations will be then executed against an instance of this Fixture object.
Would that mean, that it is not possible to run your whole app? No.
If you spend some time in the github repository of assertj-swing, it has a class called ApplicationLauncher.java, which allows you to instantiate a class that has a main method.
Executes a Java application from a class that has a "main" method.
https://github.com/joel-costigliola/assertj-swing/blob/master/assertj-swing/src/main/java/org/assertj/swing/launcher/ApplicationLauncher.java
Let's remember Sergiy mentioned java.awt.Robot which is really relevant here.
This class is used to generate native system input events for the
purposes of test automation, self-running demos, and other
applications where control of the mouse and keyboard is needed. The
primary purpose of Robot is to facilitate automated testing of Java
platform implementations.
Using the class to generate input events differs from posting events
to the AWT event queue or AWT components in that the events are
generated in the platform's native input queue. For example,
Robot.mouseMove will actually move the mouse cursor instead of just
generating mouse move events.
Source: https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html
We can see another example now:
ApplicationLauncher.application(app.qahelp.core.app.Runner.class)
.withArgs(arg).start();
...
robot = BasicRobot.robotWithCurrentAwtHierarchy();
// Find main frame of application
FrameFixture frame = WindowFinder.findFrame(
getMainFrameByName("Celsius")).using(robot);
frame.focus();
// Type 120 in text box
frame.textBox("tempCelsius").setText("120");
...
frame.button("convertTemp").click();
...
// Get result conversion
JLabelFixture lableResult = frame
.label(getLableTextByTextContain("Fahrenheit"));
AssertJUnit.assertTrue(lableResult.text().contains("248"));
...
Source: http://helpqaspb.com/swing.htm
The whole point of my answer, is that I believe you should learn to use one of the existing tools that comes with source. Then if you understand how it works, make sure you really need your own tool or you can just maybe contribute to an existing one.