Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

Alternative mouse calls for Silk4Net, Workbench and 4Test

Alternative mouse calls for Silk4Net, Workbench and 4Test

Occasionally when recording a test on a testing highly customised application SilkTest will record a Click() but will not play it back. Changing the Click() for a Select() has no effect either. Shown below is a class that presents a third way of playing back mouse actions against a recalcitrant application by calling directly to the Windows mouse API. Both the dblClick() and dragDrop() methods have optional delays before the second action is carried out. In the case of dblClick(0 this may help with applications that have one action on mousedown and a second action on mouseup. With the dragDrop method a small delay may be needed before the drag action to ensure that the object to be dragged has been registered.

The 4Test code is directly below the vb.Net code. Of the two attachments, mouseClass.zip is for Silk4Net and Workbench and mouseinc.zip is for 4Test.

Imports System.Drawing

Public Class mouse

    ' Unmanaged code. Microsoft recommend using SendInput but it doesn't have the same flexibility
    Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Integer
    Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Integer
    Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)

    ' Mouse constants
    Public Const MOUSEEVENTF_LEFTDOWN = &H2           ' left button down
    Public Const MOUSEEVENTF_LEFTUP = &H4             ' left button up
    Public Const MOUSEEVENTF_MIDDLEDOWN = &H20        ' middle button down
    Public Const MOUSEEVENTF_MIDDLEUP = &H40          ' middle button up
    Public Const MOUSEEVENTF_RIGHTDOWN = &H8          ' right button down
    Public Const MOUSEEVENTF_RIGHTUP = &H10           ' right button up

    ' ----------------------------------------------------------
    ' Single left click
    Public Sub lClick()

        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

    End Sub

    ' ----------------------------------------------------------
    ' DoubleClick
    Public Sub dblClick(Optional ByVal delay As Integer = 0)

        Dim i As Integer = 0
        For i = 0 To 1
            If i = 1 Then System.Threading.Thread.Sleep(delay)
            Call lClick()
        Next

    End Sub

    ' ----------------------------------------------------------
    ' Right click
    Public Sub rClick()

        mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)

    End Sub

    ' ----------------------------------------------------------
    ' Middle click
    Public Sub mClick()

        mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)

    End Sub

    ' ----------------------------------------------------------
    ' Drag'n'drop
    ' x and y are in pixels and should be the DROP point
    Public Sub dragDrop(ByVal x As Integer, ByVal y As Integer, Optional ByVal delay As Integer = 0)

        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        System.Threading.Thread.Sleep(delay)
        SetCursorPos(x, y)
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

    End Sub

    ' ----------------------------------------------------------
    ' Get the current cursor location - useful for checking
    Public Function cursorPos() As Point

        Dim curPos As Point
        Dim ret As Long = GetCursorPos(curPos)

        Return curPos

    End Function

End Class


To use the class you will need to ensure that you have a reference to System.Drawing in your project and you will need to declare a new instance of the class:


dim mouse as new mouse

The class calls will then be as follows:

mouse.lClick                          ' Left click
mouse.rClick                          ' Right click
mouse.dblClick(optional delay ms)     ' Double click
mouse.mClick                          ' Middle click
mouse.dragDrop(x,y,optional delay ms) ' Drag and drop - x and y are the DROP point in pixels.

To use any of these functions you will need to position the cursor first using the in-built MouseMove function e.g.

    <TestMethod()> Public Sub TestMethod1()
        With _desktop.FormsWindow("CSV no DSN Test")
            .SetActive()

            ' Clear button
            .MouseMove(New Point(140, 290))
            ' New left click at this position
            mouse.lClick()
            System.Threading.Thread.Sleep(2000)
            ' Read CSV button
            .MouseMove(New Point(240, 290))
            ' Read the csv file
            mouse.lClick()

//-----------------------------------------------------------------------------------------

4Test equivalent code follows:

[ ] // Public constants for mouse actions
[ ] const string lClick = "lClick"
[ ] const string rClick = "rClick"
[ ] const string mClick = "mClick"
[ ] const string dblClick = "dblClick"
[ ] const string dragNDrop = "dragAndDrop"
[ ]
[-] dll "user32.dll"
    [ ] mouse_event (long dwFlags ,long dx,long dy,long cButtons,long dwExtraInfo)
    [ ] SetCursorPos (long x, long y)
    [ ]
[-] void mouse(string sClickType, integer x optional, integer y optional, integer iDelay optional)
    [ ]
    [ ] // Constants to manipulate different buttons on mouse
    [ ] const MOUSEEVENTF_LEFTDOWN = 2
    [ ] const MOUSEEVENTF_LEFTUP = 4
    [ ] const MOUSEEVENTF_MIDDLEDOWN = 14
    [ ] const MOUSEEVENTF_MIDDLEUP = 64
    [ ] const MOUSEEVENTF_RIGHTDOWN = 8
    [ ] const MOUSEEVENTF_RIGHTUP = 16
    [ ]
    [ ] integer dx=0
    [ ] integer dy=0
    [ ] integer delaytime=0
    [ ]
    [ ] // Left click
    [-] if sClickType==lClick
        [ ] mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        [ ] mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        [ ]
    [ ] // Right click
    [-] if sClickType==rClick
        [ ] mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
        [ ] mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
        [ ]
    [ ] // Middle click
    [-] if sClickType==mClick
        [ ] mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
        [ ] mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)
    [ ]
    [ ] // Doubleclick - can have a delay
    [-] if sClickType==dblClick
        [-] if iDelay>0
            [ ] delaytime=iDelay
        [-] else
            [ ] delaytime=0
        [ ] mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        [ ] mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        [ ] sleep(delaytime)
        [ ] mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        [ ] mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    [ ]
    [ ] // Drag and drop - dx and dy are the DROP co-ordinates
    [-] if sClickType==dragNDrop
        [-] if x > 0
            [ ] dx=x
        [-] else
            [ ] dx=0
        [-] if y > 0
            [ ] dy=y
        [-] else
            [ ] dy=0
        [-] if iDelay>0
            [ ] delaytime=iDelay
        [-] else
            [ ] delaytime=0
        [ ] mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        [ ] sleep(delaytime)
        [ ] SetCursorPos(dx,dy)
        [ ] mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        [ ]
    [ ] return

To make use of these function you will need to 'Use' the attached mouse.inc file in your frame file e.g.


[ ] use "mouse.inc"
[ ]
[ ] const wDynamicMainWindow = CSVNoDSN
[ ] // etc..



Calling the additional functions is straightforward e.g.


[-] testcase Test1 () appstate none
    [ ] CSVNoDSN.SetActive()
    [ ] CSVNoDSN.MoveMouse(140,290)
    [ ] mouse(lClick)
    [ ] sleep(2)
    [ ] CSVNoDSN.MoveMouse(240,290)
    [ ] mouse(lClick)

See also http://microfocus.telligenthosting.net/borland/test/silk_test/w/knowledge_base/23752.getting-offsets-for-low-level-events-when-testing-custom-controls.aspx for an easy method of calculating the required MouseMove offsets.

0513.mouseClass.zip

7840.mouseinc.zip

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-10-24 16:53
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.