Jemmy Samples

To run any sample you should download Jemmy first. All samples use the GUI browser application as an example of an application to be tested. If it's unclear what the sample does, you can run the application and repeat the test manually:

   java -classpath jemmy_classes_location org.netbeans.jemmy.explorer.GUIBrowser

To execute samples copy the code to java file and run commands:

   javac -classpath "jemmy_classes_location" "sample_name".java
   java -classpath "jemmy_classes_location" "sample_name"

Contents

How to find a frame

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

public class WaitWindowSample implements Scenario {
    public int runIt(Object param) {
	try {
	    //start application
	    new ClassReference("org.netbeans.jemmy.explorer.GUIBrowser").startApplication();
	    //wait frame
	    new JFrameOperator("GUI Browser");
	} catch(Exception e) {
	    e.printStackTrace();
	    return(1);
	}
	return(0);
    }
    public static void main(String[] argv) {
	String[] params = {"WaitWindowSample"};
	org.netbeans.jemmy.Test.main(params);
    }
}

How to find a dialog

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

public class WaitDialogSample implements Scenario {
    public int runIt(Object param) {
	try {
	    //start application
	    new ClassReference("org.netbeans.jemmy.explorer.GUIBrowser").startApplication();
	    //wait frame
	    JFrameOperator mainFrame = new JFrameOperator("GUI Browser");
	    //push menu
	    //pushMenuNoBlock is used, because dialog is modal
	    //see tutorial for more information
	    new JMenuBarOperator(mainFrame).pushMenuNoBlock("Tools|Properties", "|");
	    //wait dialog
	    new JDialogOperator(mainFrame, "Properties");
	} catch(Exception e) {
	    e.printStackTrace();
	    return(1);
	}
	return(0);
    }
    public static void main(String[] argv) {
	String[] params = {"WaitDialogSample"};
	org.netbeans.jemmy.Test.main(params);
    }
}

How to find components

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

public class FindComponentsSample implements Scenario {
    public int runIt(Object param) {
	try {
	    new ClassReference("org.netbeans.jemmy.explorer.GUIBrowser").startApplication();
	    JFrameOperator mainFrame = new JFrameOperator("GUI Browser");

            new JButtonOperator(mainFrame, "Reload after").push();

	    //find JTree
	    //we can find any:
	    JTreeOperator tree = new JTreeOperator(mainFrame);

	    //or by selected node:
	    tree.selectPath(tree.findPath("GUI Browser", "|"));
	    new JTreeOperator(mainFrame, "GUI Browser");
	    
	    //There's only one menubar:
	    new JMenuBarOperator(mainFrame);

	    //and couple of text components:
	    new JTextComponentOperator(mainFrame, "0");
	    new JTextComponentOperator(mainFrame, "toString");

	    //but one of them is a test field:
	    new JTextFieldOperator(mainFrame);

	    //and another one is a text area:
	    new JTextAreaOperator(mainFrame);

	    //it's good idea to search buttons by text:
	    new AbstractButtonOperator(mainFrame, "File");

	    //but not necessarily
	    new JButtonOperator(mainFrame);
	} catch(Exception e) {
	    e.printStackTrace();
	    return(1);
	}
	return(0);
    }
    public static void main(String[] argv) {
	String[] params = {"FindComponentsSample"};
	org.netbeans.jemmy.Test.main(params);
    }
}

How to use actions

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

public class ActionsSample implements Scenario {
    public int runIt(Object param) {
	try {
	    new ClassReference("org.netbeans.jemmy.explorer.GUIBrowser").startApplication();
	    JFrameOperator mainFrame = new JFrameOperator("GUI Browser");

            new JButtonOperator(mainFrame, "Reload after").push();

	    JTreeOperator tree = new JTreeOperator(mainFrame);

	    //click in the middle of the tree
	    tree.clickMouse();

	    //collapse node	    
	    tree.collapsePath(tree.findPath("", "|"));

	    //expand node	    
	    tree.expandPath(tree.findPath("", "|"));

	    //select node
	    tree.selectPath(tree.findPath("GUI Browser", "|"));

	    JTextFieldOperator testField = new JTextFieldOperator(mainFrame);

	    //type new value in the text field
	    testField.clearText();
	    testField.typeText("3");

	    JTextAreaOperator testArea = new JTextAreaOperator(mainFrame);

	    //select text in the text area
	    testArea.selectText("toString");

	    //puch button
	    new JButtonOperator(mainFrame, "Reload").push();

	    //wait "Reloaded" footer
	    new JLabelOperator(mainFrame, "Reloaded");

	} catch(Exception e) {
	    e.printStackTrace();
	    return(1);
	}
	return(0);
    }
    public static void main(String[] argv) {
	String[] params = {"ActionsSample"};
	org.netbeans.jemmy.Test.main(params);
    }
}

The way to keep string resources

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

public class ResourceSample implements Scenario {
    public int runIt(Object param) {
	try {
	    //load bundle
	    Bundle bundle = new Bundle();
	    bundle.loadFromFile(System.getProperty("user.dir") +
				System.getProperty("file.separator") +
				"resourcesample.txt");

	    new ClassReference(bundle.getResource("guibrowser.main_class")).
		startApplication();

	    JFrameOperator mainFrame = 
		new JFrameOperator(bundle.getResource("guibrowser.main_window"));

	    new JMenuBarOperator(mainFrame).
		pushMenuNoBlock(bundle.getResource("guibrowser.show_properties_menu"), 
				"|");

	    new JDialogOperator(mainFrame, 
				bundle.getResource("guibrowser.properties_window"));

	} catch(Exception e) {
	    e.printStackTrace();
	    return(1);
	}
	return(0);
    }
    public static void main(String[] argv) {
	String[] params = {"ResourceSample"};
	org.netbeans.jemmy.Test.main(params);
    }
}

