Simulating Mouse and Mouse Wheel Actions

Applies to TestLeft 4.91, last modified on July 1, 2021

To simulate clicks, double-clicks, dragging and hovering (hot-tracking) operations, use the following methods –

Methods of the IControl test interface:

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

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

  • Drag, DragR, DragM - Simulate 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.

  • MouseWheel - Simulate mouse wheel rotations.

Methods of the Control class:

  • click, clickR, clickM - Simulate a single click performed with the left, right or middle mouse button.

  • dblClick, dblClickR, dblClickM - Simulate a double-click performed with the left, right or middle mouse button.

  • drag, dragR, dragM - Simulate 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.

  • mouseWheel - Simulate mouse wheel rotations.

These methods have parameters that specify window-related coordinates where the actions are to be simulated. The click and drag methods have an additional shift parameter that specifies a key or a key combination (Ctrl, Alt, Shift) pressed during the simulation.

The shift parameter is an enumerated type that defines the following constants for shift keys:

Constant Description
ShiftKey.Shift The Shift key is pressed.
ShiftKey.Alt The Alt key is pressed.
ShiftKey.Ctrl The Ctrl key is pressed.
ShiftKey.NoShift None of the keys (Ctrl, Shift or Alt) is pressed.

These values are additive. To specify that several keys are pressed at once, combine the needed constants using the bitwise logical OR operator.

Example

The following code demonstrates how to simulate a click within Notepad’s main window at point (30, 40) (Notepad must be running):

C#

using SmartBear.TestLeft;
using SmartBear.TestLeft.TestObjects;


public void Test()
{
  ITopLevelWindow wndNotepad = Driver.Find<IProcess>(new ProcessPattern()
  {
    ProcessName = "notepad"
  }).Find<ITopLevelWindow>(new WindowPattern()
  {
    WndClass = "Notepad"
  });

  wndNotepad.Click(30, 40);
}

Visual Basic .NET

Imports SmartBear.TestLeft
Imports SmartBear.TestLeft.TestObjects


Public Sub Test()
  Dim wndNotepad As ITopLevelWindow = Driver.Find(Of IProcess)(New ProcessPattern() With {
          .ProcessName = "notepad"
  }).Find(Of ITopLevelWindow)(New WindowPattern() With {
          .WndClass = "Notepad"
  })

  wndNotepad.Click(30, 40)
End Sub

Java

import com.smartbear.testleft.*;
import com.smartbear.testleft.testobjects.*;


@Test
public void Test() throws Exception{

  TopLevelWindow wndNotepad = driver.find(TestProcess.class, new ProcessPattern() {{
    ProcessName = "notepad";
  }}).find(TopLevelWindow.class, new WindowPattern() {{
    WndClass = "Notepad";
  }});

  wndNotepad.click(30, 40);
}

Possible Issues

Before simulating a click on a control, TestLeft performs a number of verifications:

  • Checks whether the point where a click will be performed is visible on screen.

  • Checks whether the point where a click will be performed is not overlapped by another control or window.

It is possible that during the check, another control or window may overlap the click point or the entire control, or the control where a click will be performed may change its position. This typically happens in applications and on web pages that have animated elements.

To avoid possible problems, you can pause the test run until the animation is over by using the WaitProperty method. The method pauses the test run until the control’s state changes. For instance, you can determine the 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.

It may be difficult to determine the state of the tested window or control before simulating mouse actions. You can enable TestLeft Visualizer to capture images of the tested windows and controls automatically during the test run.

See Also

Simulating User Actions
Creating TestLeft Tests
About Driver Objects
Object Identification

Highlight search results