Standard Windows code samples

End-to-End: Select an item in a combo box

The following example uses standard Windows objects, such as, IWindow, IMenu and IComboBox and their respective properties to select a font type in a combo box.

[Test]
public void TestComboBox()
{
    
	// Following is the program flow: // 1. Launch notepad.// 2. Open the Format > Font menu item.// 3. Select "Arial" from the Font combobox.// 4. Use the Assert statement to verify that "Arial" is the selected font in the combobox.
    
	Process appProcess = new Process { StartInfo = { FileName = @"C:\Windows\System32\notepad.exe" } };
	appProcess.Start();
    
	// Pause to ensure Notepad has fully opened on the computer. 
	Thread.Sleep(4 * 1000); 

	// Locate the Notepad window and assign it to an IWindow object.
	IWindow notepadWindow = Desktop.Describe<IWindow>(new WindowDescription
	{
		WindowClassRegExp = "Notepad",
		WindowTitleRegExp = " Notepad"
	});

	// Locate the Notepad menu and assign it to an IMenu object.
	IMenu notepadMenu = notepadWindow.Describe<IMenu>(new MenuDescription(MenuType.Menu));

	// Build the path for the Font menu item. (The second item in the Format menu in Notepad)var path = notepadMenu.BuildMenuPath("Format", 2);

	// Use the path to retrieve the actual Font menu item object. var 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 an IDialog object.
	IDialog notepadFontDialog = notepadWindow.Describe<IDialog>(new DialogDescription
	{
		WindowTitleRegExp = "Font"
	});

	// Locate the Font combobox in the Font dialog box and assign it to an IComboBox object. 
	IComboBox fontsComboBox = notepadFontDialog.Describe<IComboBox>(new ComboBoxDescription
	{
		AttachedText = @"&Font:",
		NativeClass = @"ComboBox"
	});
    
	// Select "Arial" font in the combobox
	fontsComboBox.Select("Arial");

	// Get the selected combobox itemvar selectedFont = fontsComboBox.SelectedItem;

	// Verify the selected combobox item is "Arial"
	Assert.AreEqual("Arial", selectedFont); 

	// Locate the Cancel button in the dialog box and assign it to an IButton object.
	IButton cancelButton = notepadFontDialog.Describe<IButton>(new ButtonDescription
	{
		Text = @"Cancel",
		NativeClass = @"Button"
	});

	// Clicks "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);

	// Exits and closes Notepad. 
	notepadMenu.Select(menuItem); 
}

Back to top

End-to-End: Locate a button using Visual Relation Identification (VRI)

The following example uses Visual Relation Identification (VRI) to locate and click buttons in the standard Windows calculator.

[Test]
public void TestCalcButtonsUsingVri()
{
   
	/* Following is the program flow:        1. Launch calculator.       2. Locate buttons number 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 "1" is printed in the calculator's static text control.    */

	Process appProcess = new Process { StartInfo = { FileName = @"C:\Windows\System32\calc.exe" } };
	appProcess.Start();
    
	//Pause to ensure Calculator has fully opened on the computer. 
	Thread.Sleep(4 * 1000);

	//Create the WindowDescription object for the IWindowvar calculatorDescription = new WindowDescription
	{
	WindowClassRegExp = "CalcFrame",
	WindowTitleRegExp = "Calculator"
	};
    
	// Locate the Calculator window and assign it to an IWindow object.
	IWindow calculator = Desktop.Describe<IWindow>(calculatorDescription);

	// Locate buttons number 2 and 4 in the calculator.var button2 =
		calculator.Describe<IButton>(new ButtonDescription { WindowId = 132, NativeClass = "Button" });
	var button4 =
		calculator.Describe<IButton>(new ButtonDescription { WindowId = 134, NativeClass = "Button" });

	// Locate button number 1 in the calculator based on the location of buttons number 2 and 4. var button1 = calculator.Describe<IButton>(
		new ButtonDescription
		{
			NativeClass = "Button",
			Vri = 
				{
				new VisualRelation
				{
					// Button number 2 is to the right of button number 1.
					TestObject = button2,
					HorizontalRelation = HorizontalVisualRelation.Right,
				},
				new VisualRelation
				{
					// Button number 4 is above the button number 1. 
					TestObject = button4,
					VerticalRelation = VerticalVisualRelation.Above,
				}
			}
		});

	// Click button number 1. 
	button1.Click();

	// Locate the calculator's output screen. var textBox = calculator.Describe<IStatic>(
		new StaticDescription { WindowId = 150, NativeClass = "Static" });

	var visibleText = textBox.GetVisibleText();
    
	// Ensure that number "1" appears on the calculator's output screen. 
	Assert.AreEqual("1", visibleText);

	// Exit and close the calculator.
	appProcess.Kill();
}

Back to top