Java code samples

Select menu items in a list

This example demonstrates how to use the Menu test object, which enables several methods to access and select menu items.

This example selects a menu item, which changes the background color of the main DesktopPane of the AUT.

[Test]
						
public void JavaMenuTest()
{
	// Launch the AUT, an executable JAR.
	var appProcess = new Process { StartInfo = { FileName = @"C:\SwingSet2.jar" } };
	appProcess.Start();

	// Create a Description for the main AUT window. Set the title for the Description object. 
	var window = Desktop.Describe<IWindow>(new WindowDescription
	{
		Title = "SwingSet2"
	});

	// Create a description for the AUT DesktopPane as an IUiObject. 
	// Set the NativeClass value for the Description object. 
	var desktopPane = window.Describe<IUiObject>(new UiObjectDescription
	{
		NativeClass = "javax.swing.JDesktopPane"
	});

	// Check the item is not selected. If selected, the background color is Aqua, 0x80c0c0.
	Assert.AreNotEqual("80c0c0", desktopPane.BackgroundColor);

	// Create a description for the top level menu item: Themes.
	var themesMenu = window.Describe<IMenu>(new MenuDescription
	{
		Label = "Themes"
	});

	// Check that the  themesMenu has the expected six submenus.
	// A submenu is a collection of menu items located directly under the parent menu. The check is not recursive.
	Assert.AreEqual(9, themesMenu.SubMenus.Count);

	// *** Different ways to select a sub menu ***
    
	// 1) The SelectSubMenu method accepts the path of the submenu. 
	//    The elements of a multi-level path are separated with a semicolon (;).
	themesMenu.SelectSubMenu("Aqua");
    
	// 2) The GetSubMenu method returns a menu test object representing the submenu item. The item is then selected.
	themesMenu.GetSubMenu("Aqua").Select();

	// 3) The SubMenus property returns a menu test object representing the submenu item. The item is then selected. 
	//     This applies only when selecting a direct child of the parent menu.
	var aquaSubMenu = themesMenu.SubMenus[4];
	Assert.AreEqual("Aqua", aquaSubMenu.Label); // to verify we have taken the correct menu from the SubMenus collection.
	aquaSubMenu.Select();

	// Check that the item is selected. If selected, the background color is Aqua.
	Assert.AreEqual("80c0c0", desktopPane.BackgroundColor);
	}
				

Back to top

Wait until a state is reached and click a link

This demonstrates how to use the:

  • WaitUntil method to wait for the AUT to reach a certain state
  • ClickLink method of the Editor test object
[Test]
						
public void EditorFunctionality_BasicTesting()
{
	// Launch the AUT, an executable JAR.
	var appProcess = new Process {StartInfo = {FileName = @"C:\\SwingSet2.jar"}};
	appProcess.Start();

	// Create a description object for main window of the AUT.
	var window = Desktop.Describe<IWindow>(new WindowDescription
	{
		Title = "SwingSet2"
	});

	// This AUT contains a dynamically loaded toolbar that starts to load when the AUT is launched.

	// Create a description for the Toolbar.
	var toolBar = window.Describe<IToolBar>(new ToolBarDescription
	{
		NativeClass = "SwingSet2$ToggleButtonToolBar"
	});

	// 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 press was loaded. If not loaded, the test fails.
	toolBar.WaitUntil(tb => (tb.Buttons.Count == 16));

	// Clicking the JEditorPane button displays the editor test object.
	toolBar.GetButton("JEditorPane").Press();

	// Create a description for the Editor test object.
	var editor = window.Describe<IEditor>(new EditorDescription
	{
		NativeClass = "javax.swing.JEditorPane"
	});

	// The editor in this AUT is a read only HTML text with links.
	Assert.True(editor.IsReadOnly);

	// Click the link to king.html.
	editor.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.
	const string expectedTextPrefix = "   \n \n \nDo here most humbly lay this small Present";
	var editorText = editor.Text;
	Assert.True(editorText.StartsWith(expectedTextPrefix));
}

Back to top