org.openqa.selenium.server
Class BrowserSessionFactory

java.lang.Object
  extended by org.openqa.selenium.server.BrowserSessionFactory

public class BrowserSessionFactory
extends java.lang.Object

Manages browser sessions, their creation, and their closure. Maintains a cache of unused and available browser sessions in case the server is reusing sessions. Also manages the creation and finalization of all browser sessions.

Author:
jbevan@google.com (Jennifer Bevan)

Nested Class Summary
static class BrowserSessionFactory.BrowserSessionInfo
          Collection class to hold the objects associated with a browser session.
protected  class BrowserSessionFactory.CleanupTask
          TimerTask that looks for unused sessions in the availableSessions collection.
 
Field Summary
protected  java.util.Set<BrowserSessionFactory.BrowserSessionInfo> activeSessions
           
protected  java.util.Set<BrowserSessionFactory.BrowserSessionInfo> availableSessions
           
 
Constructor Summary
  BrowserSessionFactory(BrowserLauncherFactory blf)
           
protected BrowserSessionFactory(BrowserLauncherFactory blf, long cleanupInterval, long maxIdleSessionTime, boolean doCleanup)
          Constructor for testing purposes.
 
Method Summary
protected  void addToAvailableSessions(BrowserSessionFactory.BrowserSessionInfo sessionInfo)
          for testing only
protected  BrowserSessionFactory.BrowserSessionInfo createNewRemoteSession(java.lang.String browserString, java.lang.String startURL, boolean multiWindow, boolean ensureClean)
          Creates and tries to open a new session.
protected  void deregisterExternalSession(BrowserSessionFactory.BrowserSessionInfo sessionInfo)
          Removes a previously registered external browser session from the list of active sessions.
protected  void endAllBrowserSessions()
          Ends all browser sessions.
protected  void endBrowserSession(BrowserSessionFactory.BrowserSessionInfo sessionInfo)
          Shuts down this browser session's launcher and clears out its session data (if session is not null).
 void endBrowserSession(java.lang.String sessionId)
          Ends a browser session, using SeleniumServer static fields to populate parameters.
 void endBrowserSession(java.lang.String sessionId, boolean cacheUnused)
          Ends a browser session, using SeleniumServer static fields to populate parameters.
protected  void endBrowserSession(java.lang.String sessionId, boolean cacheUnused, boolean ensureClean)
          Ends a browser session.
 BrowserSessionFactory.BrowserSessionInfo getNewBrowserSession(java.lang.String browserString, java.lang.String startURL, boolean multiWindow)
          Gets a new browser session, using the SeleniumServer static fields to populate parameters.
protected  BrowserSessionFactory.BrowserSessionInfo getNewBrowserSession(java.lang.String browserString, java.lang.String startURL, boolean multiWindow, boolean useCached, boolean ensureClean)
          Gets a new browser session
protected  BrowserSessionFactory.BrowserSessionInfo grabAvailableSession(java.lang.String browserString, java.lang.String baseUrl)
          Retrieves an available, unused session from the cache.
protected  boolean hasActiveSession(java.lang.String sessionId)
          for testing only
protected  boolean hasAvailableSession(java.lang.String sessionId)
          for testing only
protected  BrowserSessionFactory.BrowserSessionInfo lookupInfoByBrowserAndUrl(java.lang.String browserString, java.lang.String baseUrl, java.util.Set<BrowserSessionFactory.BrowserSessionInfo> set)
          Looks up a session in the named set by browser string and base URL
protected  BrowserSessionFactory.BrowserSessionInfo lookupInfoBySessionId(java.lang.String sessionId, java.util.Set<BrowserSessionFactory.BrowserSessionInfo> set)
          Looks up a session in the named set by session id
protected  boolean registerExternalSession(BrowserSessionFactory.BrowserSessionInfo sessionInfo)
          Adds a browser session that was not created by this factory to the set of active sessions.
protected  void removeIdleAvailableSessions()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

availableSessions

protected final java.util.Set<BrowserSessionFactory.BrowserSessionInfo> availableSessions

activeSessions

protected final java.util.Set<BrowserSessionFactory.BrowserSessionInfo> activeSessions
Constructor Detail

BrowserSessionFactory

public BrowserSessionFactory(BrowserLauncherFactory blf)

BrowserSessionFactory

protected BrowserSessionFactory(BrowserLauncherFactory blf,
                                long cleanupInterval,
                                long maxIdleSessionTime,
                                boolean doCleanup)
Constructor for testing purposes.

Parameters:
blf - an injected BrowserLauncherFactory.
cleanupInterval - the time between idle available session cleaning sweeps.
maxIdleSessionTime - the max time in ms for an available session to be idle.
doCleanup - whether or not the idle session cleanup thread should run.
Method Detail

getNewBrowserSession

public BrowserSessionFactory.BrowserSessionInfo getNewBrowserSession(java.lang.String browserString,
                                                                     java.lang.String startURL,
                                                                     boolean multiWindow)
                                                              throws RemoteCommandException
Gets a new browser session, using the SeleniumServer static fields to populate parameters.

