How to run a mouse_event on a windows remote machine?

48 Views Asked by At

I have this piece of code for clicking on a specific point for a UI test using WinAppDriver. it is OK in the local machine but my code is suppose to run from my local machine but executing on a remote machine.

the rest of the test is handled by the WinAppDriver and Appium server, but this part is not working because it is executed on my local machine.

how can I do this ?

class ClickSimulator
{
    [DllImport("user32.dll")]
    static extern bool SetCursorPos(int X, int Y);

    [DllImport("user32.dll")]
    public static extern void mouse_event(uint dwFlags, int dx, int dy, uint cButtons, uint dwExtraInfo);

    public const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
    public const uint MOUSEEVENTF_LEFTUP = 0x0004;

    public void ClickAtScreenCoordinates(int x, int y)
    {
        SetCursorPos(x, y);
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
    }
}
1

There are 1 best solutions below

0
Esmaeli On

To run mouse_event on a Windows remote machine, you can use a tool like PsExec to execute your code remotely.

For example:

using System.Diagnostics;

class ClickSimulator
{
    public void ClickAtScreenCoordinatesRemote(int x, int y, string remoteMachine)
    {
        Process.Start("psexec.exe", $"\\\\{remoteMachine} -accepteula -nobanner -s -d {System.Reflection.Assembly.GetExecutingAssembly().Location} Click {x} {y}");
    }

    public void Click(int x, int y)
    {
        // Code for local execution
        SetCursorPos(x, y);
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
    }
}

Run your application with commandline arguments:

YourApp.exe Click 123 456