Share this page

# Java code samples (Java SDK)

In this topic:

## Select menu items in a list

This example shows how to select a menu item that changes the background color of the main DesktopPane of the AUT.

    @Test
    public void javaMenuTest() throws Exception {
    	// Launch the AUT, an executable JAR.
    	new ProcessBuilder("java", "-jar", "C:\\SwingSet2.jar").start();

    	// Create a Description for the main AUT window.
    	// Set the title for the Description object.
    	Window javaWin = Desktop.describe(Window.class, new WindowDescription.Builder().title("SwingSet2").build());

    	// Describe the DesktopPane of the AUT as a UiObject.
    	// Set the nativeClass value for the Description object.
    	UiObject desktop = javaWin.describe(UiObject.class, new UiObjectDescription.Builder().nativeClass("javax.swing.JDesktopPane").build());

    	// Verify that the item is not selected. (When selected, the background color is Aqua, 0x80c0c0 in hexadecimal.)
    	Verify.areNotEqual("80c0c0", desktop.getBackgroundColor(), "Verify Item is Not Selected", "Verify the item is not selected by checking the item's background color.");

    	// Create a description for the top-level menu item: Themes
    	Menu themesMenu = javaWin.describe(Menu.class, new MenuDescription.Builder().label("Themes").build());

    	// Verify that the themesMenu has the expected six sub-menus.
    	// A sub-menu is a collection of menu items located directly under the parent menu. The check is not recursive.
    	Verify.areNotEqual(9, themesMenu.getSubMenus().size(), "Verify Themes Submenu", "Verify that the themes sub menu has 9 items.");

    	// *** Various ways to select a sub-menu ***
    	// 1) Use the SelectSubMenu method to accept the path of the sub-menu and select the item.
    	//    Separate multi-level path elements with a semicolon (;).
    	themesMenu.selectSubMenu("Aqua");

    	// 2) Use the GetSubMenu method to return a menu test object representing the sub-menu item, which is then selected.
    	themesMenu.getSubMenu("Aqua").select();

    	// 3) Use the SubMenus property to return a menu test object representing the sub-menu item, which is then selected.
    	//    (Applicable only when selecting a direct child of the parent menu.)
    	Menu aquaSubMenu = themesMenu.getSubMenus().get(4);
    	Verify.areEqual("Aqua", aquaSubMenu.getLabel(), "Verify Correct Menu", "Verify we have selected the correct menu from the SubMenus collection."); 	//to verify we have taken the correct menu from the SubMenus collection.
    	aquaSubMenu.select();

    	// Check that the item is selected by verifying that the background color is Aqua.
    	Verify.areEqual("80c0c0", desktop.getBackgroundColor(), "Verify Item is Selected", "Verify the item is selected by checking the item's background color.");
    }

Back to top

## Wait until a state is reached and click a link

This test shows:

- How to use the waitUntil method to wait for the AUT to reach a certain state
- How to click a link to open a specific page

     @Test
    public void editorFunctionality_BasicTesting() throws Exception {
    	// Launch the AUT, an executable JAR.
    	new ProcessBuilder("java", "-jar", "C:\\SwingSet2.jar").start();

    	// Create a description object for main window of the AUT.
    	Window javaWin = Desktop.describe(Window.class, new WindowDescription.Builder().title("SwingSet2").build());

    	// This AUT contains a dynamically loaded tool bar, which starts to load when the AUT is launched.

    	// Create a description for the Toolbar.
    	ToolBar toolbar = javaWin.describe(ToolBar.class, new ToolBarDescription.Builder().nativeClass("SwingSet2$ToggleButtonToolBar").build());

    	// There are 16 buttons in the toolbar. Use the WaitUntil method to wait until
    	// all 16 buttons are loaded.
    	// This ensures that any button we click is loaded. If not loaded, the test fails.
    	WaitUntilEvaluator<ToolBar> evaluator = new WaitUntilEvaluator<ToolBar>() {
    		@Override
    		public boolean evaluate(ToolBar testObject) throws GeneralLeanFtException {
    			return testObject.getButtons().size() == 16;
    		}
    	};

    	assertTrue(waitUntil(toolbar, evaluator));

    	// Note: If you build your testing project using Java 8, you can use
    	// the Lambda syntax as follows, instead of creating the evaluator:
    	// assertTrue(waitUntil(toolbar, tb->tb.getButtons().size() == 16));

    	// Clicking the JEditorPane button displays the Editor test object.
    	toolbar.getButton("JEditorPane").press();

    	// Create a description for the Editor test object.
    	Editor edit = javaWin.describe(Editor.class, new EditorDescription.Builder().nativeClass("javax.swing.JEditorPane").build());

    	// Verify that the editor in this AUT is read-only HTML with links.
    	Verify.isTrue(edit.isReadOnly(), "Verify Editor is Read Only", "Verify that the editor in this AUT is read only.");

    	// Click the link to king.html.
    	edit.clickLink("king.html");

    	// Verify that the correct page loaded by checking the text.
    	// The text is long, so check only the first segment of the text.
    	String expectedTextPrefix = "   \n \n \nDo here most humbly lay this small Present";
    	String text = edit.getText();
    	Verify.isTrue(text.startsWith(expectedTextPrefix), "Verify Correct Page Was Loaded", "Verify that the correct page was loaded by checking the page's first segment of text.");
    }

Back to top

See also:

- Java SDK
- Java SDK code samples

https://admhelp.microfocus.com Functional Testing for Developers 8942
Tips to improve your search results... OpenText™ Functional Testing for Developers Help Center OpenText™ Functional Testing for Developers Help 25.2 All View Local Help Center View Online Help Center Version: