Standard Windows code samples

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:

  1. Launch Notepad.
  2. Open the Format > Font menu item.
  3. Select Arial from the Font combo-box.
  4. Use the Assert statement to verify that Arial is the selected font in the combo-box.
@Test
public void testComboBox() throws GeneralLeanFtException, IOException, InterruptedException {        			
 // Launch Notepad.
 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 a 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. (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 "Arial" in the combo box.			
 fontsComboBox.select("Arial");
			
 // Get the selected combo box item
 String selectedFont = fontsComboBox.getSelectedItem();
			
 // As the Assert method is not enclosed in a try-catch statement, the application will throw a runtime error if the Assert returns false. 

 // Verify the selected combo box item is "Arial".			
 assertEquals("Arial", selectedFont); 
			
 // 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. (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); 			
}		

Back to top

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:

  1. Launch calculator.
  2. Locate button numbers 2 and 4 using their WindowId.
  3. Locate button number 1 using VRI, specifying the location of button number 1 in relation to buttons number 2 and 4.
  4. Click button number 1.
  5. 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 on the computer. 
 Thread.sleep(4 * 1000);      
				
 // Use try-catch to add a warning to the run report if the assert validation fails. 
 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 buttons number 2 and 4 in 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());
				
  // Use VRI to locate button number 1 in the calculator based on the location of buttons number 2 and 4. 
  Button button1 = calculator.describe(Button.class, new ButtonDescription.Builder().nativeClass("Button").				
   vri(				
    // Button number 2 is to the right of button number 1.
    new VisualRelation().setTestObject(button2).setHorizontalRelation(HorizontalVisualRelation.RIGHT),				
    // Button number 4 is above the button number 1. 
    new VisualRelation().setTestObject(button4).setVerticalRelation(VerticalVisualRelation.ABOVE)).build());           
				
  // Click button number 1. 				
  button1.click();
            				
  String visibleText = textBox.getVisibleText();
            				
  // Enclosing the Assert method in a try-catch statement ensures the application does not throw a runtime error if the Assert returns false.

 
  // Ensure that the number "1" appears on the screen. 				
  assertEquals("1", visibleText);				
 } finally {				
 // Exit and close the Calculator.				
  calc.destroy();				
 }				
}

Back to top

See also: