javaswingjemmy

How to get started with Jemmy v2


I want to learn how to test Java Swing user-interfaces and would like to try Jemmy v2. When trying to follow the instructions on http://jemmy.java.net/tutorial.html I ran into trouble. I created a new project currently consisting of only the following class, which I want to start the application I want to test and create a reference to this application. Unfortunately it seems I'm doing something fundamentally wrong, as an "java.lang.ClassCastException" exception is thrown as soon as the application i want to test is activated.

import org.netbeans.jemmy.*;
import org.netbeans.jemmy.explorer.*;
import org.netbeans.jemmy.operators.*;

public class Main implements Scenario 
{   
    public int runIt(Object param) 
    {
        try {
            //start application
            new ClassReference("org.netbeans.jemmy.explorer.GUIBrowser").startApplication();
            //wait frame
            new JFrameOperator("Tic-Tac-Toe 3.0");
        } catch(Exception e) {
            e.printStackTrace();
            return(1);
        }
        return(0);
    }

    public static void main(String[] argv) 
    {
        String[] params = {"sut2.TicTacToe"};
        org.netbeans.jemmy.Test.main(params);
    }
}

The exception I get:

Shortcut test events
Using org.netbeans.jemmy.drivers.DefaultDriverInstaller driver installer
Executed test sut2.TicTacToe
Exception in thread "main" java.lang.ClassCastException: sut2.TicTacToe cannot be cast to org.netbeans.jemmy.Scenario
    at org.netbeans.jemmy.Test.testForName(Test.java:265)
    at org.netbeans.jemmy.Test.<init>(Test.java:113)
    at org.netbeans.jemmy.Test.run(Test.java:176)
    at org.netbeans.jemmy.Test.run(Test.java:202)
    at org.netbeans.jemmy.Test.main(Test.java:250)
    at Main.main(Main.java:25)

sut2.TicTacToe is a class that implements ActionListener, but does not extend JFrame (I tried another even simpler java program that did, but got the same result). Inside this class a JFrame object is created and panels with GUI components are added.

Could you please tell/show me what I'm doing wrong and how I should properly setup Jemmy v2?


Solution

  • It looks that you pass the wrong params to org.netbeans.jemmy.Test.main. It expects implementation of Scenario. Assuming that Main is in default package, try the following:

    public static void main(String[] argv) {
        String[] params = { "Main" };
        org.netbeans.jemmy.Test.main(params);
    }
    

    You can specify the package of Main as needed.