Caption Property (MenuItem Objects)

Applies to TestComplete 15.63, last modified on April 10, 2024

Description

The MenuItem.Caption property returns the menu item caption.

Declaration

TestObj.Caption

Read-Only Property String
TestObj A variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section

Applies To

The property is applied to the following object:

View Mode

To view this property in the Object Browser panel and in other panels and dialogs, activate the Advanced view mode.

Property Value

The menu item caption. If the specified menu item has no caption, the property returns an empty string.

Remarks

The item caption specified by the property does not include the special character that is used to create an access key for that item. To specify the access key, developers typically insert the ampersand symbol before the desired character in the item name. So, the ampersand is one of these special characters. Another special character is an underscore. It is used to specify access keys for WPF menu items. So, if you are working with WPF menus, you should specify item captions that do not include these “special” underscores. Typically, only the first found ampersand or underscore symbol is treated as a special character. All other ampersands and underscores are treated as ordinary characters.

Example

The following code snippet obtains the main menu of Windows Notepad, iterates through its items and posts their captions to the test log.

JavaScript, JScript

function MenuItemSample()
{
  var Notepad, Window, MainMenu, Count;
  // Obtains the Windows Notepad main menu
  Notepad = Sys.Process("Notepad");
  Window = Notepad.Window("Notepad", "*");
  MainMenu = Window.MainMenu;

  // Iterates through the menu items
  // And posts their captions to the test log
  Count = MainMenu.Count;
  for (i = 0; i < Count; i++)
  {
    Item = MainMenu.Items(i);
    Log.Message(Item.Caption);
  }

}

Python

def MenuItemSample():
  # Obtains the Windows Notepad main menu
  Notepad = Sys.Process("Notepad")
  Window = Notepad.Window("Notepad", "*")
  MainMenu = Window.MainMenu

  # Iterates through the menu items
  # And posts their captions to the test log
  Count = MainMenu.Count
  for i in range ( 0, Count):
    Item = MainMenu.Items[i]
    Log.Message(Item.Caption)

VBScript

Sub MenuItemSample

  ' Obtains the Windows Notepad main menu
  Set Notepad = Sys.Process("Notepad")
  Set Window = Notepad.Window("Notepad", "*")
  Set MainMenu = Window.MainMenu

  ' Iterates through the menu items
  ' And posts their captions to the test log
  Count = MainMenu.Count
  For i = 0 To Count - 1
    Set Item = MainMenu.Items(i)
    Log.Message Item.Caption
  Next

End Sub

DelphiScript

procedure MenuItemSample();
var
  Notepad, Window, MainMenu, Item : OleVariant;
  Count, i : integer;
begin
  // Obtains the Windows Notepad main menu
  Notepad := Sys.Process('Notepad');
  Window := Notepad.Window('Notepad', '*');
  MainMenu := Window.MainMenu;

  // Iterates through the menu items
  // And posts their captions to the test log
  Count := MainMenu.Count;
  for i := 0 to Count - 1 do
  begin
    Item := MainMenu.Items[i];
    Log.Message(Item.Caption);
  end;

end;

C++Script, C#Script

function MenuItemSample()
{
  var Notepad, Window, MainMenu, Count;
  // Obtains the Windows Notepad main menu
  Notepad = Sys["Process"]("Notepad");
  Window = Notepad["Window"]("Notepad", "*");
  MainMenu = Window["MainMenu"];

  // Iterates through the menu items
  // And posts their captions to the test log
  Count = MainMenu["Count"];
  for (i = 0; i < Count; i++)
  {
    Item = MainMenu["Items"](i);
    Log["Message"](Item["Caption"]);
  }

}

See Also

Addressing Menu Items in Desktop Windows Applications
Id Property (MenuItem Objects)
Position Property (MenuItem Objects)

Highlight search results