当我的测试运行时,如何防止 Selenium RC 窃取窗口焦点?
我知道我可能属于少数,但我必须在运行测试的同时使用我的机器。一直困扰我的事情是,当我使用 Selenium RC 运行测试用例时,浏览器窗口总是抢走焦点。这使得我每天无法在一天结束时注销之前多次运行测试。我尝试了 Selenium Grid,但我无法让它只侦听本地主机上的请求,而不是 0.0.0.0 (我的网络管理员的要求)。
我已经深入研究了 Selenium 文档和大量 Selenium 站点,但我一直无法找到明确的答案。我可以在测试运行时防止 Selenium RC 测试窃取窗口焦点吗?
我使用的是 Firefox 3.6.13。
I know I'm probably in a small minority, but I have to use my machine at the same time my tests are running. The thing that always gets in my way is that the browser window is always stealing focus when I run test cases using Selenium RC. Which prevents me from running my tests more than once a day, at the end of the day right before I log out. I tried Selenium Grid, but I can't get it to only listen for requests on localhost, not 0.0.0.0 (a requirement from my network admin).
I've dug through the Selenium documentation, and tons of Selenium sites, but I haven't been able to find a definitive answer. Can I prevent Selenium RC tests from Stealing windows focus while my test are running?
I'm using Firefox 3.6.13.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 Linux 上,您可以在不同的显示器上启动 vncserver(例如:8 或其他),然后让 Selenium 和您的 Firefox 实例使用该显示器。对我工作的地方来说效果很好。
On Linux, you can start up a vncserver on a different display (say, :8 or something) and then have Selenium and your Firefox instances use that display. Works well for us where I work.
在 VM 中运行它。具有在不同操作系统和浏览器下进行测试的额外好处。
据我了解,在同一台机器上你无法避免窃取焦点。
Run it in a VM. Has an extra benefit of ability to test under different OS and browsers.
It is my understanding that you cannot avoid stealing focus while on the same machine.
你运行的是linux吗? FirefoxProfile 类有一个记录不足的设置,该设置加载一个特殊的库以避免在 Linux 上窃取焦点 - 将其设置为 true:
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/firefox/FirefoxProfile。 html#setAlwaysLoadNoFocusLib(布尔值)
Are you running linux? The FirefoxProfile class has a poorly documented setting that loads a special library to avoid focus stealing on linux - set this to true:
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/firefox/FirefoxProfile.html#setAlwaysLoadNoFocusLib(boolean)
我们通过在测试之间共享 selenium 实例解决了这个问题。然后,selenium 在整个测试运行期间仅尝试窃取焦点一次,这还不错。
如果您使用 JUnit 编写 Selenium 脚本,则可以使用 Spring 的 SpringJUnit4ClassRunner 将 selenium 实例作为 bean 注入。
为 Spring
selenium.xml
定义一个测试上下文(如果您不熟悉如何设置 Spring XML 配置文件,请谷歌它)并包含一个 selenium 实例:然后在您的测试中注入 selenium实例而不是 new :
我对此进行了一些简化,在我们的实际代码中,我们将 selenium 实例包装在提供程序类中,以便我们只能调用 selenium.start() 一次。
We solved this problem by sharing the selenium instance between tests. Then selenium only tries to steal focus once during the entire test run, which isn't too bad.
If you're using JUnit to script Selenium, you can use Spring's
SpringJUnit4ClassRunner
to inject the selenium instance as a bean.Define a test context for Spring
selenium.xml
(google it if you're not familiar with how to set up a Spring XML configuration file) and include a selenium instance:Then in your test, inject the selenium instance instead of
new
'ing it:I've simplified this somewhat, in our actual code we wrap the selenium instance in a provider class so that we can call selenium.start() once only.
更新
我找到了一个解决 Firefox 窗口焦点窃取问题的方法。如果您像我一样将 Selenium 作为独立服务器运行,那么您可以添加以下内容:
到启动 Selenium 测试用例的 ant 任务。这也节省了大量时间,因为在运行 selenium 测试时我不必等待两个新的 Firefox 窗口打开。解决方法是,如果每个测试都是独立测试,则每次测试开始时您都必须退出站点。我通过编辑登录方法来查找注销链接来快速完成此操作。如果存在注销链接,则我的测试将单击注销链接,并等待登录页面可用,然后继续测试。
UPDATE
I found a work-around to my focus stealing Firefox window. If you are running Selenium as a stand-alone server as I am, then you can add this:
To the ant task that launches your Selenium test cases. This saves considerable time as well because I don't have to wait for two new Firefox windows to open up while the selenium tests are running. The work around is that you will have to log out of your site every time a test starts, if each of your tests is a standalone test. I accomplished this quickly by editing my log in method to look for my log out link. If the log out link is present then my test clicks the log out link, and waits for the log in page to be available, then continues on with the test.
我们不能在同一台机器上做到这一点。我们要么需要虚拟机,要么应该在控制器级别对其进行编程,以便我们的脚本不会启动任何浏览器来运行脚本。
We can't do that in the same machine. Either we need VM or we should program it in the controller level so that our script will not launch any browser to run the scripts.
使用 chrome 而不是 firefox 对我有用。只需在另一个工作区中启动它,它就会保持原状。
Using chrome instead of firefox worked for me. Just launch it in another workspace and it stays put.
如果您在 Linux 上运行,则可以使用多个桌面并将浏览器设置为始终在特定桌面上打开,而您的 IDE 在不同的桌面上打开。
这就是我在 Ubuntu 中所做的,需要 5 分钟: https://askubuntu.com/a/90014
If you are running on linux, you can use multiples desktops and set to browser to always open at specific desktop, and your IDE in different one.
That's how i did in Ubuntu, It takes 5 minute: https://askubuntu.com/a/90014