Displaying Context Menus

Applies to TestComplete 14.70, last modified on April 22, 2021

While testing menu controls, you can use specific properties and methods of the corresponding program object to perform certain actions and obtain data stored in controls. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with the needed properties and methods from your scripts. However, when testing a control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

In order to perform operations over a context (popup) menu, it must be opened on screen first. This topic describes various approaches that can be used to invoke the context menu in scripts and explains how to verify that the context menu was actually displayed as the result of the simulated actions.

Displaying Context Menu for Windows and Controls

You can call the context menu for a window or control in the following ways:

  • By simulating a right-click over the window or control. For this purpose, you can use the ClickR action that is common to all onscreen objects, or similar actions of various objects that are used to work with controls -- ClickItemR, ClickCellR, ClickCellRXY and others (you can explore the control in the Object Browser to learn which actions it supports).

    The ClickR action has three parameters - ClientX, ClientY and Shift. The ClientX and ClientY parameters specify the coordinates of the point within the window or control that will be clicked. If they are omitted, the click will be performed in center of the window or control. The Shift parameter specifies the shift keys (Ctrl, Shift, Alt) or their combination that should be “pressed” during the click (see TShiftKey).

  • By simulating the Application key press using the Keys action. Note, that the application may provide additional context menus that can be called using special shortcuts, say, Alt+Down Arrow. To call this type of menu, you should simulate the appropriate shortcut.

Below are two examples that demonstrate how you can paste the text from the Clipboard at the end of the text in Notepad using the Paste context menu command (the Notepad must be running). They perform the same operations, but differ only in the way they invoke the context menu - the former example uses the ClickR action, whereas the latter simulates the Application key press.

Example 1

JavaScript, JScript

function Main ()
{
  var w = Sys.Process("notepad").Window("Notepad", "*");
  var w2 = w.Window("Edit");
  // Navigate to the end of text
  w2.Keys("^[End]");
  // Call the context menu
  w2.ClickR();
  // Select the "Paste" command
  w2.PopupMenu.Click("Paste");
}

Python

def Main():
  w = Sys.Process("notepad").Window("Notepad", "*")
  w2 = w.Window("Edit")
  # Navigate to the end of text
  w2.Keys("^[End]")
  # Call the context menu
  w2.ClickR()
  # Select the "Paste" command
  w2.PopupMenu.Click("Paste")

VBScript

Sub Main
  Dim w, w2
  Set w = Sys.Process("notepad").Window("Notepad", "*")
  Set w2 = w.Window("Edit")
  ' Navigate to the end of text
  w2.Keys("^[End]")
  ' Call the context menu
  w2.ClickR
  ' Select the "Paste" command
  w2.PopupMenu.Click("Paste")
End Sub

DelphiScript

procedure Main;
var w, w2 : OleVariant;
begin
  w := Sys.Process('notepad').Window('Notepad', '*');
  w2 := w.Window('Edit');
  // Navigate to the end of text
  w2.Keys('^[End]');
  // Call the context menu
  w2.ClickR;
  // Select the "Paste" command
  w2.PopupMenu.Click('Paste');
end;

C++Script, C#Script

function Main ()
{
  var w = Sys["Process"]("notepad")["Window"]("Notepad", "*");
  var w2 = w["Window"]("Edit");
  // Navigate to the end of text
  w2["Keys"]("^[End]");
  // Call the context menu
  w2["ClickR"]();
  // Select the "Paste" command
  w2["PopupMenu"]["Click"]("Paste");
}

Example 2

JavaScript, JScript

function Main ()
{
  var w = Sys.Process("notepad").Window("Notepad", "*");
  var w2 = w.Window("Edit");
  // Navigate to the end of text
  w2.Keys("^[End]");
  // Call the context menu
  w2.Keys("Apps");
  // Select the "Paste" command
  w2.PopupMenu.Click("Paste");
}

Python

def Main():
  w = Sys.Process("notepad").Window("Notepad", "*")
  w2 = w.Window("Edit")
  # Navigate to the end of text
  w2.Keys("^[End]")
  # Call the context menu
  w2.Keys("Apps")
  # Select the "Paste" command
  w2.PopupMenu.Click("Paste")

VBScript

Sub Main
  Dim w, w2
  Set w = Sys.Process("notepad").Window("Notepad", "*")
  Set w2 = w.Window("Edit")
  ' Navigate to the end of text
  w2.Keys("^[End]")
  ' Call the context menu
  w2.Keys("Apps")
  ' Select the "Paste" command
  w2.PopupMenu.Click("Paste")
End Sub

DelphiScript

procedure Main;
var w, w2 : OleVariant;
begin
  w := Sys.Process('notepad').Window('Notepad', '*');
  w2 := w.Window('Edit');
  // Navigate to the end of text
  w2.Keys('^[End]');
  // Call the context menu
  w2.Keys('Apps');
  // Select the "Paste" command
  w2.PopupMenu.Click('Paste');
end;

C++Script, C#Script

function Main ()
{
  var w = Sys["Process"]("notepad")["Window"]("Notepad", "*");
  var w2 = w["Window"]("Edit");
  // Navigate to the end of text
  w2["Keys"]("^[End]");
  // Call the context menu
  w2["Keys"]("Apps");
  // Select the "Paste" command
  w2["PopupMenu"]["Click"]("Paste");
}

Displaying Context Menu at the Current Point

