Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

I'm using Firefox 3.6.13.

like image 414
bakoyaro Avatar asked Apr 11 '11 18:04

bakoyaro


People also ask

Can I use my computer while Selenium is running?

Running normal Selenium tests take up your screen time, keeping you from being able to accomplish anything else on that device. With the UI disabled, headless testing lets you continue to use your computer while the tests execute in the background.

Which method get focus on new window in Selenium?

Selenium driver object can access the elements of the parent window. In order to switch its focus from the parent to the new popup tab, we shall take the help of the switchTo(). window method and pass the window handle id of the popup as an argument to the method.

Can we minimize window in Selenium?

In order to minimize the browser window in our Selenium automation script, we can use the below-mentioned command. driver. manage(). window().

What are two components of Selenium RC?

Selenium RC comprises of two parts: Client libraries for the preferred computer language. A server that launches and kills browsers automatically.


1 Answers

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.

like image 137
artbristol Avatar answered Oct 24 '22 08:10

artbristol