0

I am writing some Automation tests using Teststack white framework (C#, .net) for my WPF application. I want any cursor movement to be frozen while the tests are running. Is there any way to do that?

I already tried

public partial class NativeMethods
{
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool BlockInput(bool fBlockIt);
}

but it is not working as my application is not running with Admin rights

I tried NativeMethods.BlockInput(true);

I am getting access denied exception

An Kumar
  • 77
  • 1
  • 8
  • Simply disconnect your mouse. – Sinatr Apr 15 '19 at 07:06
  • As the code will go to the production.. How will it work there? – An Kumar Apr 15 '19 at 07:09
  • Are you asking how automations test will run at customer? Sure thing: they will report a problem and you will ask them to disconnect the mouse. Talking seriously: our UI tests are running on dedicated PC which has no keyboard/mouse at all, we connect to it remotely (rarely). Not sure what problem are you trying to solve. To use PC for automation tests and to serf internet there at the same time? Rather don't. – Sinatr Apr 15 '19 at 07:12

1 Answers1

0

I think you can use ClipCursor

[DllImport("user32.dll")]
    static extern void ClipCursor(ref System.Drawing.Rectangle rect);

    [DllImport("user32.dll")]
    static extern void ClipCursor(IntPtr rect);

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        this.Loaded += MainWindow_Loaded;
        this.MouseMove += Window_MouseMove;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.WindowState = WindowState.Maximized;
        HideMouse();
    }
    private void Window_MouseMove(object sender, MouseEventArgs e)
    {
        HideMouse();
    }

    private void HideMouse()
    {
        System.Drawing.Rectangle r = new System.Drawing.Rectangle(0, 0, 0, 0);
        ClipCursor(ref r);
    }
Justin CI
  • 2,693
  • 1
  • 16
  • 34