Simulating Mouse Actions

Applies to TestComplete 15.62, last modified on March 19, 2024

In your tests, you can simulate mouse actions and mouse wheel events. This topic provides detailed information on how to implement mouse event simulation in your tests. For information on how to simulate mouse wheel rotation, see Simulating Mouse Wheel Rotation.

About

To simulate clicks, double-clicks, dragging and hovering (hot-tracking) operations, each onscreen object provides the following methods:

  • Click, ClickR, ClickM - Simulates a single click performed with the left, right or middle mouse button.

  • DblClick, DblClickR, DblClickM - Simulates a double-click performed with the left, right or middle mouse button.

  • Drag, DragR, DragM - Simulates a dragging operation performed with the left, right or middle mouse button.

  • HoverMouse - Simulates moving the mouse pointer to the specified position within an object.

Each of these actions have parameters that specify window-related coordinates of the event. The click, double-click and drag actions have an additional parameter that specifies the keys or a combination of keys (Ctrl, Alt, Shift) that are pressed during simulating an event.

You can also simulate mouse clicks by using the MouseDown and MouseUp methods of the Desktop object or the MouseDown and MouseUp methods of the LLPlayer object. You can use these methods to emulate "hold and click" actions, as well as to simulate mouse clicks at any position on screen. Besides the described methods, different program objects, that are used to work with custom controls and menus have their specific actions and methods to simulate user actions on them. For complete information, see Working With Application Objects and Controls.

Simulating in keyword tests

You can simulate a mouse event from a keyword test by using the On-Screen Action operation with specified parameters. You can create a sequence of simulated events by adding the desired number of the On-Screen Action operations to the test.

For example, to click the point (10, 10) with the left mouse button on the Start button of the Windows system tray:

  1. Open your keyword test for editing.

  2. Add the On-Screen Action operation to the test. After the operation is dragged, TestComplete will display the Operation Parameters wizard.

  3. Click Pick Object, and when the Pick Object dialog appears, drag the target glyph () to the desired object. In our case, it is the Start button. Typically, it is located at the left bottom corner of the desktop.

  4. Click Next.

  5. In the next page of the wizard, select the method from the list. In our case, it will be the Click method. Click Next.

  6. On the next page of the wizard, specify parameters of the selected method:

    • ClientX

      • Select Constant from the Mode drop-down list.

      • Specify Integer in the Type cell.

      • Enter the x-coordinate of the point, where a click will be simulated, in the Value column. This coordinate is relative to the object. In our case, the x-coordinate is equal to 10. Press ENTER to confirm input in the cell.

    • Repeat the steps above to set the ClientY parameter to 20.

    • Shift

      • Choose the Code Expression from the Mode drop-down list.

      • Specify Code Expression in the Type cell.

      • Enter the constant, that specifies whether the SHIFT, CTRL or ALT key is pressed during the click. For instance, if you enter the skAlt constant, the ALT key will be pressed during the click. For information about possible values, see TShiftKey. In our case, this parameter is equal to skNoShift. Press ENTER to confirm input in the cell.

  7. Click Finish to save the changes and to close the wizard. TestComplete will append the operation to the test.

Now the mouse event will be simulated when you run your keyword test. In our example, a click of the right mouse button will be simulated at point (10, 20) of the Start button and Shift will not be pressed during the click.

You can also use the Call Object Method operation to simulate mouse events. For more information on how to perform typical tasks in keyword tests, see Common Tasks for Keyword Test Operations.

Simulating in scripts

You can simulate mouse events from a script routine. To do this, call the appropriate methods from your script.

Click at a point

The following sample code shows how to simulate a click on the point (10, 20) on the Start button of the Windows system tray:

JavaScript, JScript

var btnStart = Sys.Process("explorer").Window("Shell_TrayWnd").Window("Start", "Start");
btnStart.Click(10,20);

Python

btnStart = Sys.Process("explorer").Window("Shell_TrayWnd").Window("Start", "Start")
btnStart.Click(10,20)

VBScript

Set btnStart = Sys.Process("explorer").Window("Shell_TrayWnd").Window("Start", "Start")
Call btnStart.Click(10,20)

DelphiScript

var btnStart;
begin

  btnStart := Sys.Process('explorer').Window('Shell_TrayWnd').Window('Start', 'Start');
  btnStart.Click(10,20);

end;

C++Script, C#Script

var btnStart = Sys["Process"]("explorer")["Window"]("Shell_TrayWnd")["Window"]("Start", "Start");
btnStart["Click"](10,20);

Hold and Click

The following sample code shows how to simulate clicking the left mouse button while holding down the Ctrl key:

JavaScript, JScript

