开发者

How do I prevent Selenium RC from stealing window focus while my tests are running?

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?

开发者_JS百科

I'm using Firefox 3.6.13.


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.


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.


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.


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.


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.


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