Mark Needham

Thoughts on Software Development

Selenium – Selecting the original window

with 6 comments

I’ve not used Selenium much in my time – all of my previous projects have been client side applications or service layers – but I’ve spent a bit of time getting acquainted with it this week.

While activating some acceptance tests this week I noticed quite a strange error happening if the tests ran in a certain order:

com.thoughtworks.selenium.SeleniumException: ERROR: Current window or frame is closed! 
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:73)
at com.thoughtworks.selenium.HttpCommandProcessor.getString(HttpCommandProcessor.java:154)
at com.thoughtworks.selenium.HttpCommandProcessor.getBoolean(HttpCommandProcessor.java:227)
at com.thoughtworks.selenium.DefaultSelenium.isElementPresent(DefaultSelenium.java:394)

I tried commenting out some of the tests and then running the other ones and everything worked fine but when I ran all of them the problem returned.

Eventually after some eagle eyed debugging by a colleague of mine we realised that the only difference was the lack of the following line in the failing test:

selenium.selectWindow(null);

A quick look at the documentation explains precisely why this works:

if windowID is null, (or the string “null”) then it is assumed the user is referring to the original window instantiated by the browser).

What had in fact happened was that the previous test had launched a pop up window and when it closed that window the focus wouldn’t return to the original window launched when Selenium first started unless we executed the above method call.

Written by Mark Needham

October 25th, 2008 at 1:55 am

Posted in Testing

Tagged with ,

  • Harry N

    this does not work in my case. In my case the modal popup window closed itself after seding the parent to a different URL. No matter how the parent window is selected the error persists.

  • Lars JJ

    I have the same problem in with our test scripts. But it seems to happen sometimes and sometimes not. We are using selenium.selectWindow(null);

  • Alvin de Castro

    Big thanks to this site!

    I had difficulty regarding the switching of pop ups while testing in Selenium and this tidbit nailed it.

    Thanks again!

  • http://- Shwet

    Could you pls suggest something as selenium.selectWindow(null);this command is not working.
    and the Chrome converts the remote runner window.

  • Bkpazand

    I had a similar problem which is now fixed. Many thanks for sharing this with us.

  • KP

    Thanks