Wednesday, January 2, 2013

Testing with JemmyFX, JavaFX and ScalaTest

To ease the burden of the testing team, developers should always write automated tests. For JavaFX, there is a library called "JemmyFX" which provides an API to write such tests.


Harold Kantner Special Collection Photo [Photo]
Harold Kantner Special Collection Photo

Using Scala and Scalatest, your tests for JavaFX could look like this:



At the moment of writing this blog post, there was no maven artifact release, but in the Sonatype Snapshot Repository there is one. As such, you would have to add the snapshot repository to your pom and add folowing dependencies to your pom:


<!-- for testing javafx applications -->
<dependency>
<groupId>net.java.jemmy</groupId>
<artifactId>JemmyFX</artifactId>
<version>0.9.3-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.java.jemmy</groupId>
<artifactId>JemmyFXBrowser</artifactId>
<version>0.9.3-SNAPSHOT</version>
 <scope>test</scope>
</dependency>
<dependency>
<groupId>net.java.jemmy</groupId>
<artifactId>Jemmy3Core</artifactId>
<version>0.9.3-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.java.jemmy</groupId>
<artifactId>Jemmy3AWT</artifactId>
<version>0.9.3-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.java.jemmy</groupId>
<artifactId>Jemmy3AWTInput</artifactId>
<version>0.9.3-SNAPSHOT</version>
<scope>test</scope>
</dependency>


A production build should not have dependencies to snapshots of course.

In order for maven to find those artifacts, you have to add the Snapshot Repository to your list of known repositories:


<repositories>
  <repository>
    <id>sonatype snapshots</id>
    <snapshots>
<enabled>true</enabled>
    </snapshots>
    <releases>
<enabled>false</enabled>
    </releases>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
  </repository>
</repositories>



JemmyFX is under heavy development at the moment, but I'm positive a stable release will get published soon. In the meantime the dependencies above give you the ability to implement JemmyFX based tests for your project. Have also a look here for some Java examples how to use JemmyFX.

The scala-javafx-archetype repository at github provides a preconfigured project using the technologies mentioned above.

Small update:


Maybe you want to have a look at the tests for JavaFX for JDK 8 - not long ago the SQE Tests were open sourced here.


nxsw031mac:tests lad$ hg pull
Rufe von http://hg.openjdk.java.net/openjfx/8/master/tests ab
Suche nach Änderungen
Füge Änderungssätze hinzu
Füge Manifeste hinzu
Füge Dateiänderungen hinzu
Fügte 18 Änderungssätze mit 1463 Änderungen an 661 Dateien hinzu
(führe "hg update" aus, um ein Arbeitsverzeichnis zu erstellen)
nxsw031mac:tests lad$ hg update
477 Dateien aktualisiert, 0 Dateien zusammengeführt, 1 Dateien entfernt, 0 Dateien ungelöst


!

6 comments:

  1. Did you try this with ui created from fxml ?

    I am able to use the maven repos and run the tests from within eclipse (it sets the project to include pretty much everything)

    But when running from command line my test fails with an error like this;
    java.lang.AssertionError: fx:id="rootBorderPane" was not injected: check your FXML file 'Login.fxml'.

    That file is included in the resource folder of maven, but not sure how jemmy reads all of this

    ReplyDelete
  2. Maybe you try out the skeleton project which can be generated by the scala javafx archetype - this contains a setup to test javafx using fxml. Just enter this on the command line:

    mvn archetype:generate -DarchetypeCatalog=https://oss.sonatype.org/content/repositories/snapshots -DarchetypeGroupId=net.ladstatt -DarchetypeArtifactId=scala-javafx-archetype -DarchetypeVersion=0.2-SNAPSHOT

    and compile it with

    mvn package

    if everything works then you'll see a jemmy test running against the hello world javafx project.

    ReplyDelete
  3. I have implemented the maven artifact generation in JemmyFX build scripts. While ago, actually.

    I have not been able to get around with publishing them, though, so it is just sitting there.

    Shura

    ReplyDelete
    Replies
    1. Hi Shura, could you please try to release the Jemmy artifacts as non-snapshot releases? That would be awesome!

      Thanks,
      Dirk

      Delete
  4. I have a strange behaviour for text values typed in the textfields on OS X: jemmyFX enters the values as if they were typed in on a QWERTY keyboard. My Mac has AZERTY keyboard, OS X is in English. On a Windows PC it works fine. The project is versioned on Git: https://github.com/Javix/hello-javafx-maven. All other projects that use Cucumber work fine; Any ideas ? Thkx

    ReplyDelete
    Replies
    1. I hav the same problem. I ask a question on the Jemmy mailing list.

      Delete