Java code samples

Select Menu item that changes background color of AUT's main DesktopPane

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

var LFT = require("leanft");
var expect = require("leanft/expect");
var spawn = require('child_process').spawn;
var Java = LFT.Java;
var Desktop = LFT.Desktop;

jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000;

describe("Java examples", function () {
	beforeAll(function(done){
		LFT.init();
		LFT.whenDone(done);
	});

	beforeEach(function(done){
		LFT.beforeTest();

		LFT.whenDone(done);
	});

	// This example shows how to select a menu item that changes the background color of the main DesktopPane of the AUT.
	it("Java Menu Test", function (done) {
		// Launch the AUT, an executable JAR.
		spawn('java', ['-jar', "C:/SwingSet2.jar"]);

		// Create a description for the main AUT window.
		// Set the title for the Description object.

		var javaWin = Desktop.$(Java.Window({title:"SwingSet2"}));

		// Describe the DesktopPane of the AUT as a UiObject.
		// Set the nativeClass value for the Description object.
		var desktop = javaWin.$(Java.UiObject({nativeClass:"javax.swing.JDesktopPane"}));

		// Verify that the item is not selected. (When selected, the background color is Aqua, 0x80c0c0 in hexadecimal.)
		expect(desktop.backgroundColor()).not.toEqual("80c0c0");

		// Create a description for the top-level menu item: Themes
		var themesMenu = javaWin.$(Java.Menu({label:"Themes"}));

		// 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.
		themesMenu.subMenus().then(function (subMenus) {
			expect(subMenus.length).toEqual(9);
		});

		// *** 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 method 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.)
		themesMenu.subMenus().then(function (subMenus) {
			var aquaSubMenu = subMenus[4];
			aquaSubMenu.select();
		});

		// Check that the item is selected by verifying that the background color is Aqua.
		expect(desktop.backgroundColor()).toEqual("80c0c0");

		LFT.whenDone(done);
	});


	afterEach(function(done){
		LFT.afterTest();
		LFT.whenDone(done);
	});

	afterAll(function(done){
		LFT.cleanup();
		LFT.whenDone(done);
	});
});

See also: