Mark Needham

Thoughts on Software Development

Selenium – Selecting the original window

with 4 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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • HackerNews
  • StumbleUpon
  • Twitter

Written by Mark Needham

October 25th, 2008 at 1:55 am

Posted in Testing

Tagged with ,

4 Responses to 'Selenium – Selecting the original window'

Subscribe to comments with RSS or TrackBack to 'Selenium – Selecting the original window'.

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

    Harry N

    3 Jul 09 at 7:16 am

  2. 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);

    Lars JJ

    29 Jul 09 at 6:00 pm

  3. 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!

    Alvin de Castro

    27 Jan 10 at 11:28 am

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

    Shwet

    9 Aug 10 at 11:46 am

Leave a Reply