Selenium ChromeDriver-在后台运行但不是无头的
我正在寻找一种在后台使用chrome驱动程序运行硒测试的方法.我所说的背景,不是前景聚焦的窗口.我实际上可以做到,但是只要chrome驱动程序中发生了诸如打开新标签或在窗口之间切换(基本上就是切换窗口句柄)之类的操作,浏览器窗口就会被推到前台.
I am looking for a way to run selenium tests with a chrome driver, in the background. With the background I mean as in, not the foreground focussed window. I can actually do this, but as soon as actions like opening a new tab or switching between windows (so basically switching window handles) happen in the chrome driver, the browser window gets pushed to the foreground.
所以我的问题是,如何在不运行测试的情况下防止这种情况的发生?
So my question is, how can I prevent this from happening without running the test headless?
任何建议都值得赞赏,欢迎大家讨论.
Any suggestions are appreciated, open for discussion.
作为一种临时解决方案,我提出了以下建议.
As a somewhat temporary solution I came up with the following.
我使用Windows 10虚拟桌面功能运行测试,因此在单独的虚拟桌面中运行chrome浏览器窗口. 然后,我切换回主虚拟桌面以继续执行其他任务. 这样可以防止Chrome浏览器窗口被强制移到前台.
Using the Windows 10 Virtual Desktops feature, I run the test and thus the chrome browser window in a seperate virual desktop. I then switch back to my main virtual desktop to continue with other tasks. This prevents the chrome browser window from being forced to the foreground.
请注意,当上述任何一种操作出现时,这仍然会使任务栏中的Chrome图标闪烁.
Note that this still makes a flashing chrome icon appear in the taskbar when any of the actions described above appear.
仍在寻找更可靠的解决方案,因此任何建议仍会得到体现.
Still looking for a more solid solution, so any suggestions are still appreaciated.
希望打开新的观点和讨论点,并最终找到解决方案,我将提供一些有关代码运行情况的详细信息.
To hopefully open new perspectives and discussion points, and ultimately a solution, I will provide some more detailed info of what my code is doing.
我有5个chrome网络驱动程序,每个网络驱动程序都包含6个标签页(WindowHandles). 这个想法是必须在每个选项卡上连续重复一定的过程.因此,我们遍历每个WebDriver,并在该WebDriver中的每个选项卡上,并将此选项卡设置为WebDriver当前的WindowHandle.这样一来,Chrome浏览器窗口就会明显切换到分配的标签.
I have 5 chrome webdrivers, and each of these webdrivers contains 6 tabs (WindowHandles). The idea is that a certain process has to be repeated continuously on each tab. So we loop over each webdriver, and within that webdriver over each tab and set this tab as the webdrivers current WindowHandle. This makes the chrome window visibly switch to the assigned tab.
在进行了此切换之后,将在选项卡的内容上执行基本的硒自动化操作,直到我们重复整个过程为止.
After that switch has taken place, so basic selenium automation is performed on the content of the tab, after wich we repeat the whole process.
当chrome webdriver切换到新标签页(WindowHandle)时,似乎发生了实际问题,这时包含标签页的chrome窗口被推到前台并夺走了焦点.请注意,这并不总是发生,它经常可以切换标签而没有任何问题.因此,尚不清楚是否还有其他因素会导致窗口窃取焦点.
The actual issue seems to take place when a chrome webdriver switches to a new tab (WindowHandle), at this moment the chrome window containing the tab is pushed to the foreground and steals focus. Note that this does not always takes place, often it can switch tabs without any issues. So it is unclear wether there is another factor which would cause the window to steal focus.
执行以下操作后:
我重载了硒方法,该方法用于在选项卡(WindowHandles)之间切换,并且每次都调用SetWindoPos.不幸的是,这也没有解决这个问题.我将尝试更深入地研究造成这种情况的原因,并向您报告. – S. Van den Wyngaert
I overloaded the selenium method which is used to switch between tabs (WindowHandles), and called SetWindoPos each time. Unfortunately this did not solve this issue either. I will try to look deeper into what might be causing this and will report back. – S. Van den Wyngaert
在运行测试的过程中,我出去了几个小时,然后回过头来,发现我仍然在我的主要虚拟桌面(win10功能)上令人惊讶.这意味着在我离开时没有发生此问题.我再次开始工作,打开了一个chrome窗口,发现执行此操作后不久,焦点再次被chrome驱动程序的一个窗口窃取.
I went out for a few hours while running the tests, and came back to see that surprisingly I was still on my main Virtual Desktop (win10 feature). This means that the issue didn't occur during the time I was gone. I started working again, opened a chrome window and noticed that shortly after I did this, focus was stolen by one of the chrome driver's windows again.
由此得出的结论是,仅当打开另一个chrome窗口(不是由chrome驱动程序从代码中打开)时,才会出现此问题. 我会继续调查,并会提供最新报告.
What I conclude from this is that the issue only occurs when another chrome window (not opened by a chrome driver from code) is opened. I will keep investigating and will report back with updates.
研究了好几个小时后,我注意到当另一个镀铬窗口打开时,只要有焦点,就不会发生此问题.因此,快速总结一下:
After investigating this behavior for a few more hours I noticed that when another chrome window is open, as long as this has focus, the issue doesn't take place. So to quickly summarize this:
在以下情况下不会发生此问题:
- 没有打开或最小化其他镀铬窗口(非硒驱动)
- 另一个镀铬窗口(不是硒驱动的)打开并具有焦点
- 另一个以全屏模式运行的应用程序具有焦点
在以下情况下确实会发生此问题:
- 另一个镀铬窗口(不是硒驱动的)被打开并且没有焦点
请注意,当我谈论另一个镀铬窗口时,我指的是不是由硒驱动的镀铬窗口,因此是由用户打开的常规镀铬窗口.
Note that when I talk about another chrome window I specificly mean a chrome window that is not driven by selenium, so a regular chrome window opened by the user.
如果您的计算机具有运行docker的能力,则扩展解决方案的另一个好选择是使用 zalenium
Another good option to scale your solution, and if your machine has the capacity to run docker, is to use zalenium
这是一个基于docker的自动缩放硒网格解决方案,开箱即用.
It's a docker based, auto scaling, selenium-grid solution that works pretty quick out of the box.
您可以通过管理页面实时观看测试,事实之后观看记录,通过VNC进行实时互动来暂停/调试.还可以说不用担心更改浏览器版本的事情.
You can watch your tests live via the management pages, watch a recording after the fact, pause/debug with live interaction via VNC. Also something to be said for not having the worry about changing browser versions.
我上次使用它时,它出现了一个奇怪的错误,并时不时地抛出流错误的结尾-但这是一个好年头.
Last time i used it, it had the odd bug and throws an end of stream error every now and then - but that was a good year ago.