In certain cases, you may need to invoke a context menu at the current point where the mouse pointer is situated. To accomplish this task, you should write a script that will perform the following:

  • Determine the screen-relative coordinates of the mouse pointer using the Sys.Desktop.MouseX and Sys.Desktop.MouseY properties.
  • If you do not know the object that is currently under the mouse pointer, obtain it using the Sys.ObjectFromPoint method.
  • Call the ScreenToWindow method of the obtained onscreen object to convert the screen-relative coordinates of the mouse pointer to the object-relative coordinates.
  • Call ClickR and specify the calculated coordinates in the ClientX and ClientY parameters.

The code snippet below demonstrates how to do this:

JavaScript, JScript

function Test()
{
  var w, p, x, y;

  // Get the mouse pointer coordinates
  x = Sys.Desktop.MouseX;
  y = Sys.Desktop.MouseY;

  // Get the object under the mouse pointer
  w = Sys.ObjectFromPoint(x, y);

  // Convert screen-relative coordinates to object-relative
  p = w.ScreenToWindow(x, y);

  w.ClickR(p.X, p.Y);
}

Python

def Test():

  # Get the mouse pointer coordinates
  x = Sys.Desktop.MouseX
  y = Sys.Desktop.MouseY

  # Get the object under the mouse pointer
  w = Sys.ObjectFromPoint(x, y)

  # Convert screen-relative coordinates to object-relative
  p = w.ScreenToWindow(x, y)

  w.ClickR(p.X, p.Y)

VBScript

Sub Test
  Dim w, p, x, y

  ' Get the mouse pointer coordinates
  x = Sys.Desktop.MouseX
  y = Sys.Desktop.MouseY

  ' Get the object under the mouse pointer
  Set w = Sys.ObjectFromPoint(x, y)

  ' Convert screen-relative coordinates to object-relative
  Set p = w.ScreenToWindow(x, y)

  Call w.ClickR(p.X, p.Y)
End Sub

DelphiScript

procedure Test;
var w, p, x, y : OleVariant;
begin
  // Get the mouse pointer coordinates
  x := Sys.Desktop.MouseX;
  y := Sys.Desktop.MouseY;

  // Get the object under the mouse pointer
  w := Sys.ObjectFromPoint(x, y);

  // Convert screen-relative coordinates to object-relative
  p := w.ScreenToWindow(x, y);

  w.ClickR(p.X, p.Y);
end;

C++Script, C#Script

function Test()
{
  var w, p, x, y;

  // Get the mouse pointer coordinates
  x = Sys["Desktop"]["MouseX"];
  y = Sys["Desktop"]["MouseY"];

  // Get the object under the mouse pointer
  w = Sys["ObjectFromPoint"](x, y);

  // Convert screen-relative coordinates to object-relative
  p = w["ScreenToWindow"](x, y);

  w["ClickR"](p.X, p.Y);
}

Verifying that the Context Menu is Displayed

You can only perform operations over a context menu if it is displayed on screen. To verify that the context menu for the window or control is displayed, check the PopupMenu property value - if it is different from null (null in JavaScript, JScript, C#Script and C++Script, None in Python, Nothing in VBScript, nil in DelphiScript), then the menu is displayed; otherwise it has not been invoked. Depending on the check result, you can skip the part of your test that works with the context menu and log an error instead:

JavaScript

function Test()
{
  let w = Sys.Process("MyApplication").Window("MyWindow", "*");
  let w2 = w.Window("Edit");

  w2.ClickR();
  if (!strictEqual(w2.PopupMenu, null))
    // Perform the needed operations over the menu
    ...
  else
    Log.Error("The context menu was not invoked.");
}

JScript

function Test()
{
  var w = Sys.Process("MyApplication").Window("MyWindow", "*");
  var w2 = w.Window("Edit");

  w2.ClickR();
  if (w2.PopupMenu != null)
    // Perform the needed operations over the menu
    ...
  else
    Log.Error("The context menu was not invoked.");
}

Python

def Test():
  w = Sys.Process("MyApplication").Window("MyWindow", "*")
  w2 = w.Window("Edit")
  
  w2.ClickR()
  if (w2.PopupMenu != None):
    # Perform the needed operations over the menu
    ...
  else:
    Log.Error("The context menu was not invoked.")

VBScript

Sub Test
  Set w = Sys.Process("MyApplication").Window("MyWindow", "*")
  Set w2 = w.Window("Edit")

  w2.ClickR
  If Not (w2.PopupMenu Is Nothing) Then
    ' Perform the needed operations over the menu
    ...
  Else
    Log.Error("The context menu was not invoked.")
  End If
End Sub

DelphiScript

procedure Test;
var w, w2 : OleVariant;
begin
  w := Sys.Process('MyApplication').Window('MyWindow', '*');
  w2 := w.Window('Edit');

  w2.ClickR;
  if (w2.PopupMenu <> nil) then
    // Perform the needed operations over the menu
    ...
  else
    Log.Error('The context menu was not invoked.');
end;

C++Script, C#Script

function Test()
{
  var w = Sys["Process"]("MyApplication")["Window"]("MyWindow", "*");
  var w2 = w["Window"]("Edit");

  w2["ClickR"]();
  if (w2["PopupMenu"] != null)
    // Perform the needed operations over the menu
    ...
  else
    Log["Error"]("The context menu was not invoked.");
}

See Also

Working With Menus

Highlight search results