Standard Windows code samples (Java SDK)
Select an item in a combo box
This example uses standard Windows test objects, such as, Window, Menu and ComboBox and their respective properties, to select a font type in a combo box.
The program flow is as follows:
- Launch Notepad.
- Open the Format > Font menu item.
- Select Arial from the Font combo-box.
- Use the Verify statement to verify that Arial is the selected font in the combo-box.
@Test public void testComboBox() throws GeneralLeanFtException, IOException, InterruptedException { // Launch the Notepad application. new ProcessBuilder("C:\\Windows\\System32\\notepad.exe").start(); // Pause to ensure Notepad has fully opened on the computer. Thread.sleep(4 * 1000); // Locate the Notepad window and assign it to an Window object. Window notepadWindow = Desktop.describe(Window.class, new WindowDescription.Builder().windowClassRegExp("Notepad").windowTitleRegExp(" Notepad").build()); // Locate the Notepad menu and assign it to a Menu object. Menu notepadMenu = notepadWindow.describe(Menu.class, new MenuDescription(MenuType.MENU)); // Build the path for the Font menu item, which is the second item in the Format menu in Notepad. String path = notepadMenu.buildMenuPath("Format", 2); // Use the path to retrieve the actual Font menu item object. MenuItem menuItem = notepadMenu.getItem(path); // Open the Font dialog using the font menu item. notepadMenu.select(menuItem); // Locate the Font dialog box and assign it to a Dialog object. Dialog notepadFontDialog = notepadWindow.describe(Dialog.class, new DialogDescription.Builder().windowTitleRegExp("Font").build()); // Locate the Font combo box in the Font dialog box and assign it to a ComboBox object. ComboBox fontsComboBox = notepadFontDialog.describe(ComboBox.class, new ComboBoxDescription.Builder().attachedText("&Font:").nativeClass("ComboBox").build()); // Select the "Arial" font in the combo box. fontsComboBox.select("Arial"); // Retrieve the selected combo box item String selectedFont = fontsComboBox.getSelectedItem(); // Verify the selected combo box item is "Arial" Verify.areEqual("Arial", selectedFont, "TestComboBox-Verify-Font", "Verify the selected combobox item is 'Arial'."); // Locate the Cancel button in the dialog box and assign it to a Button object. Button cancelButton = notepadFontDialog.describe(Button.class, new ButtonDescription.Builder().text("Cancel").nativeClass("Button").build()); // Click "Cancel" in the dialog box. cancelButton.click(); // Build the path for the Exit menu item, which is the seventh item in the File menu in Notepad. path = notepadMenu.buildMenuPath("File", 7); menuItem = notepadMenu.getItem(path); // Exit and close Notepad. notepadMenu.select(menuItem); }
Identify a Button object with Visual Relations Identifiers (VRI)
This example uses Visual Relation Identification (VRI) to locate and click a button in the standard Windows calculator.
The program flow is as follows:
- Launch calculator.
- Locate button numbers 2 and 4 using their WindowId.
- Locate button number 1 using VRI, specifying the location of button number 1 in relation to buttons number 2 and 4.
- Click button number 1.
- Verify that 1 is printed in the calculator's static text control.
@Test public void testCalcButtonsUsingVri() throws GeneralLeanFtException, IOException, InterruptedException { //Launch the Calculator application. Process calc = new ProcessBuilder("C:\\Windows\\System32\\calc.exe").start(); //Pause to ensure Calculator has fully opened. Thread.sleep(4 * 1000); try { //Create the WindowDescription object for the Calculator application window. WindowDescription calculatorDescription = new WindowDescription.Builder().nativeClass("CalcFrame").build(); // Locate the Calculator window and assign it to an Window object. Window calculator = Desktop.describe(Window.class, calculatorDescription); // Identify the Calculator window static text. Static textBox = calculator.describe(Static.class, new StaticDescription.Builder().windowId(150).nativeClass("Static").build()); // Locate the number 2 and number 4 buttons on the Calculator. Button button4 = calculator.describe(Button.class, new ButtonDescription.Builder().windowId(134).nativeClass("Button").build()); Button button2 = calculator.describe(Button.class, new ButtonDescription.Builder().windowId(132).nativeClass("Button").build()); // Locate the number 1 button on the Calculator based on the location of the number 2 and 4 buttons. Button button1 = calculator.describe(Button.class, new ButtonDescription.Builder().nativeClass("Button"). vri( // The number 2 button is to the right of the number 1 button. new VisualRelation().setTestObject(button2).setHorizontalRelation(HorizontalVisualRelation.RIGHT), // The number 4 button is above the number 1 button. new VisualRelation().setTestObject(button4).setVerticalRelation(VerticalVisualRelation.ABOVE)).build()); // Click the number 1 button. button1.click(); // The number 1 should appear on the Calculator's output screen. String visibleText = textBox.getVisibleText(); // Ensure that the number 1 appears on the screen. Verify.areEqual("1", visibleText, "TestCalcButtonsUsingVri", "Verify that the number '1' appears on the calculator's output screen."); } finally { // Exit and close the Calculator. calc.destroy(); } }
See also: