private ReusableRemoteWebDriver createReusableDriver(URL remoteAddress, Capabilities desiredCapabilities) {
// construct init params
InitializationParameter initParam = new InitializationParameter(remoteAddress, desiredCapabilities);
ReusableRemoteWebDriver driver = null;
// try to reuse the session
// retrieve the session id
ReusedSession stored = sessionStore.get().pull(initParam);
// get all the stored sessions for given initParam
while (stored != null) {
SessionId reusedSessionId = stored.getSessionId();
try {
driver = ReusableRemoteWebDriver.fromReusedSession(remoteAddress, desiredCapabilities, reusedSessionId);
break;
} catch (UnableReuseSessionException ex) {
log.log(Level.WARNING, "Unable to reuse session: {0}", stored.getSessionId());
}
stored = sessionStore.get().pull(initParam);
}
if (driver == null) {
// if either browser session isn't stored or can't be reused
RemoteWebDriver newdriver = createRemoteDriver(remoteAddress, desiredCapabilities);
driver = ReusableRemoteWebDriver.fromRemoteWebDriver(newdriver);
}
initParams.get().put(driver.getSessionId(), initParam);
return driver;
}