function HoldAndClickExample()
{
  //Launches Notepad
  WshShell.Run("notepad.exe", SW_NORMAL);
  
  //Presses CTRL in the Notepad main window
  LLPlayer.KeyDown(VK_CONTROL, 2000);
  
  // Specifies the coordinates of the click
  var coordX = 15;
  var coordY = 120;
  
  // Specifies a delay in milliseconds
  var sDelay = 2000; // 2 seconds
  
  // Simulates pressing the left mouse button
  LLPlayer.MouseDown(MK_LBUTTON, coordX, coordY, sDelay);
  // ...
  // Simulates releasing the left mouse button
  LLPlayer.MouseUp(MK_LBUTTON, coordX, coordY, sDelay);
  
  //Releases CTRL
  LLPlayer.KeyUp(VK_CONTROL, 2000);
}

Python

def HoldAndClickExample():
  # Launches Notepad
  WshShell.Run("notepad.exe", SW_NORMAL)
  # Presses CTRL in the Notepad main window
  LLPlayer.KeyDown(VK_CONTROL, 2000)
  # Specifies the coordinates of the click
  coorX = 15
  coorY = 120
  # Specifies a delay in milliseconds
  sDelay = 2000 # 2 seconds
  # Simulates pressing the left mouse button
  LLPlayer.MouseDown(MK_LBUTTON, coorX, coorY, sDelay)
  # Simulates releasing the left mouse button
  LLPlayer.MouseUp(MK_LBUTTON, coorX, coorY, sDelay)
  # Releases CTRL
  LLPlayer.KeyUp(VK_CONTROL, 2000)

VBScript

Sub HoldAndClickExample()

  ' Launches Notepad
  Call WshShell.Run("notepad.exe", SW_NORMAL)
  
  ' Presses CTRL in the Notepad main window
  Call LLPlayer.KeyDown(VK_CONTROL, 2000)
  
  ' Specifies the coordinates of the click
  coordX = 15
  coordY = 120
  
  ' Specifies a delay in milliseconds
  sDelay = 2000 ' 2 seconds
  
  ' Simulates pressing the left mouse button
  Call LLPlayer.MouseDown(MK_LBUTTON, coordX, coordY, sDelay)
  ' ...
  ' Simulates releasing the left mouse button
  Call LLPlayer.MouseUp(MK_LBUTTON, coordX, coordY, sDelay)
  
  ' Releases CTRL
  Call LLPlayer.KeyUp(VK_CONTROL, 2000)
  
End Sub

DelphiScript

function HoldAndClickExample;
begin
  
  // Launches Notepad
  WshShell.Run('notepad.exe', SW_NORMAL);
  
  //Presses CTRL in the Notepad main window
  LLPlayer.KeyDown(VK_CONTROL, 2000);
  
  // Specifies the coordinates of the click
  var coordX = 15;
  var coordY = 120;
  
  // Specifies a delay in milliseconds
  var sDelay = 2000; // 2 seconds
  
  // Simulates pressing the left mouse button
  LLPlayer.MouseDown(MK_LBUTTON, coordX, coordY, sDelay);
  // ...
  // Simulates releasing the left mouse button
  LLPlayer.MouseUp(MK_LBUTTON, coordX, coordY, sDelay);
  
  //Releases CTRL
  LLPlayer.KeyUp(VK_CONTROL, 2000);
  
end;

C++Script, C#Script

function HoldAndClickExample()
{
  // Launches Notepad
  WshShell["Run"]("notepad.exe", SW_NORMAL);
  
  //Presses CTRL in the Notepad main window
  LLPlayer["KeyDown"](VK_CONTROL, 2000);
  
  // Specifies the coordinates of the click
  var coordX = 15;
  var coordY = 120;
  
  // Specifies a delay in milliseconds
  var sDelay = 2000; // 2 seconds
  
  // Simulates pressing the left mouse button
  LLPlayer["MouseDown"](MK_LBUTTON, coordX, coordY, sDelay);
  // ...
  // Simulates releasing the left mouse button
  LLPlayer["MouseUp"](MK_LBUTTON, coordX, coordY, sDelay);
  
  //Releases CTRL
  LLPlayer["KeyUp"](VK_CONTROL, 2000);
}

Simulating from low-level procedures

To simulate mouse events from a low-level procedure, record the desired sequence of user actions — moving the mouse, clicking —  as a low-level procedure. See Creating and Recording Low-Level Procedures. After that you can run this low-level procedure to simulate the recorded actions. For more information, see Running Low-Level Procedures.

Possible issues

Before simulating a click on a control, TestComplete performs a number of verifications. It finds the target control, determines the point of the click in it, and then checks whether this point is visible on the screen and whether it is overlapped by some other control or window. It is possible that, during the check, the point or entire control becomes overlapped with another control or window, or changes its position (if the control moves). This typically happens with applications and web pages that have animated elements.

To avoid the problem, you have to use test commands that will pause the test execution until the animation is over. The easiest way to achieve this is to use test commands that delay test execution for some time. A more sophisticated approach is to use the Wait methods that pause the test execution until the object state changes. For instance, you can determine properties, whose values change during the animation (this can be VisibleOnScreen), and use the WaitProperty method to pause the test run until the property gets the needed value. See Waiting for an Object to Have a Specific Property Value and Waiting for Object State Changes.

See Also

Simulating User Actions
Simulating Mouse Wheel Rotation
Creating and Recording Low-Level Procedures

Highlight search results