当我的测试运行时,如何防止 Selenium RC 窃取窗口焦点?

发布于 2024-10-31 18:52:33 字数 294 浏览 1 评论 0原文

我知道我可能属于少数,但我必须在运行测试的同时使用我的机器。一直困扰我的事情是,当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

深空失忆 2024-11-07 18:52:34

在 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.

水波映月 2024-11-07 18:52:34

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.

淡紫姑娘! 2024-11-07 18:52:34

你运行的是linux吗? FirefoxProfile 类有一个记录不足的设置,该设置加载一个特殊的库以避免在 Linux 上窃取焦点 - 将其设置为 true:

https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/firefox/FirefoxProfile。 html#setAlwaysLoadNoFocusLib(布尔值)

公共无效setAlwaysLoadNoFocusLib(布尔loadNoFocusLib)

设置是否应始终在 Linux 上加载无焦点库。

参数:

loadNoFocusLib - 是否始终加载无焦点库。

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)

public void setAlwaysLoadNoFocusLib​(boolean loadNoFocusLib)

Sets whether the no focus library should always be loaded on Linux.

Parameters:

loadNoFocusLib - Whether to always load the no focus library.

天气好吗我好吗 2024-11-07 18:52:34

我们通过在测试之间共享 selenium 实例解决了这个问题。然后,selenium 在整个测试运行期间仅尝试窃取焦点一次,这还不错。

如果您使用 JUnit 编写 Selenium 脚本,则可以使用 Spring 的 SpringJUnit4ClassRunner 将 selenium 实例作为 bean 注入。

为 Spring selenium.xml 定义一个测试上下文(如果您不熟悉如何设置 Spring XML 配置文件,请谷歌它)并包含一个 selenium 实例:

<bean class="com.thoughtworks.selenium.DefaultSelenium" name="selenium">
    <constructor-arg index="0">
        <value>localhost</value>
    </constructor-arg>
    <constructor-arg index="1">
        <value>4444</value>
    </constructor-arg>
    <constructor-arg index="2">
        <value>*firefox</value>
    </constructor-arg>
    <constructor-arg index="3">
        <value>http://localhost:8080/webapp/</value>
    </constructor-arg>
</bean>

然后在您的测试中注入 selenium实例而不是 new :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:selenium.xml")
public class WebappIT {

    @Resource
    private Selenium selenium;

    ... test code ...

}

我对此进行了一些简化,在我们的实际代码中,我们将 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:

<bean class="com.thoughtworks.selenium.DefaultSelenium" name="selenium">
    <constructor-arg index="0">
        <value>localhost</value>
    </constructor-arg>
    <constructor-arg index="1">
        <value>4444</value>
    </constructor-arg>
    <constructor-arg index="2">
        <value>*firefox</value>
    </constructor-arg>
    <constructor-arg index="3">
        <value>http://localhost:8080/webapp/</value>
    </constructor-arg>
</bean>

Then in your test, inject the selenium instance instead of new'ing it:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:selenium.xml")
public class WebappIT {

    @Resource
    private Selenium selenium;

    ... test code ...

}

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.

末が日狂欢 2024-11-07 18:52:34

更新

我找到了一个解决 Firefox 窗口焦点窃取问题的方法。如果您像我一样将 Selenium 作为独立服务器运行,那么您可以添加以下内容:

-browserSessionReuse

到启动 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:

-browserSessionReuse

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.

冷弦 2024-11-07 18:52:34

我们不能在同一台机器上做到这一点。我们要么需要虚拟机,要么应该在控制器级别对其进行编程,以便我们的脚本不会启动任何浏览器来运行脚本。

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.

南笙 2024-11-07 18:52:34

使用 chrome 而不是 firefox 对我有用。只需在另一个工作区中启动它,它就会保持原状。

Using chrome instead of firefox worked for me. Just launch it in another workspace and it stays put.

凡尘雨 2024-11-07 18:52:34

如果您在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文