11

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.

8 Answers 8

11

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.

2
  • That, of course was my first choice... I like your creativity too... If only I were allowed to run linux! +1 for a neat hack.
    – bakoyaro
    Apr 12, 2011 at 13:16
  • 2
    Run linux in a VM in windows :D
    – mark-cs
    Apr 14, 2011 at 16:30
7

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.

0
5

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.

3
  • 1
    "Darn programmers and their lack of documentation!" yelled the guilty party;-) I use Linux on some projects, windows on others, this will definitely help out.
    – bakoyaro
    Sep 24, 2013 at 12:20
  • This is a broken link
    – ar2015
    Oct 5, 2022 at 23:54
  • fixed and added a snippet in case the link breaks again - no idea if this is still relevant in 2022 though!
    – Korny
    Oct 11, 2022 at 10:16
3

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.

0
2

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.

1

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.

1

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

0

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.