2

I want to build a Java GUI test tool for testing a Swing application and I wonder how to find and manipulate components in a Java Swing application and use if for clicking and pressing keys, enter text etc.

The application under tests is started with a batch file setting arguments and finally calling this "%JRE_HOME%\bin\javaw.exe" %ARG% com.sun.javaws.Main %JAVAWS_PARAMS% http://%SERVER%:8080/appundertest/appundertest.jnlp

How can I find for example a button and simulate a click on it from another java program if I new the name of the view and button?

The source code to the program under test is available in the same repository. Instrumentation of the program under test is not allowed.

I'm not looking for a GUI test tool to do this, I want to do it from my own java application.

user1857773
  • 71
  • 2
  • 7
  • post your java code – Donald Wu Oct 28 '16 at 06:58
  • @DonaldWu OP's question is about how to test the GUI of a Java Swing application. What do you need the code for? – walen Oct 28 '16 at 07:15
  • You can make a click using `java.awt.Robot`. But I have no Idea, how you can find a button from another Java program. – Sergiy Medvynskyy Oct 28 '16 at 07:15
  • Possible duplicate of http://stackoverflow.com/questions/91179/automated-tests-for-java-swing-guis – walen Oct 28 '16 at 07:16
  • Maybe you want to take a look at ReTest (https://www.retest.de/en/). It is a novel tool that implements an innovative approach to functional regression testing, which it combines with ai-based monkey testing. – roesslerj Mar 04 '17 at 22:40
  • You can use Testmate to automate Java swing application for free https://youtu.be/BQQdJ0V23GM – Jaskaran May 19 '20 at 06:27

1 Answers1

4

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.

dbalakirev
  • 1,918
  • 3
  • 20
  • 31
  • You can also take a look to our [Sakuli](https://github.com/ConSol/sakuli) Projekt, there your are able to write also UI tests for rich clients. The framework combines a web and UI tests. You can write your test in Java or Javascript: Good starting points are: - [Containerized UI-Tests in Java with #Sakuli and #Docker](https://labs.consol.de/sakuli/development/2016/10/14/sakuli-java-dsl.html) - [First Steps](https://github.com/ConSol/sakuli/blob/master/docs/first-steps.md) - [Github: consol/sakuli-examples](https://github.com/ConSol/sakuli-examples) – toschneck Nov 14 '16 at 12:59