org.openqa.selenium.server
Class BrowserSessionFactory

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

public class BrowserSessionFactory
extends 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:
[email protected] (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  Set<BrowserSessionFactory.BrowserSessionInfo> activeSessions
           
protected  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(String browserString, String startURL, String extensionJs, Capabilities browserConfiguration, boolean ensureClean, RemoteControlConfiguration configuration)
          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(RemoteControlConfiguration configuration)
          Ends all browser sessions.
 void endBrowserSession(boolean forceClose, String sessionId, RemoteControlConfiguration configuration)
          Ends a browser session, using SeleniumServer static fields to populate parameters.
protected  void endBrowserSession(boolean forceClose, String sessionId, RemoteControlConfiguration configuration, boolean ensureClean)
          Ends a browser session.
 void endBrowserSession(String sessionId, RemoteControlConfiguration configuration)
          Ends a browser session, using SeleniumServer static fields to populate parameters.
protected  BrowserSessionFactory.BrowserSessionInfo getNewBrowserSession(String browserString, String startURL, String extensionJs, Capabilities browserConfigurations, boolean useCached, boolean ensureClean, RemoteControlConfiguration configuration)
          Gets a new browser session
 BrowserSessionFactory.BrowserSessionInfo getNewBrowserSession(String browserString, String startURL, String extensionJs, Capabilities browserConfigurations, RemoteControlConfiguration configuration)
          Gets a new browser session, using the SeleniumServer static fields to populate parameters.
protected  FrameGroupCommandQueueSet getQueueSet(String sessionId)
          Isolated dependency
protected  BrowserSessionFactory.BrowserSessionInfo grabAvailableSession(String browserString, String baseUrl)
          Retrieves an available, unused session from the cache.
protected  boolean hasActiveSession(String sessionId)
          for testing only
protected  boolean hasAvailableSession(String sessionId)
          for testing only
protected  BrowserSessionFactory.BrowserSessionInfo lookupInfoByBrowserAndUrl(String browserString, String baseUrl, Set<BrowserSessionFactory.BrowserSessionInfo> set)
          Looks up a session in the named set by browser string and base URL
protected  BrowserSessionFactory.BrowserSessionInfo lookupInfoBySessionId(String sessionId, Set<BrowserSessionFactory.BrowserSessionInfo> set)
          Looks up a session in the named set by session id
protected  FrameGroupCommandQueueSet makeQueueSet(String sessionId, int port, RemoteControlConfiguration configuration)
          Isolated dependency
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()
           
protected  void shutdownBrowserAndClearSessionData(BrowserSessionFactory.BrowserSessionInfo sessionInfo)
          Shuts down this browser session's launcher and clears out its session data (if session is not null).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

availableSessions

protected final Set<BrowserSessionFactory.BrowserSessionInfo> availableSessions

activeSessions

protected final 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(String browserString,
                                                                     String startURL,
                                                                     String extensionJs,
                                                                     Capabilities browserConfigurations,
                                                                     RemoteControlConfiguration configuration)
                                                              throws RemoteCommandException
Gets a new browser session, using the SeleniumServer static fields to populate parameters.

Parameters:
browserString -
startURL -
extensionJs - per-session user extension Javascript
configuration - Remote Control configuration. Cannot be null.
Returns:
the BrowserSessionInfo for the new browser session.
Throws:
RemoteCommandException

getNewBrowserSession

protected BrowserSessionFactory.BrowserSessionInfo getNewBrowserSession(String browserString,
                                                                        String startURL,
                                                                        String extensionJs,
                                                                        Capabilities browserConfigurations,
                                                                        boolean useCached,
                                                                        boolean ensureClean,
                                                                        RemoteControlConfiguration configuration)
                                                                 throws RemoteCommandException
Gets a new browser session

Parameters:
browserString -
startURL -
extensionJs - per-session user extension Javascript
configuration - Remote Control configuration. Cannot be null.
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(RemoteControlConfiguration configuration)
Ends all browser sessions.

Active and available but inactive sessions are ended.


endBrowserSession

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

Parameters:
sessionId - the id of the session to be ended
configuration - Remote Control configuration. Cannot be null.

endBrowserSession

public void endBrowserSession(boolean forceClose,
                              String sessionId,
                              RemoteControlConfiguration configuration)
Ends a browser session, using SeleniumServer static fields to populate parameters.

Parameters:
sessionId - the id of the session to be ended
configuration - Remote Control configuration. Cannot be null.

endBrowserSession

protected void endBrowserSession(boolean forceClose,
                                 String sessionId,
                                 RemoteControlConfiguration configuration,
                                 boolean ensureClean)
Ends a browser session.

Parameters:
sessionId - the id of the session to be ended
configuration - Remote Control configuration. Cannot be null.
ensureClean - if clean sessions (e.g. no leftover cookies) are required.

shutdownBrowserAndClearSessionData

protected void shutdownBrowserAndClearSessionData(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(String browserString,
                                                                        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.

makeQueueSet

protected FrameGroupCommandQueueSet makeQueueSet(String sessionId,
                                                 int port,
                                                 RemoteControlConfiguration configuration)
Isolated dependency

Parameters:
sessionId -
port -
configuration -
Returns:
a new FrameGroupCommandQueueSet instance

getQueueSet

protected FrameGroupCommandQueueSet getQueueSet(String sessionId)
Isolated dependency

Parameters:
sessionId -
Returns:
an existing FrameGroupCommandQueueSet instance

createNewRemoteSession

protected BrowserSessionFactory.BrowserSessionInfo createNewRemoteSession(String browserString,
                                                                          String startURL,
                                                                          String extensionJs,
                                                                          Capabilities browserConfiguration,
                                                                          boolean ensureClean,
                                                                          RemoteControlConfiguration configuration)
                                                                   throws RemoteCommandException
Creates and tries to open a new session.

Parameters:
browserString -
startURL -
extensionJs -
configuration - Remote Control configuration. Cannot be null.
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(String sessionId,
                                                                         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(String browserString,
                                                                             String baseUrl,
                                                                             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(String sessionId)
for testing only


hasAvailableSession

protected boolean hasAvailableSession(String sessionId)
for testing only


addToAvailableSessions

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



Copyright © 2012. All Rights Reserved.