Parameters:
browserString -
startURL -
multiWindow - if a new session should be started in multiWindow mode
Returns:
the BrowserSessionInfo for the new browser session.
Throws:
RemoteCommandException

getNewBrowserSession

protected BrowserSessionFactory.BrowserSessionInfo getNewBrowserSession(java.lang.String browserString,
                                                                        java.lang.String startURL,
                                                                        boolean multiWindow,
                                                                        boolean useCached,
                                                                        boolean ensureClean)
                                                                 throws RemoteCommandException
Gets a new browser session

Parameters:
browserString -
startURL -
multiWindow - if a new session should be started in multiWindow mode
useCached - if a cached session should be used if one is available
ensureClean - if a clean session (e.g. no previous cookies) is required.
Returns:
the BrowserSessionInfo for the new browser session.
Throws:
RemoteCommandException

endAllBrowserSessions

protected void endAllBrowserSessions()
Ends all browser sessions. Active and available but inactive sessions are ended.


endBrowserSession

public void endBrowserSession(java.lang.String sessionId)
Ends a browser session, using SeleniumServer static fields to populate parameters.

Parameters:
sessionId - the id of the session to be ended

endBrowserSession

public void endBrowserSession(java.lang.String sessionId,
                              boolean cacheUnused)
Ends a browser session, using SeleniumServer static fields to populate parameters.

Parameters:
sessionId - the id of the session to be ended
cacheUnused - if the session should be made available for reuse.

endBrowserSession

protected void endBrowserSession(java.lang.String sessionId,
                                 boolean cacheUnused,
                                 boolean ensureClean)
Ends a browser session.

Parameters:
sessionId - the id of the session to be ended
cacheUnused - if this session should be made available for reuse
ensureClean - if clean sessions (e.g. no leftover cookies) are required.

endBrowserSession

protected void endBrowserSession(BrowserSessionFactory.BrowserSessionInfo sessionInfo)
Shuts down this browser session's launcher and clears out its session data (if session is not null).

Parameters:
sessionInfo - the browser session to end.

grabAvailableSession

protected BrowserSessionFactory.BrowserSessionInfo grabAvailableSession(java.lang.String browserString,
                                                                        java.lang.String baseUrl)
Retrieves an available, unused session from the cache.

Parameters:
browserString - the necessary browser for a suitable session
baseUrl - the necessary baseUrl for a suitable session
Returns:
the session info of the cached session, null if none found.

createNewRemoteSession

protected BrowserSessionFactory.BrowserSessionInfo createNewRemoteSession(java.lang.String browserString,
                                                                          java.lang.String startURL,
                                                                          boolean multiWindow,
                                                                          boolean ensureClean)
                                                                   throws RemoteCommandException
Creates and tries to open a new session.

Parameters:
browserString -
startURL -
multiWindow - if new session should be opened -multiWindow
ensureClean - if a clean session is required
Returns:
the BrowserSessionInfo of the new session.
Throws:
RemoteCommandException - if the browser failed to launch and request work in the required amount of time.

registerExternalSession

protected boolean registerExternalSession(BrowserSessionFactory.BrowserSessionInfo sessionInfo)
Adds a browser session that was not created by this factory to the set of active sessions. Allows for creation of unmanaged sessions (i.e. no FrameGroupCommandQueueSet) for task such as running the HTML tests (see HTMLLauncher.java). All fields other than session are required to be non-null.

Parameters:
sessionInfo - the session info to register.

deregisterExternalSession

protected void deregisterExternalSession(BrowserSessionFactory.BrowserSessionInfo sessionInfo)
Removes a previously registered external browser session from the list of active sessions.

Parameters:
sessionInfo - the session to remove.

lookupInfoBySessionId

protected BrowserSessionFactory.BrowserSessionInfo lookupInfoBySessionId(java.lang.String sessionId,
                                                                         java.util.Set<BrowserSessionFactory.BrowserSessionInfo> set)
Looks up a session in the named set by session id

Parameters:
sessionId - the session id to find
set - the Set to inspect
Returns:
the matching BrowserSessionInfo or null if not found.

lookupInfoByBrowserAndUrl

protected BrowserSessionFactory.BrowserSessionInfo lookupInfoByBrowserAndUrl(java.lang.String browserString,
                                                                             java.lang.String baseUrl,
                                                                             java.util.Set<BrowserSessionFactory.BrowserSessionInfo> set)
Looks up a session in the named set by browser string and base URL

Parameters:
browserString - the browser string to match
baseUrl - the base URL to match.
set - the Set to inspect
Returns:
the matching BrowserSessionInfo or null if not found.

removeIdleAvailableSessions

protected void removeIdleAvailableSessions()

hasActiveSession

protected boolean hasActiveSession(java.lang.String sessionId)
for testing only


hasAvailableSession

protected boolean hasAvailableSession(java.lang.String sessionId)
for testing only


addToAvailableSessions

protected void addToAvailableSessions(BrowserSessionFactory.BrowserSessionInfo sessionInfo)
for testing only



Copyright © 2008. All Rights Reserved.