Selenium Chrome window running in second monitor screen stealing focus

前端 未结 4 1293
感动是毒
感动是毒 2021-01-01 23:00

I use WebDriver with Chrome Driver 2.35 with Selenium.

I have a dual monitor setup in which I run my test cases. I do run my cases headlessly most of the times but

相关标签:
4条回答
  • 2021-01-01 23:12

    Selenium chromedriver does steal the focus only once at startup. Afterwards you can run the browser in the background without any problems (we're doing it that way, using chromedriver 2.30, so I'm sure it works)

    Therefore you probably have some code in your web tests which performs the focus stealing:

    1) Changing the active window or opening a new window / new tab causes focus stealing.

    2) You explicitly call Focus() on an element.

    Are you sure your code is free of such calls?

    0 讨论(0)
  • 2021-01-01 23:12

    My solution to focus being stolen has always been to just use a VM or a remote computer and attach to the process remotely to do the debugging if needed.

    0 讨论(0)
  • 2021-01-01 23:18

    I would suggest setting the window position to be always on bottom. In C#, you can do this through a class that looks like:

    public static class SetWindowPosition
    {
        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
        static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
        private const UInt32 SWP_NOSIZE = 0x0001;
        private const UInt32 SWP_NOMOVE = 0x0002;
        private const UInt32 SWP_NOACTIVATE = 0x0010;
    
        [DllImport("user32.dll")]
        private static extern bool LockSetForegroundWindow(uint uLockCode);
        private const UInt32 LSFW_LOCK = 1;
    
        public static void ForceWindowToStayOnBottom(Process process)
        {
            SetWindowPos(
                process.MainWindowHandle, // The handle of the browser window
                HWND_BOTTOM, // Tells it the position, in this case the bottom
                0, 0, 0, 0, // Coordinates for sizing of the window - Will be overriden by NOSIZE
                SWP_NOSIZE | // Says to keep the window its current size
                SWP_NOMOVE | // Says to keep the window in its current spot
                SWP_NOACTIVATE // Activation brings the window to the top, we don't want that
            );
    
            // If you don't notice this helping, it can probably be deleted.
            // It only deals with other applications and gets automatically undone on user interaction
            LockSetForegroundWindow(
                LSFW_LOCK); // Locks calls to SetForegroundWindow 
        }
    
        /// <returns> Returns the parent process of each process by the name of processName </returns>
        public static List<Process> GetPrimaryProcesses(string processName) =>
    
            Process.GetProcesses() // Gets a list of every process on computer
                .Where(process => process.ProcessName.Contains(processName) // Reduces the list to every process by the name we are looking for
                    && process.MainWindowHandle != IntPtr.Zero) // Removes any process without a MainWindow (which amounts to every child process)
                .ToList();
    }
    

    called as:

    SetWindowPosition.ForceWindowToStayOnBottom(
        SetWindowPosition.GetPrimaryProcesses("chrome")[0]);
    

    where [0] is the index of the window you want to minimize (generally they get returned in the order opened).

    What this does is similar to what you may have done when you set a window to be AlwaysOnTop (try opening task manager, clicking options, and then selecting Always On Top for an example), except in reverse. You should be able to call this on any process you like, including the web driver console window.

    If you want further reading on how this works, these two posts (1, 2) were what I used to get it working, aswell as the MSDN documentation here.

    0 讨论(0)
  • 2021-01-01 23:36

    Even I have faced this Issue, the solution I found was i ran my solution from the second monitor (i.e) Dragged the Eclipse to the second monitor and then Ran the test.

    0 讨论(0)
提交回复
热议问题