#resourcesample.txt
guibrowser.main_class=org.netbeans.jemmy.explorer.GUIBrowser
guibrowser.main_window=GUI Browser
guibrowser.show_properties_menu=Tools|Properties
guibrowser.properties_window=Properties

Some examples how to use JTableOperator

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

public class TableActionsSample implements Scenario {
    public int runIt(Object param) {
	try {
	    new ClassReference("org.netbeans.jemmy.explorer.GUIBrowser").startApplication();
	    JFrameOperator mainFrame = new JFrameOperator("GUI Browser");

            new JButtonOperator(mainFrame, "Reload after").push();

	    JTreeOperator tree = new JTreeOperator(mainFrame);
	    tree.selectPath(tree.findPath("GUI Browser", "|"));

	    new JMenuBarOperator(mainFrame).pushMenu("Component|View", "|");

	    JFrameOperator viewFrame = new JFrameOperator("GUIBrowser");

	    new JTabbedPaneOperator(viewFrame).selectPage("Properties");

	    //find table
	    JTableOperator table = new JTableOperator(viewFrame);

	    //find row
	    int titleRow = table.findCellRow("GUI Browser");
	    
	    //change table text
	    table.changeCellObject(titleRow, 1, "That was window title :)");

	} catch(Exception e) {
	    e.printStackTrace();
	    return(1);
	}
	return(0);
    }
    public static void main(String[] argv) {
	String[] params = {"TableActionsSample"};
	org.netbeans.jemmy.Test.main(params);
    }
}

Some examples show how to use JTreeOperator

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

public class TreeActionsSample implements Scenario {
    public int runIt(Object param) {
	try {
	    new ClassReference("org.netbeans.jemmy.explorer.GUIBrowser").startApplication();
	    JFrameOperator mainFrame = new JFrameOperator("GUI Browser");

            new JButtonOperator(mainFrame, "Reload after").push();

	    JTreeOperator tree = new JTreeOperator(mainFrame);

	    //click in the middle of the tree
	    tree.clickMouse();

	    //collapse node	    
	    tree.collapsePath(tree.findPath("", "|"));

	    //expand node	    
	    tree.expandPath(tree.findPath("", "|"));

	    //select node
	    tree.selectPath(tree.findPath("GUI Browser", "|"));

	} catch(Exception e) {
	    e.printStackTrace();
	    return(1);
	}
	return(0);
    }
    public static void main(String[] argv) {
	String[] params = {"TreeActionsSample"};
	org.netbeans.jemmy.Test.main(params);
    }
}

Simple example how to treat the event queue from a test

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

public class QueueUsingSample implements Scenario {
    public int runIt(Object param) {
	try {
	    //start application
	    new ClassReference("org.netbeans.jemmy.explorer.GUIBrowser").startApplication();
	    //wait frame
	    JFrameOperator mainFrame = new JFrameOperator("GUI Browser");

	    //push menu
	    new JMenuBarOperator(mainFrame).pushMenuNoBlock("Tools|Properties", "|");

	    //wait for queue to be empty
	    new QueueTool().waitEmpty(100);

	    //since queue is empty, dialog has already been created and displayed.
	    //find (not wait) dialog using low-level functionality
	    System.out.println(JDialogOperator.findJDialog("Properties", false, false).getTitle());
	} catch(Exception e) {
	    e.printStackTrace();
	    return(1);
	}
	return(0);
    }
    public static void main(String[] argv) {
	String[] params = {"QueueUsingSample"};
	org.netbeans.jemmy.Test.main(params);
    }
}

What nonblocking methods are used for

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

public class ModalDialogSample implements Scenario {
    public int runIt(Object param) {
	try {
	    //start application
	    new ClassReference("org.netbeans.jemmy.explorer.GUIBrowser").startApplication();
	    //wait frame
	    JFrameOperator mainFrame = new JFrameOperator("GUI Browser");
	    //push menu
	    //pushMenuNoBlock is used, because dialog is modal
	    //see tutorial for more information
	    new JMenuBarOperator(mainFrame).pushMenu("Tools|Properties", "|");
	    //wait dialog
	    new JDialogOperator(mainFrame, "Properties");
	} catch(Exception e) {
	    e.printStackTrace();
	    return(1);
	}
	return(0);
    }
    public static void main(String[] argv) {
	JemmyProperties.getCurrentTimeouts().
	    setTimeout("Test.WholeTestTimeout", 60000);
	String[] params = {"ModalDialogSample"};
	org.netbeans.jemmy.Test.main(params);
    }
}

Tools to help find the cause of a failure

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

public class GrabImageAndXMLSample implements Scenario {
    public int runIt(Object param) {
	try {
	    new ClassReference("org.netbeans.jemmy.explorer.GUIBrowser").startApplication();
	    JFrameOperator mainFrame = new JFrameOperator("GUI Browser");
	    new JMenuBarOperator(mainFrame).pushMenuNoBlock("Tools|Properties", "|");

	    //grab image
	    PNGEncoder.captureScreen(System.getProperty("user.dir") +
				     System.getProperty("file.separator") +
				     "screen.png");
	    
	    //grab component state
	    Dumper.dumpAll(System.getProperty("user.dir") +
			   System.getProperty("file.separator") +
			   "dump.xml");
	} catch(Exception e) {
	    e.printStackTrace();
	    return(1);
	}
	return(0);
    }
    public static void main(String[] argv) {
	String[] params = {"GrabImageAndXMLSample"};
	org.netbeans.jemmy.Test.main(params);
    }
}
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo