Tuesday, November 11, 2008

Selenium close all Firefox windows in profile when finish run

There is a Chrome URL parameter &close=true in Selenium-IDE. When set to true, it will automatically close Selenium Firefox windows when finish run. However, if application window has popuped child windows like ads, selenium can't close these popup windows when quit.

This has risk to lock Firefox profile when multiple Firefox profiles running simuteniously. Please refer to "Firefox is aleady running issue"
Enhance Selenium-IDE by XPCOM API: close all Firefox windows under current profile. Add logic when Selenium quit(close=true).
Sample code:
var windowManager = Components
  .classes['@mozilla.org/appshell/window-mediator;1'].getService();
var windowManagerInterface = windowManager
  .QueryInterface( Components.interfaces.nsIWindowMediator);
var enumerator = windowManagerInterface.getEnumerator( null );
var appStartup = Components
  .classes['@mozilla.org/toolkit/app-startup;1'].
  getService(Components.interfaces.nsIAppStartup);
while ( enumerator.hasMoreElements()  )
{
  var domWindow = enumerator.getNext();
  if (("tryToClose" in domWindow) 
       && !domWindow.tryToClose())
    return false;
  domWindow.close();
};
appStartup.quit(Components.interfaces.nsIAppStartup.eAttemptQuit);

No comments:

Post a Comment